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#if defined(__clang__)
26#pragma clang diagnostic push
27#pragma clang diagnostic ignored "-Wdeprecated"
28#pragma clang diagnostic ignored "-Wdeprecated-declarations"
29#endif
30
31#include <boost/outcome.hpp>
32
33#if defined(__clang__)
34#pragma clang diagnostic pop
35#endif
36
37#include <stdexcept>
38
39namespace ripple {
40
48// Exception thrown by an invalid access to Expected.
50{
51 bad_expected_access() : runtime_error("bad expected access")
52 {
53 }
54};
55
56namespace detail {
57
58// Custom policy for Expected. Always throw on an invalid access.
59struct throw_policy : public boost::outcome_v2::policy::base
60{
61 template <class Impl>
62 static constexpr void
63 wide_value_check(Impl&& self)
64 {
65 if (!base::_has_value(std::forward<Impl>(self)))
66 Throw<bad_expected_access>();
67 }
68
69 template <class Impl>
70 static constexpr void
71 wide_error_check(Impl&& self)
72 {
73 if (!base::_has_error(std::forward<Impl>(self)))
74 Throw<bad_expected_access>();
75 }
76
77 template <class Impl>
78 static constexpr void
80 {
81 if (!base::_has_exception(std::forward<Impl>(self)))
82 Throw<bad_expected_access>();
83 }
84};
85
86} // namespace detail
87
88// Definition of Unexpected, which is used to construct the unexpected
89// return type of an Expected.
90template <class E>
92{
93public:
94 static_assert(!std::is_same<E, void>::value, "E must not be void");
95
96 Unexpected() = delete;
97
98 constexpr explicit Unexpected(E const& e) : val_(e)
99 {
100 }
101
102 constexpr explicit Unexpected(E&& e) : val_(std::move(e))
103 {
104 }
105
106 constexpr E const&
107 value() const&
108 {
109 return val_;
110 }
111
112 constexpr E&
114 {
115 return val_;
116 }
117
118 constexpr E&&
119 value() &&
120 {
121 return std::move(val_);
122 }
123
124 constexpr E const&&
125 value() const&&
126 {
127 return std::move(val_);
128 }
129
130private:
132};
133
134// Unexpected deduction guide that converts array to const*.
135template <typename E, std::size_t N>
137
138// Definition of Expected. All of the machinery comes from boost::result.
139template <class T, class E>
140class [[nodiscard]] Expected
141 : private boost::outcome_v2::result<T, E, detail::throw_policy>
142{
143 using Base = boost::outcome_v2::result<T, E, detail::throw_policy>;
144
145public:
146 template <typename U>
147 requires std::convertible_to<U, T>
148 constexpr Expected(U&& r)
149 : Base(boost::outcome_v2::in_place_type_t<T>{}, std::forward<U>(r))
150 {
151 }
152
153 template <typename U>
154 requires std::convertible_to<U, E> && (!std::is_reference_v<U>)
156 : Base(boost::outcome_v2::in_place_type_t<E>{}, std::move(e.value()))
157 {
158 }
159
160 constexpr bool
161 has_value() const
162 {
163 return Base::has_value();
164 }
165
166 constexpr T const&
167 value() const
168 {
169 return Base::value();
170 }
171
172 constexpr T&
174 {
175 return Base::value();
176 }
177
178 constexpr E const&
179 error() const
180 {
181 return Base::error();
182 }
183
184 constexpr E&
186 {
187 return Base::error();
188 }
189
190 constexpr explicit
191 operator bool() const
192 {
193 return has_value();
194 }
195
196 // Add operator* and operator-> so the Expected API looks a bit more like
197 // what std::expected is likely to look like. See:
198 // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0323r10.html
199 [[nodiscard]] constexpr T&
201 {
202 return this->value();
203 }
204
205 [[nodiscard]] constexpr T const&
206 operator*() const
207 {
208 return this->value();
209 }
210
211 [[nodiscard]] constexpr T*
213 {
214 return &this->value();
215 }
216
217 [[nodiscard]] constexpr T const*
219 {
220 return &this->value();
221 }
222};
223
224// Specialization of Expected<void, E>. Allows returning either success
225// (without a value) or the reason for the failure.
226template <class E>
227class [[nodiscard]] Expected<void, E>
228 : private boost::outcome_v2::result<void, E, detail::throw_policy>
229{
230 using Base = boost::outcome_v2::result<void, E, detail::throw_policy>;
231
232public:
233 // The default constructor makes a successful Expected<void, E>.
234 // This aligns with std::expected behavior proposed in P0323R10.
235 constexpr Expected() : Base(boost::outcome_v2::success())
236 {
237 }
238
239 template <typename U>
240 requires std::convertible_to<U, E> && (!std::is_reference_v<U>)
241 constexpr Expected(Unexpected<U> e) : Base(E(std::move(e.value())))
242 {
243 }
244
245 constexpr E const&
246 error() const
247 {
248 return Base::error();
249 }
250
251 constexpr E&
253 {
254 return Base::error();
255 }
256
257 constexpr explicit
258 operator bool() const
259 {
260 return Base::has_value();
261 }
262};
263
264} // namespace ripple
265
266#endif // RIPPLE_BASICS_EXPECTED_H_INCLUDED
constexpr E const & error() const
Definition: Expected.h:246
constexpr Expected(Unexpected< U > e)
Definition: Expected.h:241
constexpr E & error()
Definition: Expected.h:252
boost::outcome_v2::result< void, E, detail::throw_policy > Base
Definition: Expected.h:230
constexpr T const * operator->() const
Definition: Expected.h:218
constexpr T const & value() const
Definition: Expected.h:167
constexpr bool has_value() const
Definition: Expected.h:161
constexpr T const & operator*() const
Definition: Expected.h:206
constexpr E const & error() const
Definition: Expected.h:179
constexpr T & value()
Definition: Expected.h:173
constexpr Expected(Unexpected< U > e)
Definition: Expected.h:155
boost::outcome_v2::result< T, E, detail::throw_policy > Base
Definition: Expected.h:143
constexpr E & error()
Definition: Expected.h:185
constexpr T & operator*()
Definition: Expected.h:200
constexpr Expected(U &&r)
Definition: Expected.h:148
constexpr T * operator->()
Definition: Expected.h:212
constexpr E && value() &&
Definition: Expected.h:119
constexpr Unexpected(E const &e)
Definition: Expected.h:98
constexpr E const && value() const &&
Definition: Expected.h:125
constexpr E const & value() const &
Definition: Expected.h:107
constexpr Unexpected(E &&e)
Definition: Expected.h:102
constexpr E & value() &
Definition: Expected.h:113
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
STL namespace.
Expected is an approximation of std::expected (hoped for in C++23)
Definition: Expected.h:50
static constexpr void wide_exception_check(Impl &&self)
Definition: Expected.h:79
static constexpr void wide_value_check(Impl &&self)
Definition: Expected.h:63
static constexpr void wide_error_check(Impl &&self)
Definition: Expected.h:71