config.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_STATIC_ASSERT
  130. #define BOOST_STATIC_STRING_STATIC_ASSERT(cond, msg) BOOST_STATIC_ASSERT_MSG(cond, msg)
  131. #endif
  132. #ifndef BOOST_STATIC_STRING_ASSERT
  133. #define BOOST_STATIC_STRING_ASSERT(cond) BOOST_ASSERT(cond)
  134. #endif
  135. #else
  136. #ifndef BOOST_STATIC_STRING_THROW
  137. #define BOOST_STATIC_STRING_THROW(ex) throw ex
  138. #endif
  139. #ifndef BOOST_STATIC_STRING_STATIC_ASSERT
  140. #define BOOST_STATIC_STRING_STATIC_ASSERT(cond, msg) static_assert(cond, msg)
  141. #endif
  142. #ifndef BOOST_STATIC_STRING_ASSERT
  143. #define BOOST_STATIC_STRING_ASSERT(cond) assert(cond)
  144. #endif
  145. #endif
  146. #ifndef BOOST_STATIC_STRING_STANDALONE
  147. #include <boost/config.hpp>
  148. #include <boost/assert.hpp>
  149. #include <boost/container_hash/hash.hpp>
  150. #include <boost/static_assert.hpp>
  151. #include <boost/utility/string_view.hpp>
  152. #include <boost/core/detail/string_view.hpp>
  153. #include <boost/throw_exception.hpp>
  154. #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW) || \
  155. defined(BOOST_STATIC_STRING_CXX17_STRING_VIEW)
  156. #include <string_view>
  157. #define BOOST_STATIC_STRING_HAS_STD_STRING_VIEW
  158. #endif
  159. #else
  160. #include <cassert>
  161. #include <stdexcept>
  162. /*
  163. * Replicate the logic from Boost.Config
  164. */
  165. // GNU libstdc++3:
  166. #if defined(__GLIBCPP__) || defined(__GLIBCXX__)
  167. #if (BOOST_LIBSTDCXX_VERSION < 70100) || (__cplusplus <= 201402L)
  168. # define BOOST_STATIC_STRING_NO_CXX17_HDR_STRING_VIEW
  169. #endif
  170. // libc++:
  171. #elif defined(_LIBCPP_VERSION)
  172. #if (_LIBCPP_VERSION < 4000) || (__cplusplus <= 201402L)
  173. # define BOOST_STATIC_STRING_NO_CXX17_HDR_STRING_VIEW
  174. #endif
  175. // MSVC uses logic from catch all for BOOST_NO_CXX17_HDR_STRING_VIEW
  176. // catch all:
  177. #elif !defined(_YVALS) && !defined(_CPPLIB_VER)
  178. #if (!defined(__has_include) || (__cplusplus < 201700))
  179. # define BOOST_STATIC_STRING_NO_CXX17_HDR_STRING_VIEW
  180. #elif !__has_include(<string_view>)
  181. # define BOOST_STATIC_STRING_NO_CXX17_HDR_STRING_VIEW
  182. #endif
  183. #endif
  184. #if !defined(BOOST_STATIC_STRING_NO_CXX17_HDR_STRING_VIEW) || \
  185. defined(BOOST_STATIC_STRING_CXX17_STRING_VIEW)
  186. #include <string_view>
  187. #define BOOST_STATIC_STRING_HAS_STD_STRING_VIEW
  188. #endif
  189. #endif
  190. // Compiler bug prevents constexpr from working with clang 4.x and 5.x
  191. // if it is detected, we disable constexpr.
  192. #if (BOOST_STATIC_STRING_STANDARD_VERSION >= 201402L && \
  193. BOOST_STATIC_STRING_STANDARD_VERSION < 201703L) && \
  194. defined(__clang__) && \
  195. ((__clang_major__ == 4) || (__clang_major__ == 5))
  196. // This directive works on clang
  197. #warning "C++14 constexpr is not supported in clang 4.x and 5.x due to a compiler bug."
  198. #ifdef BOOST_STATIC_STRING_CPP14
  199. #undef BOOST_STATIC_STRING_CPP14
  200. #endif
  201. #undef BOOST_STATIC_STRING_CPP14_CONSTEXPR
  202. #define BOOST_STATIC_STRING_CPP14_CONSTEXPR
  203. #endif
  204. // This is for compiler/library configurations
  205. // that cannot use the library comparison function
  206. // objects at all in constant expresssions. In these
  207. // cases, we use whatever will make more constexpr work.
  208. #if defined(__clang__) && \
  209. (defined(__GLIBCXX__) || defined(_MSC_VER))
  210. #define BOOST_STATIC_STRING_NO_PTR_COMP_FUNCTIONS
  211. #endif
  212. // In gcc-5, we cannot use throw expressions in a
  213. // constexpr function. However, we have a workaround
  214. // for this using constructors. Also, non-static member
  215. // functions that return the class they are a member of
  216. // causes an ICE during constant evaluation.
  217. #if defined(__GNUC__) && (__GNUC__== 5) && \
  218. defined(BOOST_STATIC_STRING_CPP14)
  219. #define BOOST_STATIC_STRING_GCC5_BAD_CONSTEXPR
  220. #endif
  221. #ifndef BOOST_STATIC_STRING_STANDALONE
  222. #if ! defined(BOOST_NO_CWCHAR) && ! defined(BOOST_NO_SWPRINTF)
  223. #define BOOST_STATIC_STRING_HAS_WCHAR
  224. #endif
  225. #else
  226. #ifndef __has_include
  227. // If we don't have __has_include in standalone,
  228. // we will assume that <cwchar> exists.
  229. #define BOOST_STATIC_STRING_HAS_WCHAR
  230. #elif __has_include(<cwchar>)
  231. #define BOOST_STATIC_STRING_HAS_WCHAR
  232. #endif
  233. #endif
  234. #ifdef BOOST_STATIC_STRING_HAS_WCHAR
  235. #include <cwchar>
  236. #endif
  237. // Define the basic string_view type used by the library
  238. // Conversions to and from other available string_view types
  239. // are still defined.
  240. #if !defined(BOOST_STATIC_STRING_STANDALONE) || \
  241. defined(BOOST_STATIC_STRING_HAS_STD_STRING_VIEW)
  242. #define BOOST_STATIC_STRING_HAS_ANY_STRING_VIEW
  243. namespace boost {
  244. namespace static_strings {
  245. /// The type of `basic_string_view` used by the library
  246. template<typename CharT, typename Traits>
  247. using basic_string_view =
  248. #ifndef BOOST_STATIC_STRING_STANDALONE
  249. boost::basic_string_view<CharT, Traits>;
  250. #else
  251. std::basic_string_view<CharT, Traits>;
  252. #endif
  253. } // static_strings
  254. } // boost
  255. #endif
  256. #endif