1 /* A very simple result type
2 (C) 2018-2019 Niall Douglas <http://www.nedproductions.biz/> (11 commits)
6 Boost Software License - Version 1.0 - August 17th, 2003
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:
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.
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.
31 #ifndef BOOST_OUTCOME_EXPERIMENTAL_STATUS_RESULT_HPP
32 #define BOOST_OUTCOME_EXPERIMENTAL_STATUS_RESULT_HPP
34 #include "../basic_result.hpp"
35 #include "../policy/fail_to_compile_observers.hpp"
37 #include "status-code/system_error2.hpp"
39 BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
45 // Shortcut this for lower build impact. Used to tell outcome's converting constructors
46 // that they can do E => EC or E => EP as necessary.
47 template <class DomainType> struct _is_error_code_available<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code<DomainType>>
49 static constexpr bool value = true;
50 using type = BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code<DomainType>;
52 template <class DomainType> struct _is_error_code_available<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errored_status_code<DomainType>>
54 static constexpr bool value = true;
55 using type = BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errored_status_code<DomainType>;
58 #if 0 // Do NOT enable weakened implicit construction for these types
59 template <class DomainType> struct is_error_type<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code<DomainType>>
61 static constexpr bool value = true;
63 template <> struct is_error_type<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errc>
65 static constexpr bool value = true;
67 template <class DomainType, class Enum> struct is_error_type_enum<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code<DomainType>, Enum>
69 static constexpr bool value = boost::system::is_error_condition_enum<Enum>::value;
76 // Customise _set_error_is_errno
77 template <class State> constexpr inline void _set_error_is_errno(State &state, const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::generic_code & /*unused*/) { state._status |= status_error_is_errno; }
78 template <class State> constexpr inline void _set_error_is_errno(State &state, const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::posix_code & /*unused*/) { state._status |= status_error_is_errno; }
79 template <class State> constexpr inline void _set_error_is_errno(State &state, const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errc & /*unused*/) { state._status |= status_error_is_errno; }
83 namespace experimental
85 using namespace BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE;
86 using BOOST_OUTCOME_V2_NAMESPACE::success;
87 using BOOST_OUTCOME_V2_NAMESPACE::failure;
91 using namespace BOOST_OUTCOME_V2_NAMESPACE::policy;
92 template <class T, class EC, class E> struct status_code_throw
94 static_assert(!std::is_same<T, T>::value, "policy::status_code_throw not specialised for these types, did you use status_result<T, status_code<DomainType>, E>?");
96 template <class T, class DomainType> struct status_code_throw<T, status_code<DomainType>, void> : base
99 template <class Impl> static constexpr void wide_value_check(Impl &&self)
101 if(!base::_has_value(static_cast<Impl &&>(self)))
103 if(base::_has_error(static_cast<Impl &&>(self)))
105 #ifndef BOOST_NO_EXCEPTIONS
106 base::_error(static_cast<Impl &&>(self)).throw_exception();
108 BOOST_OUTCOME_THROW_EXCEPTION("wide value check failed");
113 template <class Impl> static constexpr void wide_error_check(Impl &&self) { _base::narrow_error_check(static_cast<Impl &&>(self)); }
115 template <class T, class DomainType> struct status_code_throw<T, errored_status_code<DomainType>, void> : status_code_throw<T, status_code<DomainType>, void>
117 status_code_throw() = default;
118 using status_code_throw<T, status_code<DomainType>, void>::status_code_throw;
121 template <class T, class EC>
122 using default_status_result_policy = std::conditional_t< //
123 std::is_void<EC>::value, //
124 BOOST_OUTCOME_V2_NAMESPACE::policy::terminate, //
125 std::conditional_t<is_status_code<EC>::value || is_errored_status_code<EC>::value, //
126 status_code_throw<T, EC, void>, //
127 BOOST_OUTCOME_V2_NAMESPACE::policy::fail_to_compile_observers //
129 } // namespace policy
131 /*! AWAITING HUGO JSON CONVERSION TOOL
132 SIGNATURE NOT RECOGNISED
134 template <class R, class S = system_code, class NoValuePolicy = policy::default_status_result_policy<R, S>> //
135 using status_result = basic_result<R, S, NoValuePolicy>;
137 } // namespace experimental
139 BOOST_OUTCOME_V2_NAMESPACE_END