config.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/json
  8. //
  9. #ifndef BOOST_JSON_DETAIL_CONFIG_HPP
  10. #define BOOST_JSON_DETAIL_CONFIG_HPP
  11. #include <boost/config.hpp>
  12. #include <boost/assert.hpp>
  13. #include <boost/static_assert.hpp>
  14. #include <boost/throw_exception.hpp>
  15. #include <cstdint>
  16. #include <type_traits>
  17. #include <utility>
  18. // detect 32/64 bit
  19. #if UINTPTR_MAX == UINT64_MAX
  20. # define BOOST_JSON_ARCH 64
  21. #elif UINTPTR_MAX == UINT32_MAX
  22. # define BOOST_JSON_ARCH 32
  23. #else
  24. # error Unknown or unsupported architecture, please open an issue
  25. #endif
  26. #ifndef BOOST_JSON_REQUIRE_CONST_INIT
  27. # define BOOST_JSON_REQUIRE_CONST_INIT
  28. # if __cpp_constinit >= 201907L
  29. # undef BOOST_JSON_REQUIRE_CONST_INIT
  30. # define BOOST_JSON_REQUIRE_CONST_INIT constinit
  31. # elif defined(__clang__) && defined(__has_cpp_attribute)
  32. # if __has_cpp_attribute(clang::require_constant_initialization)
  33. # undef BOOST_JSON_REQUIRE_CONST_INIT
  34. # define BOOST_JSON_REQUIRE_CONST_INIT [[clang::require_constant_initialization]]
  35. # endif
  36. # endif
  37. #endif
  38. #ifndef BOOST_JSON_NO_DESTROY
  39. # if defined(__clang__) && defined(__has_cpp_attribute)
  40. # if __has_cpp_attribute(clang::no_destroy)
  41. # define BOOST_JSON_NO_DESTROY [[clang::no_destroy]]
  42. # endif
  43. # endif
  44. #endif
  45. #if ! defined(BOOST_JSON_NO_SSE2) && \
  46. ! defined(BOOST_JSON_USE_SSE2)
  47. # if (defined(_M_IX86) && _M_IX86_FP == 2) || \
  48. defined(_M_X64) || defined(__SSE2__)
  49. # define BOOST_JSON_USE_SSE2
  50. # endif
  51. #endif
  52. #if defined(BOOST_JSON_DOCS)
  53. # define BOOST_JSON_DECL
  54. #else
  55. # if (defined(BOOST_JSON_DYN_LINK) || defined(BOOST_ALL_DYN_LINK)) && !defined(BOOST_JSON_STATIC_LINK)
  56. # if defined(BOOST_JSON_SOURCE)
  57. # define BOOST_JSON_DECL BOOST_SYMBOL_EXPORT
  58. # else
  59. # define BOOST_JSON_DECL BOOST_SYMBOL_IMPORT
  60. # endif
  61. # endif // shared lib
  62. # ifndef BOOST_JSON_DECL
  63. # define BOOST_JSON_DECL
  64. # endif
  65. # if !defined(BOOST_JSON_SOURCE) && !defined(BOOST_ALL_NO_LIB) && !defined(BOOST_JSON_NO_LIB)
  66. # define BOOST_LIB_NAME boost_json
  67. # if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_JSON_DYN_LINK)
  68. # define BOOST_DYN_LINK
  69. # endif
  70. # include <boost/config/auto_link.hpp>
  71. # endif
  72. #endif
  73. #ifndef BOOST_JSON_LIKELY
  74. # define BOOST_JSON_LIKELY(x) BOOST_LIKELY( !!(x) )
  75. #endif
  76. #ifndef BOOST_JSON_UNLIKELY
  77. # define BOOST_JSON_UNLIKELY(x) BOOST_UNLIKELY( !!(x) )
  78. #endif
  79. #ifndef BOOST_JSON_UNREACHABLE
  80. # ifdef _MSC_VER
  81. # define BOOST_JSON_UNREACHABLE() __assume(0)
  82. # elif defined(__GNUC__) || defined(__clang__)
  83. # define BOOST_JSON_UNREACHABLE() __builtin_unreachable()
  84. # elif defined(__has_builtin)
  85. # if __has_builtin(__builtin_unreachable)
  86. # define BOOST_JSON_UNREACHABLE() __builtin_unreachable()
  87. # endif
  88. # else
  89. # define BOOST_JSON_UNREACHABLE() static_cast<void>(0)
  90. # endif
  91. #endif
  92. #ifndef BOOST_JSON_ASSUME
  93. # define BOOST_JSON_ASSUME(x) (!!(x) ? void() : BOOST_JSON_UNREACHABLE())
  94. # ifdef _MSC_VER
  95. # undef BOOST_JSON_ASSUME
  96. # define BOOST_JSON_ASSUME(x) __assume(!!(x))
  97. # elif defined(__has_builtin)
  98. # if __has_builtin(__builtin_assume)
  99. # undef BOOST_JSON_ASSUME
  100. # define BOOST_JSON_ASSUME(x) __builtin_assume(!!(x))
  101. # endif
  102. # endif
  103. #endif
  104. // older versions of msvc and clang don't always
  105. // constant initialize when they are supposed to
  106. #ifndef BOOST_JSON_WEAK_CONSTINIT
  107. # if defined(_MSC_VER) && ! defined(__clang__) && _MSC_VER < 1920
  108. # define BOOST_JSON_WEAK_CONSTINIT
  109. # elif defined(__clang__) && __clang_major__ < 4
  110. # define BOOST_JSON_WEAK_CONSTINIT
  111. # endif
  112. #endif
  113. // These macros are private, for tests, do not change
  114. // them or else previously built libraries won't match.
  115. #ifndef BOOST_JSON_MAX_STRING_SIZE
  116. # define BOOST_JSON_NO_MAX_STRING_SIZE
  117. # define BOOST_JSON_MAX_STRING_SIZE 0x7ffffffe
  118. #endif
  119. #ifndef BOOST_JSON_MAX_STRUCTURED_SIZE
  120. # define BOOST_JSON_NO_MAX_STRUCTURED_SIZE
  121. # define BOOST_JSON_MAX_STRUCTURED_SIZE 0x7ffffffe
  122. #endif
  123. #ifndef BOOST_JSON_STACK_BUFFER_SIZE
  124. # define BOOST_JSON_NO_STACK_BUFFER_SIZE
  125. # if defined(__i386__) || defined(__x86_64__) || \
  126. defined(_M_IX86) || defined(_M_X64)
  127. # define BOOST_JSON_STACK_BUFFER_SIZE 4096
  128. # else
  129. // If we are not on Intel, then assume we are on
  130. // embedded and use a smaller stack size. If this
  131. // is not suitable, the user can define the macro
  132. // themselves when building the library or including
  133. // src.hpp.
  134. # define BOOST_JSON_STACK_BUFFER_SIZE 256
  135. # endif
  136. #endif
  137. #if defined(__cpp_constinit) && __cpp_constinit >= 201907L
  138. # define BOOST_JSON_CONSTINIT constinit
  139. #elif defined(__has_cpp_attribute) && defined(__clang__)
  140. # if __has_cpp_attribute(clang::require_constant_initialization)
  141. # define BOOST_JSON_CONSTINIT [[clang::require_constant_initialization]]
  142. # endif
  143. #elif defined(__GNUC__) && (__GNUC__ >= 10)
  144. # define BOOST_JSON_CONSTINIT __constinit
  145. #endif
  146. #ifndef BOOST_JSON_CONSTINIT
  147. # define BOOST_JSON_CONSTINIT
  148. #endif
  149. namespace boost {
  150. namespace json {
  151. namespace detail {
  152. template<class...>
  153. struct make_void
  154. {
  155. using type =void;
  156. };
  157. template<class... Ts>
  158. using void_t = typename
  159. make_void<Ts...>::type;
  160. template<class T>
  161. using remove_cvref = typename
  162. std::remove_cv<typename
  163. std::remove_reference<T>::type>::type;
  164. template<class T, class U>
  165. T exchange(T& t, U u) noexcept
  166. {
  167. T v = std::move(t);
  168. t = std::move(u);
  169. return v;
  170. }
  171. /* This is a derivative work, original copyright:
  172. Copyright Eric Niebler 2013-present
  173. Use, modification and distribution is subject to the
  174. Boost Software License, Version 1.0. (See accompanying
  175. file LICENSE_1_0.txt or copy at
  176. http://www.boost.org/LICENSE_1_0.txt)
  177. Project home: https://github.com/ericniebler/range-v3
  178. */
  179. template<typename T>
  180. struct static_const
  181. {
  182. static constexpr T value {};
  183. };
  184. template<typename T>
  185. constexpr T static_const<T>::value;
  186. #define BOOST_JSON_INLINE_VARIABLE(name, type) \
  187. namespace { constexpr auto& name = \
  188. ::boost::json::detail::static_const<type>::value; \
  189. } struct _unused_ ## name ## _semicolon_bait_
  190. } // detail
  191. } // namespace json
  192. } // namespace boost
  193. #endif