rippled
Loading...
Searching...
No Matches
Expected.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2021 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_BASICS_EXPECTED_H_INCLUDED
21#define RIPPLE_BASICS_EXPECTED_H_INCLUDED
22
23#include <xrpl/basics/contract.h>
24
25#include <boost/outcome.hpp>
26
27#include <concepts>
28#include <stdexcept>
29#include <type_traits>
30
31namespace ripple {
32
40// Exception thrown by an invalid access to Expected.
42{
43 bad_expected_access() : runtime_error("bad expected access")
44 {
45 }
46};
47
48namespace detail {
49
50// Custom policy for Expected. Always throw on an invalid access.
51struct throw_policy : public boost::outcome_v2::policy::base
52{
53 template <class Impl>
54 static constexpr void
55 wide_value_check(Impl&& self)
56 {
57 if (!base::_has_value(std::forward<Impl>(self)))
58 Throw<bad_expected_access>();
59 }
60
61 template <class Impl>
62 static constexpr void
63 wide_error_check(Impl&& self)
64 {
65 if (!base::_has_error(std::forward<Impl>(self)))
66 Throw<bad_expected_access>();
67 }
68
69 template <class Impl>
70 static constexpr void
72 {
73 if (!base::_has_exception(std::forward<Impl>(self)))
74 Throw<bad_expected_access>();
75 }
76};
77
78} // namespace detail
79
80// Definition of Unexpected, which is used to construct the unexpected
81// return type of an Expected.
82template <class E>
84{
85public:
86 static_assert(!std::is_same<E, void>::value, "E must not be void");
87
88 Unexpected() = delete;
89
90 constexpr explicit Unexpected(E const& e) : val_(e)
91 {
92 }
93
94 constexpr explicit Unexpected(E&& e) : val_(std::move(e))
95 {
96 }
97
98 constexpr const E&
99 value() const&
100 {
101 return val_;
102 }
103
104 constexpr E&
106 {
107 return val_;
108 }
109
110 constexpr E&&
111 value() &&
112 {
113 return std::move(val_);
114 }
115
116 constexpr const E&&
117 value() const&&
118 {
119 return std::move(val_);
120 }
121
122private:
124};
125
126// Unexpected deduction guide that converts array to const*.
127template <typename E, std::size_t N>
129
130// Definition of Expected. All of the machinery comes from boost::result.
131template <class T, class E>
132class [[nodiscard]] Expected
133 : private boost::outcome_v2::result<T, E, detail::throw_policy>
134{
135 using Base = boost::outcome_v2::result<T, E, detail::throw_policy>;
136
137public:
138 template <typename U>
139 requires std::convertible_to<U, T>
140 constexpr Expected(U&& r)
141 : Base(boost::outcome_v2::in_place_type_t<T>{}, std::forward<U>(r))
142 {
143 }
144
145 template <typename U>
146 requires std::convertible_to<U, E> && (!std::is_reference_v<U>)
148 : Base(boost::outcome_v2::in_place_type_t<E>{}, std::move(e.value()))
149 {
150 }
151
152 constexpr bool
153 has_value() const
154 {
155 return Base::has_value();
156 }
157
158 constexpr T const&
159 value() const
160 {
161 return Base::value();
162 }
163
164 constexpr T&
166 {
167 return Base::value();
168 }
169
170 constexpr E const&
171 error() const
172 {
173 return Base::error();
174 }
175
176 constexpr E&
178 {
179 return Base::error();
180 }
181
182 constexpr explicit
183 operator bool() const
184 {
185 return has_value();
186 }
187
188 // Add operator* and operator-> so the Expected API looks a bit more like
189 // what std::expected is likely to look like. See:
190 // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0323r10.html
191 [[nodiscard]] constexpr T&
193 {
194 return this->value();
195 }
196
197 [[nodiscard]] constexpr T const&
198 operator*() const
199 {
200 return this->value();
201 }
202
203 [[nodiscard]] constexpr T*
205 {
206 return &this->value();
207 }
208
209 [[nodiscard]] constexpr T const*
211 {
212 return &this->value();
213 }
214};
215
216// Specialization of Expected<void, E>. Allows returning either success
217// (without a value) or the reason for the failure.
218template <class E>
219class [[nodiscard]] Expected<void, E>
220 : private boost::outcome_v2::result<void, E, detail::throw_policy>
221{
222 using Base = boost::outcome_v2::result<void, E, detail::throw_policy>;
223
224public:
225 // The default constructor makes a successful Expected<void, E>.
226 // This aligns with std::expected behavior proposed in P0323R10.
227 constexpr Expected() : Base(boost::outcome_v2::success())
228 {
229 }
230
231 template <typename U>
232 requires std::convertible_to<U, E> && (!std::is_reference_v<U>)
233 constexpr Expected(Unexpected<U> e) : Base(E(std::move(e.value())))
234 {
235 }
236
237 constexpr E const&
238 error() const
239 {
240 return Base::error();
241 }
242
243 constexpr E&
245 {
246 return Base::error();
247 }
248
249 constexpr explicit
250 operator bool() const
251 {
252 return Base::has_value();
253 }
254};
255
256} // namespace ripple
257
258#endif // RIPPLE_BASICS_EXPECTED_H_INCLUDED
constexpr E const & error() const
Definition: Expected.h:238
constexpr Expected(Unexpected< U > e)
Definition: Expected.h:233
constexpr E & error()
Definition: Expected.h:244
boost::outcome_v2::result< void, E, detail::throw_policy > Base
Definition: Expected.h:222
constexpr T const * operator->() const
Definition: Expected.h:210
constexpr T const & value() const
Definition: Expected.h:159
constexpr bool has_value() const
Definition: Expected.h:153
constexpr T const & operator*() const
Definition: Expected.h:198
constexpr E const & error() const
Definition: Expected.h:171
constexpr T & value()
Definition: Expected.h:165
constexpr Expected(Unexpected< U > e)
Definition: Expected.h:147
boost::outcome_v2::result< T, E, detail::throw_policy > Base
Definition: Expected.h:135
constexpr E & error()
Definition: Expected.h:177
constexpr T & operator*()
Definition: Expected.h:192
constexpr Expected(U &&r)
Definition: Expected.h:140
constexpr T * operator->()
Definition: Expected.h:204
constexpr E && value() &&
Definition: Expected.h:111
constexpr const E & value() const &
Definition: Expected.h:99
constexpr Unexpected(E const &e)
Definition: Expected.h:90
constexpr const E && value() const &&
Definition: Expected.h:117
constexpr Unexpected(E &&e)
Definition: Expected.h:94
constexpr E & value() &
Definition: Expected.h:105
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
STL namespace.
Expected is an approximation of std::expected (hoped for in C++23)
Definition: Expected.h:42
static constexpr void wide_exception_check(Impl &&self)
Definition: Expected.h:71
static constexpr void wide_value_check(Impl &&self)
Definition: Expected.h:55
static constexpr void wide_error_check(Impl &&self)
Definition: Expected.h:63