config.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  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/throw_exception.hpp>
  14. #include <cstdint>
  15. #include <type_traits>
  16. #include <utility>
  17. // detect 32/64 bit
  18. #if UINTPTR_MAX == UINT64_MAX
  19. # define BOOST_JSON_ARCH 64
  20. #elif UINTPTR_MAX == UINT32_MAX
  21. # define BOOST_JSON_ARCH 32
  22. #else
  23. # error Unknown or unsupported architecture, please open an issue
  24. #endif
  25. // VFALCO Copied from Boost.Config
  26. // This is a derivative work.
  27. #ifndef BOOST_JSON_NODISCARD
  28. # ifdef __has_cpp_attribute
  29. // clang-6 accepts [[nodiscard]] with -std=c++14, but warns about it -pedantic
  30. # if __has_cpp_attribute(nodiscard) && !(defined(__clang__) && (__cplusplus < 201703L))
  31. # define BOOST_JSON_NODISCARD [[nodiscard]]
  32. # else
  33. # define BOOST_JSON_NODISCARD
  34. # endif
  35. # else
  36. # define BOOST_JSON_NODISCARD
  37. # endif
  38. #endif
  39. #ifndef BOOST_JSON_REQUIRE_CONST_INIT
  40. # define BOOST_JSON_REQUIRE_CONST_INIT
  41. # if __cpp_constinit >= 201907L
  42. # undef BOOST_JSON_REQUIRE_CONST_INIT
  43. # define BOOST_JSON_REQUIRE_CONST_INIT constinit
  44. # elif defined(__clang__) && defined(__has_cpp_attribute)
  45. # if __has_cpp_attribute(clang::require_constant_initialization)
  46. # undef BOOST_JSON_REQUIRE_CONST_INIT
  47. # define BOOST_JSON_REQUIRE_CONST_INIT [[clang::require_constant_initialization]]
  48. # endif
  49. # endif
  50. #endif
  51. #ifndef BOOST_JSON_NO_DESTROY
  52. # if defined(__clang__) && defined(__has_cpp_attribute)
  53. # if __has_cpp_attribute(clang::no_destroy)
  54. # define BOOST_JSON_NO_DESTROY [[clang::no_destroy]]
  55. # endif
  56. # endif
  57. #endif
  58. // BOOST_NORETURN ---------------------------------------------//
  59. // Macro to use before a function declaration/definition to designate
  60. // the function as not returning normally (i.e. with a return statement
  61. // or by leaving the function scope, if the function return type is void).
  62. #if !defined(BOOST_NORETURN)
  63. # if defined(_MSC_VER)
  64. # define BOOST_NORETURN __declspec(noreturn)
  65. # elif defined(__GNUC__)
  66. # define BOOST_NORETURN __attribute__ ((__noreturn__))
  67. # elif defined(__has_attribute) && defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x5130)
  68. # if __has_attribute(noreturn)
  69. # define BOOST_NORETURN [[noreturn]]
  70. # endif
  71. # elif defined(__has_cpp_attribute)
  72. # if __has_cpp_attribute(noreturn)
  73. # define BOOST_NORETURN [[noreturn]]
  74. # endif
  75. # endif
  76. #endif
  77. #ifndef BOOST_ASSERT
  78. #define BOOST_ASSERT assert
  79. #endif
  80. #ifndef BOOST_STATIC_ASSERT
  81. #define BOOST_STATIC_ASSERT( ... ) static_assert(__VA_ARGS__, #__VA_ARGS__)
  82. #endif
  83. #ifndef BOOST_FALLTHROUGH
  84. #define BOOST_FALLTHROUGH [[fallthrough]]
  85. #endif
  86. #ifndef BOOST_FORCEINLINE
  87. # ifdef _MSC_VER
  88. # define BOOST_FORCEINLINE __forceinline
  89. # elif defined(__GNUC__) || defined(__clang__)
  90. # define BOOST_FORCEINLINE inline __attribute__((always_inline))
  91. # else
  92. # define BOOST_FORCEINLINE inline
  93. # endif
  94. #endif
  95. #ifndef BOOST_NOINLINE
  96. # ifdef _MSC_VER
  97. # define BOOST_NOINLINE __declspec(noinline)
  98. # elif defined(__GNUC__) || defined(__clang__)
  99. # define BOOST_NOINLINE __attribute__((noinline))
  100. # else
  101. # define BOOST_NOINLINE
  102. # endif
  103. #endif
  104. #ifndef BOOST_THROW_EXCEPTION
  105. # ifndef BOOST_NO_EXCEPTIONS
  106. # define BOOST_THROW_EXCEPTION(x) throw(x)
  107. # else
  108. # define BOOST_THROW_EXCEPTION(x) do{}while(0)
  109. # endif
  110. #endif
  111. #if ! defined(BOOST_JSON_NO_SSE2) && \
  112. ! defined(BOOST_JSON_USE_SSE2)
  113. # if (defined(_M_IX86) && _M_IX86_FP == 2) || \
  114. defined(_M_X64) || defined(__SSE2__)
  115. # define BOOST_JSON_USE_SSE2
  116. # endif
  117. #endif
  118. #ifndef BOOST_SYMBOL_VISIBLE
  119. #define BOOST_SYMBOL_VISIBLE
  120. #endif
  121. #if defined(BOOST_JSON_DOCS)
  122. # define BOOST_JSON_DECL
  123. #else
  124. # if (defined(BOOST_JSON_DYN_LINK) || defined(BOOST_ALL_DYN_LINK)) && !defined(BOOST_JSON_STATIC_LINK)
  125. # if defined(BOOST_JSON_SOURCE)
  126. # define BOOST_JSON_DECL BOOST_SYMBOL_EXPORT
  127. # else
  128. # define BOOST_JSON_DECL BOOST_SYMBOL_IMPORT
  129. # endif
  130. # endif // shared lib
  131. # ifndef BOOST_JSON_DECL
  132. # define BOOST_JSON_DECL
  133. # endif
  134. # if !defined(BOOST_JSON_SOURCE) && !defined(BOOST_ALL_NO_LIB) && !defined(BOOST_JSON_NO_LIB)
  135. # define BOOST_LIB_NAME boost_json
  136. # if defined(BOOST_ALL_DYN_LINK) || defined(BOOST_JSON_DYN_LINK)
  137. # define BOOST_DYN_LINK
  138. # endif
  139. # include <boost/config/auto_link.hpp>
  140. # endif
  141. #endif
  142. #ifndef BOOST_JSON_LIKELY
  143. # if defined(__GNUC__) || defined(__clang__)
  144. # define BOOST_JSON_LIKELY(x) __builtin_expect(!!(x), 1)
  145. # else
  146. # define BOOST_JSON_LIKELY(x) x
  147. # endif
  148. #endif
  149. #ifndef BOOST_JSON_UNLIKELY
  150. # if defined(__GNUC__) || defined(__clang__)
  151. # define BOOST_JSON_UNLIKELY(x) __builtin_expect(!!(x), 0)
  152. # else
  153. # define BOOST_JSON_UNLIKELY(x) x
  154. # endif
  155. #endif
  156. #ifndef BOOST_JSON_UNREACHABLE
  157. # ifdef _MSC_VER
  158. # define BOOST_JSON_UNREACHABLE() __assume(0)
  159. # elif defined(__GNUC__) || defined(__clang__)
  160. # define BOOST_JSON_UNREACHABLE() __builtin_unreachable()
  161. # elif defined(__has_builtin)
  162. # if __has_builtin(__builtin_unreachable)
  163. # define BOOST_JSON_UNREACHABLE() __builtin_unreachable()
  164. # endif
  165. # else
  166. # define BOOST_JSON_UNREACHABLE() static_cast<void>(0)
  167. # endif
  168. #endif
  169. #ifndef BOOST_JSON_ASSUME
  170. # define BOOST_JSON_ASSUME(x) (!!(x) ? void() : BOOST_JSON_UNREACHABLE())
  171. # ifdef _MSC_VER
  172. # undef BOOST_JSON_ASSUME
  173. # define BOOST_JSON_ASSUME(x) __assume(!!(x))
  174. # elif defined(__has_builtin)
  175. # if __has_builtin(__builtin_assume)
  176. # undef BOOST_JSON_ASSUME
  177. # define BOOST_JSON_ASSUME(x) __builtin_assume(!!(x))
  178. # endif
  179. # endif
  180. #endif
  181. // older versions of msvc and clang don't always
  182. // constant initialize when they are supposed to
  183. #ifndef BOOST_JSON_WEAK_CONSTINIT
  184. # if defined(_MSC_VER) && ! defined(__clang__) && _MSC_VER < 1920
  185. # define BOOST_JSON_WEAK_CONSTINIT
  186. # elif defined(__clang__) && __clang_major__ < 4
  187. # define BOOST_JSON_WEAK_CONSTINIT
  188. # endif
  189. #endif
  190. // These macros are private, for tests, do not change
  191. // them or else previously built libraries won't match.
  192. #ifndef BOOST_JSON_MAX_STRING_SIZE
  193. # define BOOST_JSON_NO_MAX_STRING_SIZE
  194. # define BOOST_JSON_MAX_STRING_SIZE 0x7ffffffe
  195. #endif
  196. #ifndef BOOST_JSON_MAX_STRUCTURED_SIZE
  197. # define BOOST_JSON_NO_MAX_STRUCTURED_SIZE
  198. # define BOOST_JSON_MAX_STRUCTURED_SIZE 0x7ffffffe
  199. #endif
  200. #ifndef BOOST_JSON_STACK_BUFFER_SIZE
  201. # define BOOST_JSON_NO_STACK_BUFFER_SIZE
  202. # if defined(__i386__) || defined(__x86_64__) || \
  203. defined(_M_IX86) || defined(_M_X64)
  204. # define BOOST_JSON_STACK_BUFFER_SIZE 4096
  205. # else
  206. // If we are not on Intel, then assume we are on
  207. // embedded and use a smaller stack size. If this
  208. // is not suitable, the user can define the macro
  209. // themselves when building the library or including
  210. // src.hpp.
  211. # define BOOST_JSON_STACK_BUFFER_SIZE 256
  212. # endif
  213. #endif
  214. #if ! defined(BOOST_JSON_BIG_ENDIAN) && ! defined(BOOST_JSON_LITTLE_ENDIAN)
  215. // Copied from Boost.Endian
  216. # if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  217. # define BOOST_JSON_LITTLE_ENDIAN
  218. # elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  219. # define BOOST_JSON_BIG_ENDIAN
  220. # elif defined(__LITTLE_ENDIAN__)
  221. # define BOOST_JSON_LITTLE_ENDIAN
  222. # elif defined(__BIG_ENDIAN__)
  223. # define BOOST_JSON_BIG_ENDIAN
  224. # elif defined(_MSC_VER) || defined(__i386__) || defined(__x86_64__)
  225. # define BOOST_JSON_LITTLE_ENDIAN
  226. # else
  227. # error The Boost.JSON library could not determine the endianness of this platform. Define either BOOST_JSON_BIG_ENDIAN or BOOST_JSON_LITTLE_ENDIAN.
  228. # endif
  229. #endif
  230. #if defined(__cpp_constinit) && __cpp_constinit >= 201907L
  231. # define BOOST_JSON_CONSTINIT constinit
  232. #elif defined(__has_cpp_attribute) && defined(__clang__)
  233. # if __has_cpp_attribute(clang::require_constant_initialization)
  234. # define BOOST_JSON_CONSTINIT [[clang::require_constant_initialization]]
  235. # endif
  236. #elif defined(__GNUC__) && (__GNUC__ >= 10)
  237. # define BOOST_JSON_CONSTINIT __constinit
  238. #endif
  239. #ifndef BOOST_JSON_CONSTINIT
  240. # define BOOST_JSON_CONSTINIT
  241. #endif
  242. namespace boost {
  243. namespace json {
  244. namespace detail {
  245. template<class...>
  246. struct make_void
  247. {
  248. using type =void;
  249. };
  250. template<class... Ts>
  251. using void_t = typename
  252. make_void<Ts...>::type;
  253. template<class T>
  254. using remove_cvref = typename
  255. std::remove_cv<typename
  256. std::remove_reference<T>::type>::type;
  257. template<class T, class U>
  258. T exchange(T& t, U u) noexcept
  259. {
  260. T v = std::move(t);
  261. t = std::move(u);
  262. return v;
  263. }
  264. /* This is a derivative work, original copyright:
  265. Copyright Eric Niebler 2013-present
  266. Use, modification and distribution is subject to the
  267. Boost Software License, Version 1.0. (See accompanying
  268. file LICENSE_1_0.txt or copy at
  269. http://www.boost.org/LICENSE_1_0.txt)
  270. Project home: https://github.com/ericniebler/range-v3
  271. */
  272. template<typename T>
  273. struct static_const
  274. {
  275. static constexpr T value {};
  276. };
  277. template<typename T>
  278. constexpr T static_const<T>::value;
  279. #define BOOST_JSON_INLINE_VARIABLE(name, type) \
  280. namespace { constexpr auto& name = \
  281. ::boost::json::detail::static_const<type>::value; \
  282. } struct _unused_ ## name ## _semicolon_bait_
  283. } // detail
  284. } // namespace json
  285. } // namespace boost
  286. #endif