Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / outcome / boost_result.hpp
1 /* A very simple result type
2 (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (10 commits)
3 File Created: June 2017
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_BOOST_RESULT_HPP
32 #define BOOST_OUTCOME_BOOST_RESULT_HPP
33
34 #include "config.hpp"
35
36 #include "boost/exception_ptr.hpp"
37 #include "boost/system/system_error.hpp"
38
39 BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
40
41 /*! AWAITING HUGO JSON CONVERSION TOOL 
42 SIGNATURE NOT RECOGNISED
43 */
44 namespace policy
45 {
46   namespace detail
47   {
48     /* Pass through `make_error_code` function for `boost::system::error_code`.
49      */
50     inline boost::system::error_code make_error_code(boost::system::error_code v) { return v; }
51
52     /* Pass through `make_exception_ptr` function for `boost::exception_ptr`.
53         The reason this needs to be here, declared before the rest of Outcome,
54         is that there is no boost::make_exception_ptr as Boost still uses the old
55         naming boost::copy_exception. Therefore the ADL discovered make_exception_ptr
56         doesn't work, hence this hacky pre-declaration here.
57
58         I was tempted to just inject a boost::make_exception_ptr, but I can see
59         Boost doing that itself at some point. This hack should keep working after.
60         */
61     inline boost::exception_ptr make_exception_ptr(boost::exception_ptr v) { return v; }
62   }  // namespace detail
63 }  // namespace policy
64 BOOST_OUTCOME_V2_NAMESPACE_END
65
66 #include "std_result.hpp"
67
68
69 // ADL injection of outcome_throw_as_system_error_with_payload
70 namespace boost
71 {
72   namespace system
73   {
74     inline void outcome_throw_as_system_error_with_payload(const error_code &error) { BOOST_OUTCOME_THROW_EXCEPTION(system_error(error)); }
75     namespace errc
76     {
77       BOOST_OUTCOME_TEMPLATE(class Error)
78       BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(is_error_code_enum<std::decay_t<Error>>::value || is_error_condition_enum<std::decay_t<Error>>::value))
79       inline void outcome_throw_as_system_error_with_payload(Error &&error) { BOOST_OUTCOME_THROW_EXCEPTION(system_error(make_error_code(error))); }
80     }  // namespace errc
81   }    // namespace system
82 }  // namespace boost
83
84 BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
85
86 namespace detail
87 {
88   // Customise _set_error_is_errno
89   template <class State> constexpr inline void _set_error_is_errno(State &state, const boost::system::error_code &error)
90   {
91     if(error.category() == boost::system::generic_category()
92 #ifndef _WIN32
93        || error.category() == boost::system::system_category()
94 #endif
95     )
96     {
97       state._status |= status_error_is_errno;
98     }
99   }
100   template <class State> constexpr inline void _set_error_is_errno(State &state, const boost::system::error_condition &error)
101   {
102     if(error.category() == boost::system::generic_category()
103 #ifndef _WIN32
104        || error.category() == boost::system::system_category()
105 #endif
106     )
107     {
108       state._status |= status_error_is_errno;
109     }
110   }
111   template <class State> constexpr inline void _set_error_is_errno(State &state, const boost::system::errc::errc_t & /*unused*/) { state._status |= status_error_is_errno; }
112
113 }  // namespace detail
114
115 /*! AWAITING HUGO JSON CONVERSION TOOL 
116 SIGNATURE NOT RECOGNISED
117 */
118 namespace trait
119 {
120   namespace detail
121   {
122     // Shortcut these for lower build impact
123     template <> struct _is_error_code_available<boost::system::error_code>
124     {
125       static constexpr bool value = true;
126       using type = boost::system::error_code;
127     };
128     template <> struct _is_exception_ptr_available<boost::exception_ptr>
129     {
130       static constexpr bool value = true;
131       using type = boost::exception_ptr;
132     };
133   }  // namespace detail
134
135   // boost::system::error_code is an error type
136   template <> struct is_error_type<boost::system::error_code>
137   {
138     static constexpr bool value = true;
139   };
140   // boost::system::error_code::errc_t is an error type
141   template <> struct is_error_type<boost::system::errc::errc_t>
142   {
143     static constexpr bool value = true;
144   };
145   // boost::exception_ptr is an error types
146   template <> struct is_error_type<boost::exception_ptr>
147   {
148     static constexpr bool value = true;
149   };
150   // For boost::system::error_code, boost::system::is_error_condition_enum<> is the trait we want.
151   template <class Enum> struct is_error_type_enum<boost::system::error_code, Enum>
152   {
153     static constexpr bool value = boost::system::is_error_condition_enum<Enum>::value;
154   };
155
156 }  // namespace trait
157
158
159 /*! AWAITING HUGO JSON CONVERSION TOOL 
160 SIGNATURE NOT RECOGNISED
161 */
162 template <class R, class S = boost::system::error_code, class NoValuePolicy = policy::default_policy<R, S, void>>  //
163 using boost_result = basic_result<R, S, NoValuePolicy>;
164
165 /*! AWAITING HUGO JSON CONVERSION TOOL 
166 type alias template <class R, class S = boost::system::error_code> boost_unchecked. Potential doc page: `boost_unchecked<T, E = boost::system::error_code>`
167 */
168 template <class R, class S = boost::system::error_code> using boost_unchecked = boost_result<R, S, policy::all_narrow>;
169
170 /*! AWAITING HUGO JSON CONVERSION TOOL 
171 type alias template <class R, class S = boost::system::error_code> boost_checked. Potential doc page: `boost_checked<T, E = boost::system::error_code>`
172 */
173 template <class R, class S = boost::system::error_code> using boost_checked = boost_result<R, S, policy::throw_bad_result_access<S, void>>;
174
175 BOOST_OUTCOME_V2_NAMESPACE_END
176
177 #endif