| 1 | /* Proposed SG14 status_code |
| 2 | (C) 2018 - 2021 Niall Douglas <http://www.nedproductions.biz/> (5 commits) |
| 3 | File Created: Feb 2018 |
| 4 | |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | you may not use this file except in compliance with the License. |
| 8 | You may obtain a copy of the License in the accompanying file |
| 9 | Licence.txt or at |
| 10 | |
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | |
| 13 | Unless required by applicable law or agreed to in writing, software |
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | See the License for the specific language governing permissions and |
| 17 | limitations under the License. |
| 18 | |
| 19 | |
| 20 | Distributed under the Boost Software License, Version 1.0. |
| 21 | (See accompanying file Licence.txt or copy at |
| 22 | http://www.boost.org/LICENSE_1_0.txt) |
| 23 | */ |
| 24 | |
| 25 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_CONFIG_HPP |
| 26 | #define BOOST_OUTCOME_SYSTEM_ERROR2_CONFIG_HPP |
| 27 | |
| 28 | // < 0.1 each |
| 29 | #include <cassert> |
| 30 | #include <cstddef> // for size_t |
| 31 | #include <cstdlib> // for free |
| 32 | |
| 33 | // 0.22 |
| 34 | #include <type_traits> |
| 35 | |
| 36 | // 0.29 |
| 37 | #include <atomic> |
| 38 | |
| 39 | // 0.28 (0.15 of which is exception_ptr) |
| 40 | #include <exception> // for std::exception |
| 41 | // <new> includes <exception>, <exception> includes <new> |
| 42 | #include <new> |
| 43 | |
| 44 | // 0.01 |
| 45 | #include <initializer_list> |
| 46 | |
| 47 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_HAVE_BIT_CAST |
| 48 | #ifdef __has_include |
| 49 | #if __has_include(<bit>) && (__cplusplus >= 202002L || _HAS_CXX20) |
| 50 | #define BOOST_OUTCOME_SYSTEM_ERROR2_HAVE_BIT_CAST 1 |
| 51 | #endif |
| 52 | #elif __cplusplus >= 202002L |
| 53 | #define BOOST_OUTCOME_SYSTEM_ERROR2_HAVE_BIT_CAST 1 |
| 54 | #endif |
| 55 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_HAVE_BIT_CAST |
| 56 | #define BOOST_OUTCOME_SYSTEM_ERROR2_HAVE_BIT_CAST 0 |
| 57 | #endif |
| 58 | #endif |
| 59 | #if BOOST_OUTCOME_SYSTEM_ERROR2_HAVE_BIT_CAST |
| 60 | #include <bit> |
| 61 | #if __cpp_lib_bit_cast < 201806L |
| 62 | #undef BOOST_OUTCOME_SYSTEM_ERROR2_HAVE_BIT_CAST |
| 63 | #define BOOST_OUTCOME_SYSTEM_ERROR2_HAVE_BIT_CAST 0 |
| 64 | #endif |
| 65 | #endif |
| 66 | |
| 67 | #if BOOST_OUTCOME_SYSTEM_ERROR2_USE_STD_ADDRESSOF |
| 68 | #include <memory> // for std::addressof |
| 69 | #define BOOST_OUTCOME_SYSTEM_ERROR2_ADDRESS_OF(...) std::addressof(__VA_ARGS__) |
| 70 | #else |
| 71 | #define BOOST_OUTCOME_SYSTEM_ERROR2_ADDRESS_OF(...) (&__VA_ARGS__) |
| 72 | #endif |
| 73 | |
| 74 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 |
| 75 | #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || __cplusplus >= 201400 || _MSC_VER >= 1910 /* VS2017 */ |
| 76 | //! Defined to be `constexpr` when on C++ 14 or better compilers. Usually automatic, can be overriden. |
| 77 | #define BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 constexpr |
| 78 | #else |
| 79 | #define BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 |
| 80 | #endif |
| 81 | #endif |
| 82 | |
| 83 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR20 |
| 84 | #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || __cplusplus >= 202000 || _HAS_CXX20 |
| 85 | //! Defined to be `constexpr` when on C++ 20 or better compilers. Usually automatic, can be overriden. |
| 86 | #define BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR20 constexpr |
| 87 | #else |
| 88 | #define BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR20 |
| 89 | #endif |
| 90 | #endif |
| 91 | |
| 92 | |
| 93 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN |
| 94 | #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || (_HAS_CXX17 && _MSC_VER >= 1911 /* VS2017.3 */) |
| 95 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN [[noreturn]] |
| 96 | #endif |
| 97 | #endif |
| 98 | #if !defined(BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN) |
| 99 | #ifdef __has_cpp_attribute |
| 100 | #if __has_cpp_attribute(noreturn) |
| 101 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN [[noreturn]] |
| 102 | #endif |
| 103 | #endif |
| 104 | #endif |
| 105 | #if !defined(BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN) |
| 106 | #if defined(_MSC_VER) |
| 107 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN __declspec(noreturn) |
| 108 | #elif defined(__GNUC__) |
| 109 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN __attribute__((__noreturn__)) |
| 110 | #else |
| 111 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN |
| 112 | #endif |
| 113 | #endif |
| 114 | // GCCs before 7 don't grok [[noreturn]] virtual functions, and warn annoyingly |
| 115 | #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 7 |
| 116 | #undef BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN |
| 117 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN |
| 118 | #endif |
| 119 | |
| 120 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD |
| 121 | #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || (_HAS_CXX17 && _MSC_VER >= 1911 /* VS2017.3 */) |
| 122 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD [[nodiscard]] |
| 123 | #endif |
| 124 | #endif |
| 125 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD |
| 126 | #ifdef __has_cpp_attribute |
| 127 | #if __has_cpp_attribute(nodiscard) |
| 128 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD [[nodiscard]] |
| 129 | #endif |
| 130 | #elif defined(__clang__) |
| 131 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD __attribute__((warn_unused_result)) |
| 132 | #elif defined(_MSC_VER) |
| 133 | // _Must_inspect_result_ expands into this |
| 134 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD \ |
| 135 | __declspec( \ |
| 136 | "SAL_name" \ |
| 137 | "(" \ |
| 138 | "\"_Must_inspect_result_\"" \ |
| 139 | "," \ |
| 140 | "\"\"" \ |
| 141 | "," \ |
| 142 | "\"2\"" \ |
| 143 | ")") __declspec("SAL_begin") __declspec("SAL_post") __declspec("SAL_mustInspect") __declspec("SAL_post") __declspec("SAL_checkReturn") __declspec("SAL_end") |
| 144 | #endif |
| 145 | #endif |
| 146 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD |
| 147 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD |
| 148 | #endif |
| 149 | |
| 150 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI |
| 151 | #if defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) || (__clang_major__ >= 7 && !defined(__APPLE__)) |
| 152 | //! Defined to be `[[clang::trivial_abi]]` when on a new enough clang compiler. Usually automatic, can be overriden. |
| 153 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI [[clang::trivial_abi]] |
| 154 | #else |
| 155 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI |
| 156 | #endif |
| 157 | #endif |
| 158 | |
| 159 | #if defined(__cpp_concepts) && !defined(BOOST_OUTCOME_SYSTEM_ERROR2_DISABLE_CONCEPTS_SUPPORT) |
| 160 | #define BOOST_OUTCOME_SYSTEM_ERROR2_GLUE(x, y) x y |
| 161 | |
| 162 | #define BOOST_OUTCOME_SYSTEM_ERROR2_RETURN_ARG_COUNT(_1_, _2_, _3_, _4_, _5_, _6_, _7_, _8_, count, ...) count |
| 163 | #define BOOST_OUTCOME_SYSTEM_ERROR2_EXPAND_ARGS(args) BOOST_OUTCOME_SYSTEM_ERROR2_RETURN_ARG_COUNT args |
| 164 | #define BOOST_OUTCOME_SYSTEM_ERROR2_COUNT_ARGS_MAX8(...) BOOST_OUTCOME_SYSTEM_ERROR2_EXPAND_ARGS((__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0)) |
| 165 | |
| 166 | #define BOOST_OUTCOME_SYSTEM_ERROR2_OVERLOAD_MACRO2(name, count) name##count |
| 167 | #define BOOST_OUTCOME_SYSTEM_ERROR2_OVERLOAD_MACRO1(name, count) BOOST_OUTCOME_SYSTEM_ERROR2_OVERLOAD_MACRO2(name, count) |
| 168 | #define BOOST_OUTCOME_SYSTEM_ERROR2_OVERLOAD_MACRO(name, count) BOOST_OUTCOME_SYSTEM_ERROR2_OVERLOAD_MACRO1(name, count) |
| 169 | |
| 170 | #define BOOST_OUTCOME_SYSTEM_ERROR2_CALL_OVERLOAD(name, ...) BOOST_OUTCOME_SYSTEM_ERROR2_GLUE(BOOST_OUTCOME_SYSTEM_ERROR2_OVERLOAD_MACRO(name, BOOST_OUTCOME_SYSTEM_ERROR2_COUNT_ARGS_MAX8(__VA_ARGS__)), (__VA_ARGS__)) |
| 171 | |
| 172 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND8(a, b, c, d, e, f, g, h) a &&BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND7(b, c, d, e, f, g, h) |
| 173 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND7(a, b, c, d, e, f, g) a &&BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND6(b, c, d, e, f, g) |
| 174 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND6(a, b, c, d, e, f) a &&BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND5(b, c, d, e, f) |
| 175 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND5(a, b, c, d, e) a &&BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND4(b, c, d, e) |
| 176 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND4(a, b, c, d) a &&BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND3(b, c, d) |
| 177 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND3(a, b, c) a &&BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND2(b, c) |
| 178 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND2(a, b) a &&BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND1(b) |
| 179 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND1(a) a |
| 180 | |
| 181 | //! Expands into a && b && c && ... |
| 182 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(...) requires BOOST_OUTCOME_SYSTEM_ERROR2_CALL_OVERLOAD(BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES_EXPAND, __VA_ARGS__) |
| 183 | |
| 184 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(...) template <__VA_ARGS__> |
| 185 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TEXPR(...) \ |
| 186 | requires { (__VA_ARGS__); } |
| 187 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(...) (__VA_ARGS__) |
| 188 | #if !defined(_MSC_VER) || _MSC_FULL_VER >= 192400000 // VS 2019 16.3 is broken here |
| 189 | #define BOOST_OUTCOME_SYSTEM_ERROR2_REQUIRES(...) requires(__VA_ARGS__) |
| 190 | #else |
| 191 | #define BOOST_OUTCOME_SYSTEM_ERROR2_REQUIRES(...) |
| 192 | #endif |
| 193 | #else |
| 194 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(...) template <__VA_ARGS__ |
| 195 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(...) , __VA_ARGS__ > |
| 196 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TEXPR(...) typename = decltype(__VA_ARGS__) |
| 197 | #ifdef _MSC_VER |
| 198 | // MSVC gives an error if every specialisation of a template is always ill-formed, so |
| 199 | // the more powerful SFINAE form below causes pukeage :( |
| 200 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(...) typename = typename std::enable_if<(__VA_ARGS__)>::type |
| 201 | #else |
| 202 | #define BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(...) typename std::enable_if<(__VA_ARGS__), bool>::type = true |
| 203 | #endif |
| 204 | #define BOOST_OUTCOME_SYSTEM_ERROR2_REQUIRES(...) |
| 205 | #endif |
| 206 | |
| 207 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE |
| 208 | //! The system_error2 namespace name. |
| 209 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE system_error2 |
| 210 | //! Begins the system_error2 namespace. |
| 211 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN \ |
| 212 | namespace system_error2 \ |
| 213 | { |
| 214 | //! Ends the system_error2 namespace. |
| 215 | #define BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END } |
| 216 | #endif |
| 217 | |
| 218 | //! Namespace for the library |
| 219 | BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN |
| 220 | |
| 221 | //! Namespace for user specialised traits |
| 222 | namespace traits |
| 223 | { |
| 224 | /*! Specialise to true if you guarantee that a type is move bitcopying (i.e. |
| 225 | its move constructor equals copying bits from old to new, old is left in a |
| 226 | default constructed state, and calling the destructor on a default constructed |
| 227 | instance is trivial). All trivially copyable types are move bitcopying by |
| 228 | definition, and that is the unspecialised implementation. |
| 229 | */ |
| 230 | template <class T> struct is_move_bitcopying |
| 231 | { |
| 232 | static constexpr bool value = std::is_trivially_copyable<T>::value; |
| 233 | }; |
| 234 | } // namespace traits |
| 235 | |
| 236 | namespace detail |
| 237 | { |
| 238 | #if __cplusplus >= 201400 || _MSC_VER >= 1910 /* VS2017 */ |
| 239 | inline constexpr size_t cstrlen(const char *str) |
| 240 | { |
| 241 | const char *end = nullptr; |
| 242 | for(end = str; *end != 0; ++end) // NOLINT |
| 243 | ; |
| 244 | return end - str; |
| 245 | } |
| 246 | #else |
| 247 | inline constexpr size_t cstrlen_(const char *str, size_t acc) { return (str[0] == 0) ? acc : cstrlen_(str + 1, acc + 1); } |
| 248 | inline constexpr size_t cstrlen(const char *str) { return cstrlen_(str, 0); } |
| 249 | #endif |
| 250 | |
| 251 | #if(__cplusplus >= 202002L || _MSVC_LANG >= 202002L) && __cpp_lib_remove_cvref >= 201711L |
| 252 | |
| 253 | template <class T> using remove_cvref = std::remove_cvref<T>; |
| 254 | |
| 255 | #else |
| 256 | |
| 257 | template <class T> struct remove_cvref |
| 258 | { |
| 259 | using type = typename std::remove_cv<typename std::remove_reference<T>::type>::type; |
| 260 | }; |
| 261 | |
| 262 | #endif |
| 263 | |
| 264 | /* A partially compliant implementation of C++20's std::bit_cast function contributed |
| 265 | by Jesse Towner. |
| 266 | |
| 267 | Our bit_cast is only guaranteed to be constexpr when both the input and output |
| 268 | arguments are either integrals or enums. However, this covers most use cases |
| 269 | since the vast majority of status_codes have an underlying type that is either |
| 270 | an integral or enum. We still attempt a constexpr union-based type pun for non-array |
| 271 | input types, which some compilers accept. For array inputs, we fall back to |
| 272 | non-constexpr memmove. |
| 273 | */ |
| 274 | |
| 275 | template <class T> using is_integral_or_enum = std::integral_constant<bool, std::is_integral<T>::value || std::is_enum<T>::value>; |
| 276 | |
| 277 | 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>; |
| 278 | |
| 279 | template <class To, class From> |
| 280 | using is_union_castable = std::integral_constant<bool, !is_static_castable<To, From>::value && !std::is_array<To>::value && !std::is_array<From>::value>; |
| 281 | |
| 282 | template <class To, class From> |
| 283 | using is_bit_castable = |
| 284 | std::integral_constant<bool, sizeof(To) == sizeof(From) && traits::is_move_bitcopying<To>::value && traits::is_move_bitcopying<From>::value>; |
| 285 | |
| 286 | template <class To, class From> union bit_cast_union |
| 287 | { |
| 288 | From source; |
| 289 | To target; |
| 290 | }; |
| 291 | |
| 292 | #if BOOST_OUTCOME_SYSTEM_ERROR2_HAVE_BIT_CAST |
| 293 | using std::bit_cast; // available for all trivially copyable types |
| 294 | |
| 295 | // For move bit copying types |
| 296 | template <class To, class From> |
| 297 | requires(is_bit_castable<To, From>::value // |
| 298 | &&is_union_castable<To, From>::value // |
| 299 | && (!std::is_trivially_copyable_v<From> // |
| 300 | || !std::is_trivially_copyable_v<To>) ) // |
| 301 | constexpr To bit_cast(const From &from) noexcept |
| 302 | { |
| 303 | return bit_cast_union<To, From>{from}.target; |
| 304 | } |
| 305 | template <class To, class From> |
| 306 | requires(is_bit_castable<To, From>::value // |
| 307 | && !is_union_castable<To, From>::value // |
| 308 | && (!std::is_trivially_copyable_v<From> // |
| 309 | || !std::is_trivially_copyable_v<To>) ) // |
| 310 | To bit_cast(const From &from) |
| 311 | noexcept |
| 312 | { |
| 313 | bit_cast_union<To, From> ret; |
| 314 | memmove(&ret.source, &from, sizeof(ret.source)); |
| 315 | return ret.target; |
| 316 | } |
| 317 | #else |
| 318 | BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class To, class From, int = 5) |
| 319 | BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_bit_castable<To, From>::value // |
| 320 | &&is_static_castable<To, From>::value // |
| 321 | && !is_union_castable<To, From>::value)) |
| 322 | constexpr To bit_cast(const From &from) noexcept { return static_cast<To>(from); } |
| 323 | |
| 324 | #if defined(__GNUC__) && !defined(__clang__) |
| 325 | #pragma GCC diagnostic push |
| 326 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
| 327 | #endif |
| 328 | BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class To, class From, long = 5) |
| 329 | BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_bit_castable<To, From>::value // |
| 330 | && !is_static_castable<To, From>::value // |
| 331 | && is_union_castable<To, From>::value)) |
| 332 | constexpr To bit_cast(const From &from) noexcept { return bit_cast_union<To, From>{from}.target; } |
| 333 | #if defined(__GNUC__) && !defined(__clang__) |
| 334 | #pragma GCC diagnostic pop |
| 335 | #endif |
| 336 | |
| 337 | BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class To, class From, short = 5) |
| 338 | BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_bit_castable<To, From>::value // |
| 339 | && !is_static_castable<To, From>::value // |
| 340 | && !is_union_castable<To, From>::value)) |
| 341 | To bit_cast(const From &from) noexcept |
| 342 | { |
| 343 | bit_cast_union<To, From> ret; |
| 344 | memmove(&ret.source, &from, sizeof(ret.source)); |
| 345 | return ret.target; |
| 346 | } |
| 347 | #endif |
| 348 | |
| 349 | /* erasure_cast performs a bit_cast with additional rules to handle types |
| 350 | of differing sizes. For integral & enum types, it may perform a narrowing |
| 351 | or widing conversion with static_cast if necessary, before doing the final |
| 352 | conversion with bit_cast. When casting to or from non-integral, non-enum |
| 353 | types it may insert the value into another object with extra padding bytes |
| 354 | to satisfy bit_cast's preconditions that both types have the same size. */ |
| 355 | |
| 356 | template <class To, class From> |
| 357 | using is_erasure_castable = std::integral_constant<bool, traits::is_move_bitcopying<To>::value && traits::is_move_bitcopying<From>::value>; |
| 358 | |
| 359 | template <class T, bool = std::is_enum<T>::value> struct identity_or_underlying_type |
| 360 | { |
| 361 | using type = T; |
| 362 | }; |
| 363 | template <class T> struct identity_or_underlying_type<T, true> |
| 364 | { |
| 365 | using type = typename std::underlying_type<T>::type; |
| 366 | }; |
| 367 | |
| 368 | template <class OfSize, class OfSign> |
| 369 | using erasure_integer_type = typename std::conditional<std::is_signed<typename identity_or_underlying_type<OfSign>::type>::value, |
| 370 | typename std::make_signed<typename identity_or_underlying_type<OfSize>::type>::type, |
| 371 | typename std::make_unsigned<typename identity_or_underlying_type<OfSize>::type>::type>::type; |
| 372 | |
| 373 | template <class ErasedType, std::size_t N> struct padded_erasure_object |
| 374 | { |
| 375 | static_assert(traits::is_move_bitcopying<ErasedType>::value, "ErasedType must be TriviallyCopyable or MoveBitcopying" ); |
| 376 | static_assert(alignof(ErasedType) <= sizeof(ErasedType), "ErasedType must not be over-aligned" ); |
| 377 | ErasedType value; |
| 378 | char padding[N]; |
| 379 | constexpr explicit padded_erasure_object(const ErasedType &v) noexcept |
| 380 | : value(v) |
| 381 | , padding{} |
| 382 | { |
| 383 | } |
| 384 | }; |
| 385 | |
| 386 | BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class To, class From) |
| 387 | BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_erasure_castable<To, From>::value && (sizeof(To) == sizeof(From)))) |
| 388 | constexpr To erasure_cast(const From &from) noexcept { return bit_cast<To>(from); } |
| 389 | |
| 390 | #if defined(_WIN32) || defined(__APPLE__) || __LITTLE_ENDIAN__ || (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) |
| 391 | // We can avoid the type pun on little endian architectures which can aid optimisation |
| 392 | BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class To, class From, long = 5) |
| 393 | BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_erasure_castable<To, From>::value &&is_static_castable<To, From>::value && (sizeof(To) < sizeof(From)))) |
| 394 | constexpr To erasure_cast(const From &from) noexcept { return static_cast<To>(bit_cast<erasure_integer_type<From, To>>(from)); } |
| 395 | |
| 396 | BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class To, class From, int = 5) |
| 397 | BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_erasure_castable<To, From>::value &&is_static_castable<To, From>::value && (sizeof(To) > sizeof(From)))) |
| 398 | constexpr To erasure_cast(const From &from) noexcept { return bit_cast<To>(static_cast<erasure_integer_type<To, From>>(from)); } |
| 399 | |
| 400 | BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class To, class From, short = 5) |
| 401 | BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_erasure_castable<To, From>::value && !is_static_castable<To, From>::value && (sizeof(To) < sizeof(From)))) |
| 402 | constexpr To erasure_cast(const From &from) noexcept { return bit_cast<padded_erasure_object<To, sizeof(From) - sizeof(To)>>(from).value; } |
| 403 | |
| 404 | BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class To, class From, char = 5) |
| 405 | BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_erasure_castable<To, From>::value && !is_static_castable<To, From>::value && (sizeof(To) > sizeof(From)))) |
| 406 | constexpr To erasure_cast(const From &from) noexcept { return bit_cast<To>(padded_erasure_object<From, sizeof(To) - sizeof(From)>{from}); } |
| 407 | #else |
| 408 | BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class To, class From, short = 5) |
| 409 | BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_erasure_castable<To, From>::value && (sizeof(To) < sizeof(From)))) |
| 410 | constexpr To erasure_cast(const From &from) noexcept { return bit_cast<padded_erasure_object<To, sizeof(From) - sizeof(To)>>(from).value; } |
| 411 | |
| 412 | BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class To, class From, char = 5) |
| 413 | BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(is_erasure_castable<To, From>::value && (sizeof(To) > sizeof(From)))) |
| 414 | constexpr To erasure_cast(const From &from) noexcept { return bit_cast<To>(padded_erasure_object<From, sizeof(To) - sizeof(From)>{from}); } |
| 415 | #endif |
| 416 | } // namespace detail |
| 417 | BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END |
| 418 | |
| 419 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_FATAL |
| 420 | #ifdef BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX |
| 421 | #error If BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX is defined, you must define your own BOOST_OUTCOME_SYSTEM_ERROR2_FATAL implementation! |
| 422 | #endif |
| 423 | #include <cstdlib> // for abort |
| 424 | #ifdef __APPLE__ |
| 425 | #include <unistd.h> // for write |
| 426 | #endif |
| 427 | |
| 428 | BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN |
| 429 | namespace detail |
| 430 | { |
| 431 | namespace avoid_stdio_include |
| 432 | { |
| 433 | #if !defined(__APPLE__) && !defined(_MSC_VER) |
| 434 | extern "C" ptrdiff_t write(int, const void *, size_t); |
| 435 | #elif defined(_MSC_VER) |
| 436 | extern ptrdiff_t write(int, const void *, size_t); |
| 437 | #if(defined(__x86_64__) || defined(_M_X64)) || (defined(__aarch64__) || defined(_M_ARM64)) || (defined(__arm__) || defined(_M_ARM)) |
| 438 | #pragma comment(linker, "/alternatename:?write@avoid_stdio_include@detail@system_error2@@YA_JHPEBX_K@Z=write") |
| 439 | #elif defined(__x86__) || defined(_M_IX86) || defined(__i386__) |
| 440 | #pragma comment(linker, "/alternatename:?write@avoid_stdio_include@detail@system_error2@@YAHHPBXI@Z=_write") |
| 441 | #else |
| 442 | #error Unknown architecture |
| 443 | #endif |
| 444 | #endif |
| 445 | } // namespace avoid_stdio_include |
| 446 | inline void do_fatal_exit(const char *msg) |
| 447 | { |
| 448 | using namespace avoid_stdio_include; |
| 449 | write(2 /*stderr*/, msg, cstrlen(str: msg)); |
| 450 | write(2 /*stderr*/, "\n" , 1); |
| 451 | abort(); |
| 452 | } |
| 453 | } // namespace detail |
| 454 | BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END |
| 455 | //! Prints msg to stderr, and calls `std::terminate()`. Can be overriden via predefinition. |
| 456 | #define BOOST_OUTCOME_SYSTEM_ERROR2_FATAL(msg) ::BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::detail::do_fatal_exit(msg) |
| 457 | #endif |
| 458 | |
| 459 | #endif |
| 460 | |