config.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* Proposed SG14 status_code
  2. (C) 2018 - 2019 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 relocating (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 relocating by
  123. definition, and that is the unspecialised implementation.
  124. */
  125. template <class T> struct is_move_relocating
  126. {
  127. static constexpr bool value = std::is_trivially_copyable<T>::value;
  128. };
  129. } // namespace traits
  130. namespace detail
  131. {
  132. inline BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 size_t cstrlen(const char *str)
  133. {
  134. const char *end = nullptr;
  135. for(end = str; *end != 0; ++end) // NOLINT
  136. ;
  137. return end - str;
  138. }
  139. /* A partially compliant implementation of C++20's std::bit_cast function contributed
  140. by Jesse Towner. TODO FIXME Replace with C++ 20 bit_cast when available.
  141. Our bit_cast is only guaranteed to be constexpr when both the input and output
  142. arguments are either integrals or enums. However, this covers most use cases
  143. since the vast majority of status_codes have an underlying type that is either
  144. an integral or enum. We still attempt a constexpr union-based type pun for non-array
  145. input types, which some compilers accept. For array inputs, we fall back to
  146. non-constexpr memmove.
  147. */
  148. template <class T> using is_integral_or_enum = std::integral_constant<bool, std::is_integral<T>::value || std::is_enum<T>::value>;
  149. 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>;
  150. 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>;
  151. template <class To, class From> using is_bit_castable = std::integral_constant<bool, sizeof(To) == sizeof(From) && traits::is_move_relocating<To>::value && traits::is_move_relocating<From>::value>;
  152. template <class To, class From> union bit_cast_union {
  153. From source;
  154. To target;
  155. };
  156. template <class To, class From,
  157. typename std::enable_if< //
  158. is_bit_castable<To, From>::value //
  159. && is_static_castable<To, From>::value //
  160. && !is_union_castable<To, From>::value, //
  161. bool>::type = true> //
  162. constexpr To bit_cast(const From &from) noexcept
  163. {
  164. return static_cast<To>(from);
  165. }
  166. template <class To, class From,
  167. typename std::enable_if< //
  168. is_bit_castable<To, From>::value //
  169. && !is_static_castable<To, From>::value //
  170. && is_union_castable<To, From>::value, //
  171. bool>::type = true> //
  172. constexpr To bit_cast(const From &from) noexcept
  173. {
  174. return bit_cast_union<To, From>{from}.target;
  175. }
  176. template <class To, class From,
  177. typename std::enable_if< //
  178. is_bit_castable<To, From>::value //
  179. && !is_static_castable<To, From>::value //
  180. && !is_union_castable<To, From>::value, //
  181. bool>::type = true> //
  182. To bit_cast(const From &from) noexcept
  183. {
  184. bit_cast_union<To, From> ret;
  185. memmove(&ret.source, &from, sizeof(ret.source));
  186. return ret.target;
  187. }
  188. /* erasure_cast performs a bit_cast with additional rules to handle types
  189. of differing sizes. For integral & enum types, it may perform a narrowing
  190. or widing conversion with static_cast if necessary, before doing the final
  191. conversion with bit_cast. When casting to or from non-integral, non-enum
  192. types it may insert the value into another object with extra padding bytes
  193. to satisfy bit_cast's preconditions that both types have the same size. */
  194. template <class To, class From> using is_erasure_castable = std::integral_constant<bool, traits::is_move_relocating<To>::value && traits::is_move_relocating<From>::value>;
  195. template <class T, bool = std::is_enum<T>::value> struct identity_or_underlying_type
  196. {
  197. using type = T;
  198. };
  199. template <class T> struct identity_or_underlying_type<T, true>
  200. {
  201. using type = typename std::underlying_type<T>::type;
  202. };
  203. template <class OfSize, class OfSign>
  204. 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;
  205. template <class ErasedType, std::size_t N> struct padded_erasure_object
  206. {
  207. static_assert(traits::is_move_relocating<ErasedType>::value, "ErasedType must be TriviallyCopyable or MoveRelocating");
  208. static_assert(alignof(ErasedType) <= sizeof(ErasedType), "ErasedType must not be over-aligned");
  209. ErasedType value;
  210. char padding[N];
  211. constexpr explicit padded_erasure_object(const ErasedType &v) noexcept
  212. : value(v)
  213. , padding{}
  214. {
  215. }
  216. };
  217. 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); }
  218. 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)); }
  219. 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)); }
  220. 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
  221. {
  222. return bit_cast<padded_erasure_object<To, sizeof(From) - sizeof(To)>>(from).value;
  223. }
  224. 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
  225. {
  226. return bit_cast<To>(padded_erasure_object<From, sizeof(To) - sizeof(From)>{from});
  227. }
  228. } // namespace detail
  229. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  230. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_FATAL
  231. #include <cstdlib> // for abort
  232. #ifdef __APPLE__
  233. #include <unistd.h> // for write
  234. #endif
  235. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  236. namespace detail
  237. {
  238. namespace avoid_stdio_include
  239. {
  240. #ifndef __APPLE__
  241. extern "C" ptrdiff_t write(int, const void *, size_t);
  242. #endif
  243. } // namespace avoid_stdio_include
  244. inline void do_fatal_exit(const char *msg)
  245. {
  246. using namespace avoid_stdio_include;
  247. write(2 /*stderr*/, msg, cstrlen(msg));
  248. write(2 /*stderr*/, "\n", 1);
  249. abort();
  250. }
  251. } // namespace detail
  252. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  253. //! Prints msg to stderr, and calls `std::terminate()`. Can be overriden via predefinition.
  254. #define BOOST_OUTCOME_SYSTEM_ERROR2_FATAL(msg) ::BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::detail::do_fatal_exit(msg)
  255. #endif
  256. #endif