| 1 | /* Traits for Outcome |
| 2 | (C) 2018-2024 Niall Douglas <http://www.nedproductions.biz/> (6 commits) |
| 3 | File Created: March 2018 |
| 4 | |
| 5 | |
| 6 | Boost Software License - Version 1.0 - August 17th, 2003 |
| 7 | |
| 8 | Permission is hereby granted, free of charge, to any person or organization |
| 9 | obtaining a copy of the software and accompanying documentation covered by |
| 10 | this license (the "Software") to use, reproduce, display, distribute, |
| 11 | execute, and transmit the Software, and to prepare derivative works of the |
| 12 | Software, and to permit third-parties to whom the Software is furnished to |
| 13 | do so, all subject to the following: |
| 14 | |
| 15 | The copyright notices in the Software and this entire statement, including |
| 16 | the above license grant, this restriction and the following disclaimer, |
| 17 | must be included in all copies of the Software, in whole or in part, and |
| 18 | all derivative works of the Software, unless such copies or derivative |
| 19 | works are solely in the form of machine-executable object code generated by |
| 20 | a source language processor. |
| 21 | |
| 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 25 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 26 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 27 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 28 | DEALINGS IN THE SOFTWARE. |
| 29 | */ |
| 30 | |
| 31 | #ifndef BOOST_OUTCOME_TRAIT_STD_ERROR_CODE_HPP |
| 32 | #define BOOST_OUTCOME_TRAIT_STD_ERROR_CODE_HPP |
| 33 | |
| 34 | #include "../config.hpp" |
| 35 | |
| 36 | #include <system_error> |
| 37 | |
| 38 | BOOST_OUTCOME_V2_NAMESPACE_BEGIN |
| 39 | |
| 40 | namespace detail |
| 41 | { |
| 42 | // Customise _set_error_is_errno |
| 43 | template <class State> constexpr inline void _set_error_is_errno(State &state, const std::error_code &error) |
| 44 | { |
| 45 | if(error.category() == std::generic_category() |
| 46 | #ifndef _WIN32 |
| 47 | || error.category() == std::system_category() |
| 48 | #endif |
| 49 | ) |
| 50 | { |
| 51 | state._status.set_have_error_is_errno(true); |
| 52 | } |
| 53 | } |
| 54 | template <class State> constexpr inline void _set_error_is_errno(State &state, const std::error_condition &error) |
| 55 | { |
| 56 | if(error.category() == std::generic_category() |
| 57 | #ifndef _WIN32 |
| 58 | || error.category() == std::system_category() |
| 59 | #endif |
| 60 | ) |
| 61 | { |
| 62 | state._status.set_have_error_is_errno(true); |
| 63 | } |
| 64 | } |
| 65 | template <class State> constexpr inline void _set_error_is_errno(State &state, const std::errc & /*unused*/) { |
| 66 | state._status.set_have_error_is_errno(true); |
| 67 | } |
| 68 | |
| 69 | } // namespace detail |
| 70 | |
| 71 | namespace policy |
| 72 | { |
| 73 | namespace detail |
| 74 | { |
| 75 | /* Pass through `make_error_code` function for `std::error_code`. |
| 76 | */ |
| 77 | inline std::error_code make_error_code(std::error_code v) { return v; } |
| 78 | |
| 79 | // Try ADL, if not use fall backs above |
| 80 | template <class T> constexpr inline decltype(auto) error_code(T &&v) { return make_error_code(std::forward<T>(v)); } |
| 81 | |
| 82 | struct std_enum_overload_tag |
| 83 | { |
| 84 | }; |
| 85 | } // namespace detail |
| 86 | |
| 87 | /*! AWAITING HUGO JSON CONVERSION TOOL |
| 88 | SIGNATURE NOT RECOGNISED |
| 89 | */ |
| 90 | template <class T> constexpr inline decltype(auto) error_code(T &&v) { return detail::error_code(std::forward<T>(v)); } |
| 91 | |
| 92 | /*! AWAITING HUGO JSON CONVERSION TOOL |
| 93 | SIGNATURE NOT RECOGNISED |
| 94 | */ |
| 95 | // inline void outcome_throw_as_system_error_with_payload(...) = delete; // To use the error_code_throw_as_system_error policy with a custom Error type, you must define a outcome_throw_as_system_error_with_payload() free function to say how to handle the payload |
| 96 | inline void outcome_throw_as_system_error_with_payload(const std::error_code &error) { BOOST_OUTCOME_THROW_EXCEPTION(std::system_error(error)); } // NOLINT |
| 97 | BOOST_OUTCOME_TEMPLATE(class Error) |
| 98 | BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_error_code_enum<std::decay_t<Error>>::value || std::is_error_condition_enum<std::decay_t<Error>>::value)) |
| 99 | inline void outcome_throw_as_system_error_with_payload(Error &&error, detail::std_enum_overload_tag /*unused*/ = detail::std_enum_overload_tag()) { BOOST_OUTCOME_THROW_EXCEPTION(std::system_error(make_error_code(error))); } // NOLINT |
| 100 | } // namespace policy |
| 101 | |
| 102 | namespace trait |
| 103 | { |
| 104 | namespace detail |
| 105 | { |
| 106 | template <> struct _is_error_code_available<std::error_code> |
| 107 | { |
| 108 | // Shortcut this for lower build impact |
| 109 | static constexpr bool value = true; |
| 110 | using type = std::error_code; |
| 111 | }; |
| 112 | } // namespace detail |
| 113 | |
| 114 | // std::error_code is an error type |
| 115 | template <> struct is_error_type<std::error_code> |
| 116 | { |
| 117 | static constexpr bool value = true; |
| 118 | }; |
| 119 | // For std::error_code, std::is_error_condition_enum<> is the trait we want. |
| 120 | template <class Enum> struct is_error_type_enum<std::error_code, Enum> |
| 121 | { |
| 122 | static constexpr bool value = std::is_error_condition_enum<Enum>::value; |
| 123 | }; |
| 124 | |
| 125 | } // namespace trait |
| 126 | |
| 127 | BOOST_OUTCOME_V2_NAMESPACE_END |
| 128 | |
| 129 | #endif |
| 130 | |