macro_scope.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. // This file contains all internal macro definitions
  3. // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
  4. // exclude unsupported compilers
  5. #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
  6. #if defined(__clang__)
  7. #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
  8. #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
  9. #endif
  10. #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
  11. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40900
  12. #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
  13. #endif
  14. #endif
  15. #endif
  16. // disable float-equal warnings on GCC/clang
  17. #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
  18. #pragma GCC diagnostic push
  19. #pragma GCC diagnostic ignored "-Wfloat-equal"
  20. #endif
  21. // disable documentation warnings on clang
  22. #if defined(__clang__)
  23. #pragma GCC diagnostic push
  24. #pragma GCC diagnostic ignored "-Wdocumentation"
  25. #endif
  26. // allow for portable deprecation warnings
  27. #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
  28. #define JSON_DEPRECATED __attribute__((deprecated))
  29. #elif defined(_MSC_VER)
  30. #define JSON_DEPRECATED __declspec(deprecated)
  31. #else
  32. #define JSON_DEPRECATED
  33. #endif
  34. // allow to disable exceptions
  35. #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
  36. #define JSON_THROW(exception) throw exception
  37. #define JSON_TRY try
  38. #define JSON_CATCH(exception) catch(exception)
  39. #else
  40. #define JSON_THROW(exception) std::abort()
  41. #define JSON_TRY if(true)
  42. #define JSON_CATCH(exception) if(false)
  43. #endif
  44. // override exception macros
  45. #if defined(JSON_THROW_USER)
  46. #undef JSON_THROW
  47. #define JSON_THROW JSON_THROW_USER
  48. #endif
  49. #if defined(JSON_TRY_USER)
  50. #undef JSON_TRY
  51. #define JSON_TRY JSON_TRY_USER
  52. #endif
  53. #if defined(JSON_CATCH_USER)
  54. #undef JSON_CATCH
  55. #define JSON_CATCH JSON_CATCH_USER
  56. #endif
  57. // manual branch prediction
  58. #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
  59. #define JSON_LIKELY(x) __builtin_expect(!!(x), 1)
  60. #define JSON_UNLIKELY(x) __builtin_expect(!!(x), 0)
  61. #else
  62. #define JSON_LIKELY(x) x
  63. #define JSON_UNLIKELY(x) x
  64. #endif
  65. // C++ language standard detection
  66. #if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
  67. #define JSON_HAS_CPP_17
  68. #define JSON_HAS_CPP_14
  69. #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
  70. #define JSON_HAS_CPP_14
  71. #endif
  72. // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
  73. // may be removed in the future once the class is split.
  74. #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
  75. template<template<typename, typename, typename...> class ObjectType, \
  76. template<typename, typename...> class ArrayType, \
  77. class StringType, class BooleanType, class NumberIntegerType, \
  78. class NumberUnsignedType, class NumberFloatType, \
  79. template<typename> class AllocatorType, \
  80. template<typename, typename = void> class JSONSerializer>
  81. #define NLOHMANN_BASIC_JSON_TPL \
  82. basic_json<ObjectType, ArrayType, StringType, BooleanType, \
  83. NumberIntegerType, NumberUnsignedType, NumberFloatType, \
  84. AllocatorType, JSONSerializer>
  85. /*!
  86. @brief Helper to determine whether there's a key_type for T.
  87. This helper is used to tell associative containers apart from other containers
  88. such as sequence containers. For instance, `std::map` passes the test as it
  89. contains a `mapped_type`, whereas `std::vector` fails the test.
  90. @sa http://stackoverflow.com/a/7728728/266378
  91. @since version 1.0.0, overworked in version 2.0.6
  92. */
  93. #define NLOHMANN_JSON_HAS_HELPER(type) \
  94. template<typename T> struct has_##type { \
  95. private: \
  96. template<typename U, typename = typename U::type> \
  97. static int detect(U &&); \
  98. static void detect(...); \
  99. public: \
  100. static constexpr bool value = \
  101. std::is_integral<decltype(detect(std::declval<T>()))>::value; \
  102. }