1/* Says how to convert value, error and exception types
2(C) 2017-2024 Niall Douglas <http://www.nedproductions.biz/> (12 commits)
3File Created: Nov 2017
4
5
6Boost Software License - Version 1.0 - August 17th, 2003
7
8Permission is hereby granted, free of charge, to any person or organization
9obtaining a copy of the software and accompanying documentation covered by
10this license (the "Software") to use, reproduce, display, distribute,
11execute, and transmit the Software, and to prepare derivative works of the
12Software, and to permit third-parties to whom the Software is furnished to
13do so, all subject to the following:
14
15The copyright notices in the Software and this entire statement, including
16the above license grant, this restriction and the following disclaimer,
17must be included in all copies of the Software, in whole or in part, and
18all derivative works of the Software, unless such copies or derivative
19works are solely in the form of machine-executable object code generated by
20a source language processor.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28DEALINGS IN THE SOFTWARE.
29*/
30
31#ifndef BOOST_OUTCOME_CONVERT_HPP
32#define BOOST_OUTCOME_CONVERT_HPP
33
34#include "detail/basic_result_storage.hpp"
35
36BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
37
38namespace concepts
39{
40#if defined(__cpp_concepts)
41#if(defined(_MSC_VER) || defined(__clang__) || (defined(__GNUC__) && __cpp_concepts >= 201707) || BOOST_OUTCOME_FORCE_STD_BOOST_OUTCOME_C_CONCEPTS) && \
42!BOOST_OUTCOME_FORCE_LEGACY_GCC_BOOST_OUTCOME_C_CONCEPTS
43#define BOOST_OUTCOME_GCC6_CONCEPT_BOOL
44#else
45#ifndef BOOST_OUTCOME_SUPPRESS_LEGACY_CONCEPTS_WARNING
46#warning "WARNING: Legacy GCC concepts are known to fail to compile in a number of important situations!"
47#endif
48#define BOOST_OUTCOME_GCC6_CONCEPT_BOOL bool
49#endif
50 namespace detail
51 {
52 template <class T, class U>
53 concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL SameHelper = std::is_same<T, U>::value;
54 template <class T, class U>
55 concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL same_as = detail::SameHelper<T, U> && detail::SameHelper<U, T>;
56 template <class T, class U>
57 concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL convertible = std::is_convertible<T, U>::value;
58 template <class T, class U>
59 concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL base_of = std::is_base_of<T, U>::value;
60 } // namespace detail
61
62
63 /* The `value_or_none` concept.
64 \requires That `U::value_type` exists and that `std::declval<U>().has_value()` returns a `bool` and `std::declval<U>().value()` exists.
65 */
66 template <class U>
67 concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL value_or_none = requires(U a) {
68 {
69 a.has_value()
70 } -> detail::same_as<bool>;
71 {
72 a.value()
73 };
74 };
75 /* The `value_or_error` concept.
76 \requires That `U::value_type` and `U::error_type` exist;
77 that `std::declval<U>().has_value()` returns a `bool`, `std::declval<U>().value()` and `std::declval<U>().error()` exists.
78 */
79 template <class U>
80 concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL value_or_error = requires(U a) {
81 {
82 a.has_value()
83 } -> detail::same_as<bool>;
84 {
85 a.value()
86 };
87 {
88 a.error()
89 };
90 };
91
92#else
93 namespace detail
94 {
95 struct no_match
96 {
97 };
98 inline no_match match_value_or_none(...);
99 inline no_match match_value_or_error(...);
100 BOOST_OUTCOME_TEMPLATE(class U)
101 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<U>().has_value()), BOOST_OUTCOME_TEXPR(std::declval<U>().value()))
102 inline U match_value_or_none(U &&);
103 BOOST_OUTCOME_TEMPLATE(class U)
104 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<U>().has_value()), BOOST_OUTCOME_TEXPR(std::declval<U>().value()), BOOST_OUTCOME_TEXPR(std::declval<U>().error()))
105 inline U match_value_or_error(U &&);
106
107 template <class U>
108 static constexpr bool value_or_none =
109 !std::is_same<no_match, decltype(match_value_or_none(std::declval<BOOST_OUTCOME_V2_NAMESPACE::detail::devoid<U>>()))>::value;
110 template <class U>
111 static constexpr bool value_or_error =
112 !std::is_same<no_match, decltype(match_value_or_error(std::declval<BOOST_OUTCOME_V2_NAMESPACE::detail::devoid<U>>()))>::value;
113 } // namespace detail
114 /* The `value_or_none` concept.
115 \requires That `U::value_type` exists and that `std::declval<U>().has_value()` returns a `bool` and `std::declval<U>().value()` exists.
116 */
117 template <class U> static constexpr bool value_or_none = detail::value_or_none<U>;
118 /* The `value_or_error` concept.
119 \requires That `U::value_type` and `U::error_type` exist;
120 that `std::declval<U>().has_value()` returns a `bool`, `std::declval<U>().value()` and `std::declval<U>().error()` exists.
121 */
122 template <class U> static constexpr bool value_or_error = detail::value_or_error<U>;
123#endif
124} // namespace concepts
125
126namespace convert
127{
128#if BOOST_OUTCOME_ENABLE_LEGACY_SUPPORT_FOR < 220
129#if defined(__cpp_concepts)
130 template <class U>
131 concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL ValueOrNone = concepts::value_or_none<U>;
132 template <class U>
133 concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL ValueOrError = concepts::value_or_error<U>;
134#else
135 template <class U> static constexpr bool ValueOrNone = concepts::value_or_none<U>;
136 template <class U> static constexpr bool ValueOrError = concepts::value_or_error<U>;
137#endif
138#endif
139
140 namespace detail
141 {
142 template <class T, class X> struct make_type
143 {
144 template <class U> static constexpr T value(U &&v) { return T{in_place_type<typename T::value_type>, static_cast<U &&>(v).value()}; }
145 template <class U> static constexpr T error(U &&v) { return T{in_place_type<typename T::error_type>, static_cast<U &&>(v).error()}; }
146 static constexpr T error() { return T{in_place_type<typename T::error_type>}; }
147 };
148 template <class T> struct make_type<T, void>
149 {
150 template <class U> static constexpr T value(U && /*unused*/) { return T{in_place_type<typename T::value_type>}; }
151 template <class U> static constexpr T error(U && /*unused*/) { return T{in_place_type<typename T::error_type>}; }
152 static constexpr T error() { return T{in_place_type<typename T::error_type>}; }
153 };
154 } // namespace detail
155
156 /*! AWAITING HUGO JSON CONVERSION TOOL
157type definition value_or_error. Potential doc page: NOT FOUND
158*/
159 template <class T, class U> struct value_or_error
160 {
161 static constexpr bool enable_result_inputs = false;
162 static constexpr bool enable_outcome_inputs = false;
163 BOOST_OUTCOME_TEMPLATE(class X)
164 BOOST_OUTCOME_TREQUIRES(
165 BOOST_OUTCOME_TPRED(std::is_same<U, std::decay_t<X>>::value //
166 &&concepts::value_or_error<U> //
167 && (std::is_void<typename std::decay_t<X>::value_type>::value ||
168 BOOST_OUTCOME_V2_NAMESPACE::detail::is_explicitly_constructible<typename T::value_type, typename std::decay_t<X>::value_type>) //
169 &&(std::is_void<typename std::decay_t<X>::error_type>::value ||
170 BOOST_OUTCOME_V2_NAMESPACE::detail::is_explicitly_constructible<typename T::error_type, typename std::decay_t<X>::error_type>) ))
171 constexpr T operator()(X &&v)
172 {
173 return v.has_value() ? detail::make_type<T, typename T::value_type>::value(static_cast<X &&>(v)) :
174 detail::make_type<T, typename U::error_type>::error(static_cast<X &&>(v));
175 }
176 };
177} // namespace convert
178
179BOOST_OUTCOME_V2_NAMESPACE_END
180
181#endif
182

source code of boost/libs/outcome/include/boost/outcome/convert.hpp