Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / outcome / detail / trait_std_error_code.hpp
1 /* Traits for Outcome
2 (C) 2018-2019 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 |= status_error_is_errno;
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 |= status_error_is_errno;
63     }
64   }
65   template <class State> constexpr inline void _set_error_is_errno(State &state, const std::errc & /*unused*/) { state._status |= status_error_is_errno; }
66
67 }  // namespace detail
68
69 namespace policy
70 {
71   namespace detail
72   {
73     /* Pass through `make_error_code` function for `std::error_code`.
74      */
75     inline std::error_code make_error_code(std::error_code v) { return v; }
76
77     // Try ADL, if not use fall backs above
78     template <class T> constexpr inline decltype(auto) error_code(T &&v) { return make_error_code(std::forward<T>(v)); }
79
80     struct std_enum_overload_tag
81     {
82     };
83   }  // namespace detail
84
85   /*! AWAITING HUGO JSON CONVERSION TOOL 
86 SIGNATURE NOT RECOGNISED
87 */
88   template <class T> constexpr inline decltype(auto) error_code(T &&v) { return detail::error_code(std::forward<T>(v)); }
89
90   /*! AWAITING HUGO JSON CONVERSION TOOL 
91 SIGNATURE NOT RECOGNISED
92 */
93   // 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
94   inline void outcome_throw_as_system_error_with_payload(const std::error_code &error) { BOOST_OUTCOME_THROW_EXCEPTION(std::system_error(error)); }  // NOLINT
95   BOOST_OUTCOME_TEMPLATE(class Error)
96   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))
97   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
98 }  // namespace policy
99
100 namespace trait
101 {
102   namespace detail
103   {
104     template <> struct _is_error_code_available<std::error_code>
105     {
106       // Shortcut this for lower build impact
107       static constexpr bool value = true;
108       using type = std::error_code;
109     };
110   }  // namespace detail
111
112   // std::error_code is an error type
113   template <> struct is_error_type<std::error_code>
114   {
115     static constexpr bool value = true;
116   };
117   // For std::error_code, std::is_error_condition_enum<> is the trait we want.
118   template <class Enum> struct is_error_type_enum<std::error_code, Enum>
119   {
120     static constexpr bool value = std::is_error_condition_enum<Enum>::value;
121   };
122
123 }  // namespace trait
124
125 BOOST_OUTCOME_V2_NAMESPACE_END
126
127 #endif