Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / outcome / convert.hpp
1 /* Says how to convert value, error and exception types
2 (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (12 commits)
3 File Created: Nov 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_CONVERT_HPP
32 #define BOOST_OUTCOME_CONVERT_HPP
33
34 #include "detail/basic_result_storage.hpp"
35
36 BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
37
38 namespace convert
39 {
40 #if defined(__cpp_concepts)
41 #if !defined(_MSC_VER) && !defined(__clang__) && __GNUC__ < 9
42 #define BOOST_OUTCOME_GCC6_CONCEPT_BOOL bool
43 #else
44 #define BOOST_OUTCOME_GCC6_CONCEPT_BOOL
45 #endif
46   namespace detail
47   {
48     template <class T, class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL SameHelper = std::is_same<T, U>::value;
49     template <class T, class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL same_as = detail::SameHelper<T, U> &&detail::SameHelper<U, T>;
50   }  // namespace detail
51
52
53   /* The `ValueOrNone` concept.
54   \requires That `U::value_type` exists and that `std::declval<U>().has_value()` returns a `bool` and `std::declval<U>().value()` exists.
55   */
56   template <class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL ValueOrNone = requires(U a)
57   {
58     {
59       a.has_value()
60     }
61     ->detail::same_as<bool>;
62     {a.value()};
63   };
64   /* The `ValueOrError` concept.
65   \requires That `U::value_type` and `U::error_type` exist;
66   that `std::declval<U>().has_value()` returns a `bool`, `std::declval<U>().value()` and  `std::declval<U>().error()` exists.
67   */
68   template <class U> concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL ValueOrError = requires(U a)
69   {
70     {
71       a.has_value()
72     }
73     ->detail::same_as<bool>;
74     {a.value()};
75     {a.error()};
76   };
77 #else
78   namespace detail
79   {
80     struct no_match
81     {
82     };
83     inline no_match match_value_or_none(...);
84     inline no_match match_value_or_error(...);
85     BOOST_OUTCOME_TEMPLATE(class U)
86     BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval<U>().has_value()), BOOST_OUTCOME_TEXPR(std::declval<U>().value()))
87     inline U match_value_or_none(U &&);
88     BOOST_OUTCOME_TEMPLATE(class U)
89     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()))
90     inline U match_value_or_error(U &&);
91
92     template <class U> static constexpr bool ValueOrNone = !std::is_same<no_match, decltype(match_value_or_none(std::declval<BOOST_OUTCOME_V2_NAMESPACE::detail::devoid<U>>()))>::value;
93     template <class U> static constexpr bool ValueOrError = !std::is_same<no_match, decltype(match_value_or_error(std::declval<BOOST_OUTCOME_V2_NAMESPACE::detail::devoid<U>>()))>::value;
94   }  // namespace detail
95   /* The `ValueOrNone` concept.
96   \requires That `U::value_type` exists and that `std::declval<U>().has_value()` returns a `bool` and `std::declval<U>().value()` exists.
97   */
98   template <class U> static constexpr bool ValueOrNone = detail::ValueOrNone<U>;
99   /* The `ValueOrError` concept.
100   \requires That `U::value_type` and `U::error_type` exist;
101   that `std::declval<U>().has_value()` returns a `bool`, `std::declval<U>().value()` and  `std::declval<U>().error()` exists.
102   */
103   template <class U> static constexpr bool ValueOrError = detail::ValueOrError<U>;
104 #endif
105
106   namespace detail
107   {
108     template <class T, class X> struct make_type
109     {
110       template <class U> static constexpr T value(U &&v) { return T{in_place_type<typename T::value_type>, static_cast<U &&>(v).value()}; }
111       template <class U> static constexpr T error(U &&v) { return T{in_place_type<typename T::error_type>, static_cast<U &&>(v).error()}; }
112       static constexpr T error() { return T{in_place_type<typename T::error_type>}; }
113     };
114     template <class T> struct make_type<T, void>
115     {
116       template <class U> static constexpr T value(U && /*unused*/) { return T{in_place_type<typename T::value_type>}; }
117       template <class U> static constexpr T error(U && /*unused*/) { return T{in_place_type<typename T::error_type>}; }
118       static constexpr T error() { return T{in_place_type<typename T::error_type>}; }
119     };
120   }  // namespace detail
121
122   /*! AWAITING HUGO JSON CONVERSION TOOL
123 type definition  value_or_error. Potential doc page: NOT FOUND
124 */
125   template <class T, class U> struct value_or_error
126   {
127     static constexpr bool enable_result_inputs = false;
128     static constexpr bool enable_outcome_inputs = false;
129     BOOST_OUTCOME_TEMPLATE(class X)
130     BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_same<U, std::decay_t<X>>::value                                                                                                                                                    //
131                                     &&ValueOrError<U>                                                                                                                                                                          //
132                                     && (std::is_void<typename std::decay_t<X>::value_type>::value || BOOST_OUTCOME_V2_NAMESPACE::detail::is_explicitly_constructible<typename T::value_type, typename std::decay_t<X>::value_type>)  //
133                                     &&(std::is_void<typename std::decay_t<X>::error_type>::value || BOOST_OUTCOME_V2_NAMESPACE::detail::is_explicitly_constructible<typename T::error_type, typename std::decay_t<X>::error_type>) ))
134     constexpr T operator()(X &&v) { return v.has_value() ? detail::make_type<T, typename T::value_type>::value(static_cast<X &&>(v)) : detail::make_type<T, typename U::error_type>::error(static_cast<X &&>(v)); }
135   };
136 }  // namespace convert
137
138 BOOST_OUTCOME_V2_NAMESPACE_END
139
140 #endif