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