Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / outcome / experimental / status_outcome.hpp
1 /* A less simple result type
2 (C) 2018-2019 Niall Douglas <http://www.nedproductions.biz/> (17 commits)
3 File Created: Apr 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_EXPERIMENTAL_STATUS_OUTCOME_HPP
32 #define BOOST_OUTCOME_EXPERIMENTAL_STATUS_OUTCOME_HPP
33
34 #include "../basic_outcome.hpp"
35
36 #include "../detail/trait_std_exception.hpp"
37 #include "status_result.hpp"
38
39 #include "boost/exception_ptr.hpp"
40
41 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
42 template <class DomainType> inline std::exception_ptr basic_outcome_failure_exception_from_error(const status_code<DomainType> &sc)
43 {
44   (void) sc;
45 #ifndef BOOST_NO_EXCEPTIONS
46   try
47   {
48     sc.throw_exception();
49   }
50   catch(...)
51   {
52     return std::current_exception();
53   }
54 #endif
55   return {};
56 }
57 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
58
59 BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
60
61 namespace experimental
62 {
63   namespace policy
64   {
65     template <class T, class EC, class E>
66     using default_status_outcome_policy = std::conditional_t<                                                                                                                              //
67     std::is_void<EC>::value && std::is_void<E>::value,                                                                                                                                     //
68     BOOST_OUTCOME_V2_NAMESPACE::policy::terminate,                                                                                                                                               //
69     std::conditional_t<(is_status_code<EC>::value || is_errored_status_code<EC>::value) && (std::is_void<E>::value || BOOST_OUTCOME_V2_NAMESPACE::trait::is_exception_ptr_available<E>::value),  //
70                        status_code_throw<T, EC, E>,                                                                                                                                        //
71                        BOOST_OUTCOME_V2_NAMESPACE::policy::fail_to_compile_observers                                                                                                             //
72                        >>;
73   }  // namespace policy
74
75   /*! AWAITING HUGO JSON CONVERSION TOOL 
76 SIGNATURE NOT RECOGNISED
77 */
78   template <class R, class S = system_code, class P = std::exception_ptr, class NoValuePolicy = policy::default_status_outcome_policy<R, S, P>>  //
79   using status_outcome = basic_outcome<R, S, P, NoValuePolicy>;
80
81   namespace policy
82   {
83     template <class T, class DomainType, class E> struct status_code_throw<T, status_code<DomainType>, E> : base
84     {
85       using _base = base;
86       template <class Impl> static constexpr void wide_value_check(Impl &&self)
87       {
88         if(!base::_has_value(static_cast<Impl &&>(self)))
89         {
90           if(base::_has_exception(static_cast<Impl &&>(self)))
91           {
92             BOOST_OUTCOME_V2_NAMESPACE::policy::detail::_rethrow_exception<trait::is_exception_ptr_available<E>::value>(base::_exception<T, status_code<DomainType>, E, status_code_throw>(static_cast<Impl &&>(self)));  // NOLINT
93           }
94           if(base::_has_error(static_cast<Impl &&>(self)))
95           {
96 #ifndef BOOST_NO_EXCEPTIONS
97             base::_error(static_cast<Impl &&>(self)).throw_exception();
98 #else
99             BOOST_OUTCOME_THROW_EXCEPTION("wide value check failed");
100 #endif
101           }
102         }
103       }
104       template <class Impl> static constexpr void wide_error_check(Impl &&self) { _base::narrow_error_check(static_cast<Impl &&>(self)); }
105       template <class Impl> static constexpr void wide_exception_check(Impl &&self) { _base::narrow_exception_check(static_cast<Impl &&>(self)); }
106     };
107     template <class T, class DomainType, class E> struct status_code_throw<T, errored_status_code<DomainType>, E> : status_code_throw<T, status_code<DomainType>, E>
108     {
109       status_code_throw() = default;
110       using status_code_throw<T, status_code<DomainType>, E>::status_code_throw;
111     };
112   }  // namespace policy
113
114 }  // namespace experimental
115
116 BOOST_OUTCOME_V2_NAMESPACE_END
117
118 #endif