config.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* Proposed SG14 status_code
  2. (C) 2018-2019 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: Feb 2018
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_CONFIG_HPP
  26. #define BOOST_OUTCOME_SYSTEM_ERROR2_CONFIG_HPP
  27. // < 0.1 each
  28. #include <cassert>
  29. #include <cstddef> // for size_t
  30. #include <cstdlib> // for free
  31. // 0.22
  32. #include <type_traits>
  33. // 0.29
  34. #include <atomic>
  35. // 0.28 (0.15 of which is exception_ptr)
  36. #include <exception> // for std::exception
  37. // <new> includes <exception>, <exception> includes <new>
  38. #include <new>
  39. // 0.01
  40. #include <initializer_list>
  41. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14
  42. #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || __cplusplus >= 201400 || _MSC_VER >= 1910 /* VS2017 */
  43. //! Defined to be `constexpr` when on C++ 14 or better compilers. Usually automatic, can be overriden.
  44. #define BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 constexpr
  45. #else
  46. #define BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14
  47. #endif
  48. #endif
  49. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN
  50. #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || (_HAS_CXX17 && _MSC_VER >= 1911 /* VS2017.3 */)
  51. #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN [[noreturn]]
  52. #endif
  53. #endif
  54. #if !defined(BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN)
  55. #ifdef __has_cpp_attribute
  56. #if __has_cpp_attribute(noreturn)
  57. #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN [[noreturn]]
  58. #endif
  59. #endif
  60. #endif
  61. #if !defined(BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN)
  62. #if defined(_MSC_VER)
  63. #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN __declspec(noreturn)
  64. #elif defined(__GNUC__)
  65. #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN __attribute__((__noreturn__))
  66. #else
  67. #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN
  68. #endif
  69. #endif
  70. // GCCs before 7 don't grok [[noreturn]] virtual functions, and warn annoyingly
  71. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 7
  72. #undef BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN
  73. #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN
  74. #endif
  75. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD
  76. #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || (_HAS_CXX17 && _MSC_VER >= 1911 /* VS2017.3 */)
  77. #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD [[nodiscard]]
  78. #endif
  79. #endif
  80. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD
  81. #ifdef __has_cpp_attribute
  82. #if __has_cpp_attribute(nodiscard)
  83. #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD [[nodiscard]]
  84. #endif
  85. #elif defined(__clang__)
  86. #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD __attribute__((warn_unused_result))
  87. #elif defined(_MSC_VER)
  88. // _Must_inspect_result_ expands into this
  89. #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD \
  90. __declspec("SAL_name" \
  91. "(" \
  92. "\"_Must_inspect_result_\"" \
  93. "," \
  94. "\"\"" \
  95. "," \
  96. "\"2\"" \
  97. ")") __declspec("SAL_begin") __declspec("SAL_post") __declspec("SAL_mustInspect") __declspec("SAL_post") __declspec("SAL_checkReturn") __declspec("SAL_end")
  98. #endif
  99. #endif
  100. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD
  101. #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD
  102. #endif
  103. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE
  104. //! The system_error2 namespace name.
  105. #define BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE system_error2
  106. //! Begins the system_error2 namespace.
  107. #define BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN \
  108. namespace system_error2 \
  109. {
  110. //! Ends the system_error2 namespace.
  111. #define BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END }
  112. #endif
  113. //! Namespace for the library
  114. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  115. //! Namespace for user specialised traits
  116. namespace traits
  117. {
  118. /*! Specialise to true if you guarantee that a type is move relocating (i.e.
  119. its move constructor equals copying bits from old to new, old is left in a
  120. default constructed state, and calling the destructor on a default constructed
  121. instance is trivial). All trivially copyable types are move relocating by
  122. definition, and that is the unspecialised implementation.
  123. */
  124. template <class T> struct is_move_relocating
  125. {
  126. static constexpr bool value = std::is_trivially_copyable<T>::value;
  127. };
  128. } // namespace traits
  129. namespace detail
  130. {
  131. inline BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 size_t cstrlen(const char *str)
  132. {
  133. const char *end = nullptr;
  134. for(end = str; *end != 0; ++end) // NOLINT
  135. ;
  136. return end - str;
  137. }
  138. /* A partially compliant implementation of C++20's std::bit_cast function contributed
  139. by Jesse Towner. TODO FIXME Replace with C++ 20 bit_cast when available.
  140. Our bit_cast is only guaranteed to be constexpr when both the input and output
  141. arguments are either integrals or enums. However, this covers most use cases
  142. since the vast majority of status_codes have an underlying type that is either
  143. an integral or enum.
  144. */
  145. template <class T> using is_integral_or_enum = std::integral_constant<bool, std::is_integral<T>::value || std::is_enum<T>::value>;
  146. 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>;
  147. 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>;
  148. template <class To, class From> union bit_cast_union {
  149. From source;
  150. To target;
  151. };
  152. template <class To, class From, typename std::enable_if<is_bit_castable<To, From>::value && is_static_castable<To, From>::value, bool>::type = true> constexpr To bit_cast(const From &from) noexcept { return static_cast<To>(from); }
  153. template <class To, class From, typename std::enable_if<is_bit_castable<To, From>::value && !is_static_castable<To, From>::value, bool>::type = true> constexpr To bit_cast(const From &from) noexcept { return bit_cast_union<To, From>{from}.target; }
  154. /* erasure_cast performs a bit_cast with additional rules to handle types
  155. of differing sizes. For integral & enum types, it may perform a narrowing
  156. or widing conversion with static_cast if necessary, before doing the final
  157. conversion with bit_cast. When casting to or from non-integral, non-enum
  158. types it may insert the value into another object with extra padding bytes
  159. to satisfy bit_cast's preconditions that both types have the same size. */
  160. 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>;
  161. template <class T, bool = std::is_enum<T>::value> struct identity_or_underlying_type
  162. {
  163. using type = T;
  164. };
  165. template <class T> struct identity_or_underlying_type<T, true>
  166. {
  167. using type = typename std::underlying_type<T>::type;
  168. };
  169. template <class OfSize, class OfSign>
  170. 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;
  171. template <class ErasedType, std::size_t N> struct padded_erasure_object
  172. {
  173. static_assert(traits::is_move_relocating<ErasedType>::value, "ErasedType must be TriviallyCopyable or MoveRelocating");
  174. static_assert(alignof(ErasedType) <= sizeof(ErasedType), "ErasedType must not be over-aligned");
  175. ErasedType value;
  176. char padding[N];
  177. constexpr explicit padded_erasure_object(const ErasedType &v) noexcept
  178. : value(v)
  179. , padding{}
  180. {
  181. }
  182. };
  183. 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); }
  184. 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)); }
  185. 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)); }
  186. 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
  187. {
  188. return bit_cast<padded_erasure_object<To, sizeof(From) - sizeof(To)>>(from).value;
  189. }
  190. 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
  191. {
  192. return bit_cast<To>(padded_erasure_object<From, sizeof(To) - sizeof(From)>{from});
  193. }
  194. } // namespace detail
  195. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  196. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_FATAL
  197. #include <cstdlib> // for abort
  198. #ifdef __APPLE__
  199. #include <unistd.h> // for write
  200. #endif
  201. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  202. namespace detail
  203. {
  204. namespace avoid_stdio_include
  205. {
  206. #ifndef __APPLE__
  207. extern "C" ptrdiff_t write(int, const void *, size_t);
  208. #endif
  209. }
  210. inline void do_fatal_exit(const char *msg)
  211. {
  212. using namespace avoid_stdio_include;
  213. write(2 /*stderr*/, msg, cstrlen(msg));
  214. write(2 /*stderr*/, "\n", 1);
  215. abort();
  216. }
  217. } // namespace detail
  218. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  219. //! Prints msg to stderr, and calls `std::terminate()`. Can be overriden via predefinition.
  220. #define BOOST_OUTCOME_SYSTEM_ERROR2_FATAL(msg) ::BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::detail::do_fatal_exit(msg)
  221. #endif
  222. #endif