config.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Proposed SG14 status_code
  2. (C) 2018 - 2020 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: Feb 2018
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License in the accompanying file
  7. Licence.txt or at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. Distributed under the Boost Software License, Version 1.0.
  15. (See accompanying file Licence.txt or copy at
  16. http://www.boost.org/LICENSE_1_0.txt)
  17. */
  18. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_CONFIG_HPP
  19. #define BOOST_OUTCOME_SYSTEM_ERROR2_CONFIG_HPP
  20. // < 0.1 each
  21. #include <cassert>
  22. #include <cstddef> // for size_t
  23. #include <cstdlib> // for free
  24. // 0.22
  25. #include <type_traits>
  26. // 0.29
  27. #include <atomic>
  28. // 0.28 (0.15 of which is exception_ptr)
  29. #include <exception> // for std::exception
  30. // <new> includes <exception>, <exception> includes <new>
  31. #include <new>
  32. // 0.01
  33. #include <initializer_list>
  34. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14
  35. #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || __cplusplus >= 201400 || _MSC_VER >= 1910 /* VS2017 */
  36. //! Defined to be `constexpr` when on C++ 14 or better compilers. Usually automatic, can be overriden.
  37. #define BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 constexpr
  38. #else
  39. #define BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14
  40. #endif
  41. #endif
  42. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN
  43. #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || (_HAS_CXX17 && _MSC_VER >= 1911 /* VS2017.3 */)
  44. #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN [[noreturn]]
  45. #endif
  46. #endif
  47. #if !defined(BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN)
  48. #ifdef __has_cpp_attribute
  49. #if __has_cpp_attribute(noreturn)
  50. #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN [[noreturn]]
  51. #endif
  52. #endif
  53. #endif
  54. #if !defined(BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN)
  55. #if defined(_MSC_VER)
  56. #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN __declspec(noreturn)
  57. #elif defined(__GNUC__)
  58. #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN __attribute__((__noreturn__))
  59. #else
  60. #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN
  61. #endif
  62. #endif
  63. // GCCs before 7 don't grok [[noreturn]] virtual functions, and warn annoyingly
  64. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 7
  65. #undef BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN
  66. #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN
  67. #endif
  68. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD
  69. #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || (_HAS_CXX17 && _MSC_VER >= 1911 /* VS2017.3 */)
  70. #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD [[nodiscard]]
  71. #endif
  72. #endif
  73. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD
  74. #ifdef __has_cpp_attribute
  75. #if __has_cpp_attribute(nodiscard)
  76. #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD [[nodiscard]]
  77. #endif
  78. #elif defined(__clang__)
  79. #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD __attribute__((warn_unused_result))
  80. #elif defined(_MSC_VER)
  81. // _Must_inspect_result_ expands into this
  82. #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD \
  83. __declspec("SAL_name" \
  84. "(" \
  85. "\"_Must_inspect_result_\"" \
  86. "," \
  87. "\"\"" \
  88. "," \
  89. "\"2\"" \
  90. ")") __declspec("SAL_begin") __declspec("SAL_post") __declspec("SAL_mustInspect") __declspec("SAL_post") __declspec("SAL_checkReturn") __declspec("SAL_end")
  91. #endif
  92. #endif
  93. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD
  94. #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD
  95. #endif
  96. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI
  97. #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || (__clang_major__ >= 7 && !defined(__APPLE__))
  98. //! Defined to be `[[clang::trivial_abi]]` when on a new enough clang compiler. Usually automatic, can be overriden.
  99. #define BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI [[clang::trivial_abi]]
  100. #else
  101. #define BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI
  102. #endif
  103. #endif
  104. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE
  105. //! The system_error2 namespace name.
  106. #define BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE system_error2
  107. //! Begins the system_error2 namespace.
  108. #define BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN \
  109. namespace system_error2 \
  110. {
  111. //! Ends the system_error2 namespace.
  112. #define BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END }
  113. #endif
  114. //! Namespace for the library
  115. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  116. //! Namespace for user specialised traits
  117. namespace traits
  118. {
  119. /*! Specialise to true if you guarantee that a type is move bitcopying (i.e.
  120. its move constructor equals copying bits from old to new, old is left in a
  121. default constructed state, and calling the destructor on a default constructed
  122. instance is trivial). All trivially copyable types are move bitcopying by
  123. definition, and that is the unspecialised implementation.
  124. */
  125. template <class T> struct is_move_bitcopying
  126. {
  127. static constexpr bool value = std::is_trivially_copyable<T>::value;
  128. };
  129. } // namespace traits
  130. namespace detail
  131. {
  132. #if __cplusplus >= 201400 || _MSC_VER >= 1910 /* VS2017 */
  133. inline constexpr size_t cstrlen(const char *str)
  134. {
  135. const char *end = nullptr;
  136. for(end = str; *end != 0; ++end) // NOLINT
  137. ;
  138. return end - str;
  139. }
  140. #else
  141. inline constexpr size_t cstrlen_(const char *str, size_t acc)
  142. {
  143. return (str[0] == 0) ? acc : cstrlen_(str + 1, acc + 1);
  144. }
  145. inline constexpr size_t cstrlen(const char *str)
  146. {
  147. return cstrlen_(str, 0);
  148. }
  149. #endif
  150. /* A partially compliant implementation of C++20's std::bit_cast function contributed
  151. by Jesse Towner. TODO FIXME Replace with C++ 20 bit_cast when available.
  152. Our bit_cast is only guaranteed to be constexpr when both the input and output
  153. arguments are either integrals or enums. However, this covers most use cases
  154. since the vast majority of status_codes have an underlying type that is either
  155. an integral or enum. We still attempt a constexpr union-based type pun for non-array
  156. input types, which some compilers accept. For array inputs, we fall back to
  157. non-constexpr memmove.
  158. */
  159. template <class T> using is_integral_or_enum = std::integral_constant<bool, std::is_integral<T>::value || std::is_enum<T>::value>;
  160. template <class To, class From> using is_static_castable = std::integral_constant<bool, is_integral_or_enum<To>::value && is_integral_or_enum<From>::value>;
  161. template <class To, class From> using is_union_castable = std::integral_constant<bool, !is_static_castable<To, From>::value && !std::is_array<To>::value && !std::is_array<From>::value>;
  162. template <class To, class From> using is_bit_castable = std::integral_constant<bool, sizeof(To) == sizeof(From) && traits::is_move_bitcopying<To>::value && traits::is_move_bitcopying<From>::value>;
  163. template <class To, class From> union bit_cast_union {
  164. From source;
  165. To target;
  166. };
  167. template <class To, class From,
  168. typename std::enable_if< //
  169. is_bit_castable<To, From>::value //
  170. && is_static_castable<To, From>::value //
  171. && !is_union_castable<To, From>::value, //
  172. bool>::type = true> //
  173. constexpr To bit_cast(const From &from) noexcept
  174. {
  175. return static_cast<To>(from);
  176. }
  177. #if defined(__GNUC__) && !defined(__clang__)
  178. #pragma GCC diagnostic push
  179. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  180. #endif
  181. template <class To, class From,
  182. typename std::enable_if< //
  183. is_bit_castable<To, From>::value //
  184. && !is_static_castable<To, From>::value //
  185. && is_union_castable<To, From>::value, //
  186. bool>::type = true> //
  187. constexpr To bit_cast(const From &from) noexcept
  188. {
  189. return bit_cast_union<To, From>{from}.target;
  190. }
  191. #if defined(__GNUC__) && !defined(__clang__)
  192. #pragma GCC diagnostic pop
  193. #endif
  194. template <class To, class From,
  195. typename std::enable_if< //
  196. is_bit_castable<To, From>::value //
  197. && !is_static_castable<To, From>::value //
  198. && !is_union_castable<To, From>::value, //
  199. bool>::type = true> //
  200. To bit_cast(const From &from) noexcept
  201. {
  202. bit_cast_union<To, From> ret;
  203. memmove(&ret.source, &from, sizeof(ret.source));
  204. return ret.target;
  205. }
  206. /* erasure_cast performs a bit_cast with additional rules to handle types
  207. of differing sizes. For integral & enum types, it may perform a narrowing
  208. or widing conversion with static_cast if necessary, before doing the final
  209. conversion with bit_cast. When casting to or from non-integral, non-enum
  210. types it may insert the value into another object with extra padding bytes
  211. to satisfy bit_cast's preconditions that both types have the same size. */
  212. template <class To, class From> using is_erasure_castable = std::integral_constant<bool, traits::is_move_bitcopying<To>::value && traits::is_move_bitcopying<From>::value>;
  213. template <class T, bool = std::is_enum<T>::value> struct identity_or_underlying_type
  214. {
  215. using type = T;
  216. };
  217. template <class T> struct identity_or_underlying_type<T, true>
  218. {
  219. using type = typename std::underlying_type<T>::type;
  220. };
  221. template <class OfSize, class OfSign>
  222. using erasure_integer_type = typename std::conditional<std::is_signed<typename identity_or_underlying_type<OfSign>::type>::value, typename std::make_signed<typename identity_or_underlying_type<OfSize>::type>::type, typename std::make_unsigned<typename identity_or_underlying_type<OfSize>::type>::type>::type;
  223. template <class ErasedType, std::size_t N> struct padded_erasure_object
  224. {
  225. static_assert(traits::is_move_bitcopying<ErasedType>::value, "ErasedType must be TriviallyCopyable or MoveBitcopying");
  226. static_assert(alignof(ErasedType) <= sizeof(ErasedType), "ErasedType must not be over-aligned");
  227. ErasedType value;
  228. char padding[N];
  229. constexpr explicit padded_erasure_object(const ErasedType &v) noexcept
  230. : value(v)
  231. , padding{}
  232. {
  233. }
  234. };
  235. template <class To, class From, typename std::enable_if<is_erasure_castable<To, From>::value && (sizeof(To) == sizeof(From)), bool>::type = true> constexpr To erasure_cast(const From &from) noexcept { return bit_cast<To>(from); }
  236. template <class To, class From, typename std::enable_if<is_erasure_castable<To, From>::value && is_static_castable<To, From>::value && (sizeof(To) < sizeof(From)), bool>::type = true> constexpr To erasure_cast(const From &from) noexcept { return static_cast<To>(bit_cast<erasure_integer_type<From, To>>(from)); }
  237. template <class To, class From, typename std::enable_if<is_erasure_castable<To, From>::value && is_static_castable<To, From>::value && (sizeof(To) > sizeof(From)), bool>::type = true> constexpr To erasure_cast(const From &from) noexcept { return bit_cast<To>(static_cast<erasure_integer_type<To, From>>(from)); }
  238. template <class To, class From, typename std::enable_if<is_erasure_castable<To, From>::value && !is_static_castable<To, From>::value && (sizeof(To) < sizeof(From)), bool>::type = true> constexpr To erasure_cast(const From &from) noexcept
  239. {
  240. return bit_cast<padded_erasure_object<To, sizeof(From) - sizeof(To)>>(from).value;
  241. }
  242. template <class To, class From, typename std::enable_if<is_erasure_castable<To, From>::value && !is_static_castable<To, From>::value && (sizeof(To) > sizeof(From)), bool>::type = true> constexpr To erasure_cast(const From &from) noexcept
  243. {
  244. return bit_cast<To>(padded_erasure_object<From, sizeof(To) - sizeof(From)>{from});
  245. }
  246. } // namespace detail
  247. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  248. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_FATAL
  249. #ifdef BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX
  250. #error If BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX is defined, you must define your own BOOST_OUTCOME_SYSTEM_ERROR2_FATAL implementation!
  251. #endif
  252. #include <cstdlib> // for abort
  253. #ifdef __APPLE__
  254. #include <unistd.h> // for write
  255. #endif
  256. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  257. namespace detail
  258. {
  259. namespace avoid_stdio_include
  260. {
  261. #if !defined(__APPLE__) && !defined(_MSC_VER)
  262. extern "C" ptrdiff_t write(int, const void *, size_t);
  263. #elif defined(_MSC_VER)
  264. extern ptrdiff_t write(int, const void *, size_t);
  265. #if(defined(__x86_64__) || defined(_M_X64)) || (defined(__aarch64__) || defined(_M_ARM64)) || (defined(__arm__) || defined(_M_ARM))
  266. #pragma comment(linker, "/alternatename:?write@avoid_stdio_include@detail@system_error2@@YA_JHPEBX_K@Z=write")
  267. #elif defined(__x86__) || defined(_M_IX86) || defined(__i386__)
  268. #pragma comment(linker, "/alternatename:?write@avoid_stdio_include@detail@system_error2@@YAHHPBXI@Z=_write")
  269. #else
  270. #error Unknown architecture
  271. #endif
  272. #endif
  273. } // namespace avoid_stdio_include
  274. inline void do_fatal_exit(const char *msg)
  275. {
  276. using namespace avoid_stdio_include;
  277. write(2 /*stderr*/, msg, cstrlen(msg));
  278. write(2 /*stderr*/, "\n", 1);
  279. abort();
  280. }
  281. } // namespace detail
  282. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  283. //! Prints msg to stderr, and calls `std::terminate()`. Can be overriden via predefinition.
  284. #define BOOST_OUTCOME_SYSTEM_ERROR2_FATAL(msg) ::BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::detail::do_fatal_exit(msg)
  285. #endif
  286. #endif