macro_scope.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include <utility> // pair
  3. #include <nlohmann/thirdparty/hedley/hedley.hpp>
  4. // This file contains all internal macro definitions
  5. // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
  6. // exclude unsupported compilers
  7. #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
  8. #if defined(__clang__)
  9. #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
  10. #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
  11. #endif
  12. #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
  13. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
  14. #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
  15. #endif
  16. #endif
  17. #endif
  18. // C++ language standard detection
  19. #if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
  20. #define JSON_HAS_CPP_17
  21. #define JSON_HAS_CPP_14
  22. #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
  23. #define JSON_HAS_CPP_14
  24. #endif
  25. // disable float-equal warnings on GCC/clang
  26. #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
  27. #pragma GCC diagnostic push
  28. #pragma GCC diagnostic ignored "-Wfloat-equal"
  29. #endif
  30. // disable documentation warnings on clang
  31. #if defined(__clang__)
  32. #pragma GCC diagnostic push
  33. #pragma GCC diagnostic ignored "-Wdocumentation"
  34. #endif
  35. // allow to disable exceptions
  36. #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
  37. #define JSON_THROW(exception) throw exception
  38. #define JSON_TRY try
  39. #define JSON_CATCH(exception) catch(exception)
  40. #define JSON_INTERNAL_CATCH(exception) catch(exception)
  41. #else
  42. #include <cstdlib>
  43. #define JSON_THROW(exception) std::abort()
  44. #define JSON_TRY if(true)
  45. #define JSON_CATCH(exception) if(false)
  46. #define JSON_INTERNAL_CATCH(exception) if(false)
  47. #endif
  48. // override exception macros
  49. #if defined(JSON_THROW_USER)
  50. #undef JSON_THROW
  51. #define JSON_THROW JSON_THROW_USER
  52. #endif
  53. #if defined(JSON_TRY_USER)
  54. #undef JSON_TRY
  55. #define JSON_TRY JSON_TRY_USER
  56. #endif
  57. #if defined(JSON_CATCH_USER)
  58. #undef JSON_CATCH
  59. #define JSON_CATCH JSON_CATCH_USER
  60. #undef JSON_INTERNAL_CATCH
  61. #define JSON_INTERNAL_CATCH JSON_CATCH_USER
  62. #endif
  63. #if defined(JSON_INTERNAL_CATCH_USER)
  64. #undef JSON_INTERNAL_CATCH
  65. #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
  66. #endif
  67. /*!
  68. @brief macro to briefly define a mapping between an enum and JSON
  69. @def NLOHMANN_JSON_SERIALIZE_ENUM
  70. @since version 3.4.0
  71. */
  72. #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
  73. template<typename BasicJsonType> \
  74. inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
  75. { \
  76. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  77. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  78. auto it = std::find_if(std::begin(m), std::end(m), \
  79. [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  80. { \
  81. return ej_pair.first == e; \
  82. }); \
  83. j = ((it != std::end(m)) ? it : std::begin(m))->second; \
  84. } \
  85. template<typename BasicJsonType> \
  86. inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
  87. { \
  88. static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
  89. static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
  90. auto it = std::find_if(std::begin(m), std::end(m), \
  91. [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
  92. { \
  93. return ej_pair.second == j; \
  94. }); \
  95. e = ((it != std::end(m)) ? it : std::begin(m))->first; \
  96. }
  97. // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
  98. // may be removed in the future once the class is split.
  99. #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
  100. template<template<typename, typename, typename...> class ObjectType, \
  101. template<typename, typename...> class ArrayType, \
  102. class StringType, class BooleanType, class NumberIntegerType, \
  103. class NumberUnsignedType, class NumberFloatType, \
  104. template<typename> class AllocatorType, \
  105. template<typename, typename = void> class JSONSerializer, \
  106. class BinaryType>
  107. #define NLOHMANN_BASIC_JSON_TPL \
  108. basic_json<ObjectType, ArrayType, StringType, BooleanType, \
  109. NumberIntegerType, NumberUnsignedType, NumberFloatType, \
  110. AllocatorType, JSONSerializer, BinaryType>