config.hpp 8.2 KB

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