config.hpp 8.5 KB

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