config.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. // Copyright (c) 2019-2020 Krystian Stasiowski (sdkrystian at gmail dot com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/static_string
  9. //
  10. #ifndef BOOST_STATIC_STRING_CONFIG_HPP
  11. #define BOOST_STATIC_STRING_CONFIG_HPP
  12. // Are we dependent on Boost?
  13. // #define BOOST_STATIC_STRING_STANDALONE
  14. #include <cstdint>
  15. // detect 32/64 bit
  16. #if UINTPTR_MAX == UINT64_MAX
  17. #define BOOST_STATIC_STRING_ARCH 64
  18. #elif UINTPTR_MAX == UINT32_MAX
  19. #define BOOST_STATIC_STRING_ARCH 32
  20. #else
  21. #error Unknown or unsupported architecture, please open an issue
  22. #endif
  23. // Can we have deduction guides?
  24. #if __cpp_deduction_guides >= 201703L
  25. #define BOOST_STATIC_STRING_USE_DEDUCT
  26. #endif
  27. // Include <version> if we can
  28. #ifdef __has_include
  29. #if __has_include(<version>)
  30. #include <version>
  31. #endif
  32. #endif
  33. // Can we use __has_builtin?
  34. #ifdef __has_builtin
  35. #define BOOST_STATIC_STRING_HAS_BUILTIN(arg) __has_builtin(arg)
  36. #else
  37. #define BOOST_STATIC_STRING_HAS_BUILTIN(arg) 0
  38. #endif
  39. // Can we use is_constant_evaluated?
  40. #if __cpp_lib_is_constant_evaluated >= 201811L
  41. #define BOOST_STATIC_STRING_IS_CONST_EVAL std::is_constant_evaluated()
  42. #elif BOOST_STATIC_STRING_HAS_BUILTIN(__builtin_is_constant_evaluated)
  43. #define BOOST_STATIC_STRING_IS_CONST_EVAL __builtin_is_constant_evaluated()
  44. #endif
  45. // Check for an attribute
  46. #if defined(__has_cpp_attribute)
  47. #define BOOST_STATIC_STRING_CHECK_FOR_ATTR(x) __has_cpp_attribute(x)
  48. #elif defined(__has_attribute)
  49. #define BOOST_STATIC_STRING_CHECK_FOR_ATTR(x) __has_attribute(x)
  50. #else
  51. #define BOOST_STATIC_STRING_CHECK_FOR_ATTR(x) 0
  52. #endif
  53. // Decide which attributes we can use
  54. #define BOOST_STATIC_STRING_UNLIKELY
  55. #define BOOST_STATIC_STRING_NODISCARD
  56. #define BOOST_STATIC_STRING_NORETURN
  57. #define BOOST_STATIC_STRING_NO_NORETURN
  58. // unlikely
  59. #if BOOST_STATIC_STRING_CHECK_FOR_ATTR(unlikely)
  60. #undef BOOST_STATIC_STRING_UNLIKELY
  61. #define BOOST_STATIC_STRING_UNLIKELY [[unlikely]]
  62. #endif
  63. // nodiscard
  64. #if BOOST_STATIC_STRING_CHECK_FOR_ATTR(nodiscard)
  65. #undef BOOST_STATIC_STRING_NODISCARD
  66. #define BOOST_STATIC_STRING_NODISCARD [[nodiscard]]
  67. #elif defined(_MSC_VER) && _MSC_VER >= 1700
  68. #undef BOOST_STATIC_STRING_NODISCARD
  69. #define BOOST_STATIC_STRING_NODISCARD _Check_return_
  70. #elif defined(__GNUC__) || defined(__clang__)
  71. #undef BOOST_STATIC_STRING_NODISCARD
  72. #define BOOST_STATIC_STRING_NODISCARD __attribute__((warn_unused_result))
  73. #endif
  74. // noreturn
  75. #if BOOST_STATIC_STRING_CHECK_FOR_ATTR(noreturn)
  76. #undef BOOST_STATIC_STRING_NORETURN
  77. #undef BOOST_STATIC_STRING_NO_NORETURN
  78. #define BOOST_STATIC_STRING_NORETURN [[noreturn]]
  79. #elif defined(_MSC_VER)
  80. #undef BOOST_STATIC_STRING_NORETURN
  81. #undef BOOST_STATIC_STRING_NO_NORETURN
  82. #define BOOST_STATIC_STRING_NORETURN __declspec(noreturn)
  83. #elif defined(__GNUC__) || defined(__clang__)
  84. #undef BOOST_STATIC_STRING_NORETURN
  85. #undef BOOST_STATIC_STRING_NO_NORETURN
  86. #define BOOST_STATIC_STRING_NORETURN __attribute__((__noreturn__))
  87. #endif
  88. // _MSVC_LANG isn't avaliable until after VS2015
  89. #if defined(_MSC_VER) && _MSC_VER < 1910L
  90. // The constexpr support in this version is effectively that of
  91. // c++11, so we treat it as such
  92. #define BOOST_STATIC_STRING_STANDARD_VERSION 201103L
  93. #elif defined(_MSVC_LANG)
  94. // MSVC doesn't define __cplusplus by default
  95. #define BOOST_STATIC_STRING_STANDARD_VERSION _MSVC_LANG
  96. #else
  97. #define BOOST_STATIC_STRING_STANDARD_VERSION __cplusplus
  98. #endif
  99. // Decide what level of constexpr we can use
  100. #define BOOST_STATIC_STRING_CPP20_CONSTEXPR
  101. #define BOOST_STATIC_STRING_CPP17_CONSTEXPR
  102. #define BOOST_STATIC_STRING_CPP14_CONSTEXPR
  103. #define BOOST_STATIC_STRING_CPP11_CONSTEXPR
  104. #if BOOST_STATIC_STRING_STANDARD_VERSION >= 202002L
  105. #define BOOST_STATIC_STRING_CPP20
  106. #undef BOOST_STATIC_STRING_CPP20_CONSTEXPR
  107. #define BOOST_STATIC_STRING_CPP20_CONSTEXPR constexpr
  108. #endif
  109. #if BOOST_STATIC_STRING_STANDARD_VERSION >= 201703L
  110. #define BOOST_STATIC_STRING_CPP17
  111. #undef BOOST_STATIC_STRING_CPP17_CONSTEXPR
  112. #define BOOST_STATIC_STRING_CPP17_CONSTEXPR constexpr
  113. #endif
  114. #if BOOST_STATIC_STRING_STANDARD_VERSION >= 201402L
  115. #define BOOST_STATIC_STRING_CPP14
  116. #undef BOOST_STATIC_STRING_CPP14_CONSTEXPR
  117. #define BOOST_STATIC_STRING_CPP14_CONSTEXPR constexpr
  118. #endif
  119. #if BOOST_STATIC_STRING_STANDARD_VERSION >= 201103L
  120. #define BOOST_STATIC_STRING_CPP11
  121. #undef BOOST_STATIC_STRING_CPP11_CONSTEXPR
  122. #define BOOST_STATIC_STRING_CPP11_CONSTEXPR constexpr
  123. #endif
  124. // Boost and non-Boost versions of utilities
  125. #ifndef BOOST_STATIC_STRING_STANDALONE
  126. #ifndef BOOST_STATIC_STRING_THROW
  127. #define BOOST_STATIC_STRING_THROW(ex) BOOST_THROW_EXCEPTION(ex)
  128. #endif
  129. #ifndef BOOST_STATIC_STRING_ASSERT
  130. #define BOOST_STATIC_STRING_ASSERT(cond) BOOST_ASSERT(cond)
  131. #endif
  132. #else
  133. #ifndef BOOST_STATIC_STRING_THROW
  134. #define BOOST_STATIC_STRING_THROW(ex) throw ex
  135. #endif
  136. #ifndef BOOST_STATIC_STRING_ASSERT
  137. #define BOOST_STATIC_STRING_ASSERT(cond) assert(cond)
  138. #endif
  139. #endif
  140. #ifndef BOOST_STATIC_STRING_STANDALONE
  141. #include <boost/config.hpp>
  142. #include <boost/assert.hpp>
  143. #include <boost/container_hash/hash.hpp>
  144. #include <boost/utility/string_view.hpp>
  145. #include <boost/core/detail/string_view.hpp>
  146. #include <boost/throw_exception.hpp>
  147. #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW) || \
  148. defined(BOOST_STATIC_STRING_CXX17_STRING_VIEW)
  149. #include <string_view>
  150. #define BOOST_STATIC_STRING_HAS_STD_STRING_VIEW
  151. #endif
  152. #else
  153. #include <cassert>
  154. #include <stdexcept>
  155. #if defined(__has_include)
  156. # if !__has_include(<string_view>)
  157. # define BOOST_STATIC_STRING_NO_CXX17_HDR_STRING_VIEW
  158. # endif
  159. /*
  160. * Replicate the logic from Boost.Config
  161. */
  162. // GNU libstdc++3:
  163. #elif defined(__GLIBCPP__) || defined(__GLIBCXX__)
  164. # if ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 70100) || (__cplusplus <= 201402L)
  165. # define BOOST_STATIC_STRING_NO_CXX17_HDR_STRING_VIEW
  166. # endif
  167. // libc++:
  168. #elif defined(_LIBCPP_VERSION)
  169. # if (_LIBCPP_VERSION < 4000) || (__cplusplus <= 201402L)
  170. # define BOOST_STATIC_STRING_NO_CXX17_HDR_STRING_VIEW
  171. # endif
  172. // MSVC uses logic from catch all for BOOST_NO_CXX17_HDR_STRING_VIEW
  173. // catch all:
  174. #elif !defined(_YVALS) && !defined(_CPPLIB_VER)
  175. # if (!defined(__has_include) || (__cplusplus < 201700))
  176. # define BOOST_STATIC_STRING_NO_CXX17_HDR_STRING_VIEW
  177. # elif !__has_include(<string_view>)
  178. # define BOOST_STATIC_STRING_NO_CXX17_HDR_STRING_VIEW
  179. # endif
  180. #endif
  181. #if !defined(BOOST_STATIC_STRING_NO_CXX17_HDR_STRING_VIEW) || \
  182. defined(BOOST_STATIC_STRING_CXX17_STRING_VIEW)
  183. #include <string_view>
  184. #define BOOST_STATIC_STRING_HAS_STD_STRING_VIEW
  185. #endif
  186. #endif
  187. // Compiler bug prevents constexpr from working with clang 4.x and 5.x
  188. // if it is detected, we disable constexpr.
  189. #if (BOOST_STATIC_STRING_STANDARD_VERSION >= 201402L && \
  190. BOOST_STATIC_STRING_STANDARD_VERSION < 201703L) && \
  191. defined(__clang__) && \
  192. ((__clang_major__ == 4) || (__clang_major__ == 5))
  193. // This directive works on clang
  194. #warning "C++14 constexpr is not supported in clang 4.x and 5.x due to a compiler bug."
  195. #ifdef BOOST_STATIC_STRING_CPP14
  196. #undef BOOST_STATIC_STRING_CPP14
  197. #endif
  198. #undef BOOST_STATIC_STRING_CPP14_CONSTEXPR
  199. #define BOOST_STATIC_STRING_CPP14_CONSTEXPR
  200. #endif
  201. // This is for compiler/library configurations
  202. // that cannot use the library comparison function
  203. // objects at all in constant expresssions. In these
  204. // cases, we use whatever will make more constexpr work.
  205. #if defined(__clang__) && \
  206. (defined(__GLIBCXX__) || defined(_MSC_VER))
  207. #define BOOST_STATIC_STRING_NO_PTR_COMP_FUNCTIONS
  208. #endif
  209. // In gcc-5, we cannot use throw expressions in a
  210. // constexpr function. However, we have a workaround
  211. // for this using constructors. Also, non-static member
  212. // functions that return the class they are a member of
  213. // causes an ICE during constant evaluation.
  214. #if defined(__GNUC__) && (__GNUC__== 5) && \
  215. defined(BOOST_STATIC_STRING_CPP14)
  216. #define BOOST_STATIC_STRING_GCC5_BAD_CONSTEXPR
  217. #endif
  218. #ifndef BOOST_STATIC_STRING_STANDALONE
  219. #if ! defined(BOOST_NO_CWCHAR) && ! defined(BOOST_NO_SWPRINTF)
  220. #define BOOST_STATIC_STRING_HAS_WCHAR
  221. #endif
  222. #else
  223. #ifndef __has_include
  224. // If we don't have __has_include in standalone,
  225. // we will assume that <cwchar> exists.
  226. #define BOOST_STATIC_STRING_HAS_WCHAR
  227. #elif __has_include(<cwchar>)
  228. #define BOOST_STATIC_STRING_HAS_WCHAR
  229. #endif
  230. #endif
  231. #ifdef BOOST_STATIC_STRING_HAS_WCHAR
  232. #include <cwchar>
  233. #endif
  234. // Define the basic string_view type used by the library
  235. // Conversions to and from other available string_view types
  236. // are still defined.
  237. #if !defined(BOOST_STATIC_STRING_STANDALONE) || \
  238. defined(BOOST_STATIC_STRING_HAS_STD_STRING_VIEW)
  239. #define BOOST_STATIC_STRING_HAS_ANY_STRING_VIEW
  240. namespace boost {
  241. namespace static_strings {
  242. /// The type of `basic_string_view` used by the library
  243. template<typename CharT, typename Traits>
  244. using basic_string_view =
  245. #ifndef BOOST_STATIC_STRING_STANDALONE
  246. boost::basic_string_view<CharT, Traits>;
  247. #else
  248. std::basic_string_view<CharT, Traits>;
  249. #endif
  250. } // static_strings
  251. } // boost
  252. #endif
  253. #if defined(__cpp_lib_to_string) && __cpp_lib_to_string >= 202306L // std::to_[w]string() redefined in terms of std::format()
  254. #define BOOST_STATIC_STRING_USE_STD_FORMAT
  255. #endif
  256. #endif