userver: userver/utils/expected.hpp Source File
Loading...
Searching...
No Matches
expected.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <concepts>
7#include <stdexcept>
8#include <string>
9#include <utility>
10#include <variant>
11
12#include <userver/compiler/impl/lifetime.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace utils {
18
19// NOLINTBEGIN(readability-identifier-naming)
20
21namespace impl {
22
23// Standard library implementations of std::cmp_equal are not required to be SFINAE-friendly (e.g. libstdc++
24// enforces the "both types are standard integers" precondition via a body-level static_assert instead of a
25// constrained declaration), so `requires { std::cmp_equal(lhs, rhs); }` is not a portable way to detect
26// applicability. Do the check manually instead, following the exact rules of [utility.intcmp].
27template <class T>
28concept StandardInteger =
29 std::integral<T> && !std::same_as<T, bool> && !std::same_as<T, char> && !std::same_as<T, wchar_t> &&
30 !std::same_as<T, char8_t> && !std::same_as<T, char16_t> && !std::same_as<T, char32_t>;
31
34template <class T, class U>
35constexpr bool ExpectedEqual(const T& lhs, const U& rhs) {
36 if constexpr (StandardInteger<T> && StandardInteger<U>) {
37 return std::cmp_equal(lhs, rhs);
38 } else {
39 return lhs == rhs;
40 }
41}
42
43} // namespace impl
44
45class bad_expected_access : public std::exception {
46public:
47 using std::exception::exception;
48
49 explicit bad_expected_access(const std::string& message)
50 : message_{message}
51 {}
52
53 const char* what() const noexcept override;
54
55private:
56 std::string message_;
57};
58
59template <class E>
60class [[nodiscard]] unexpected {
61public:
62 unexpected(const E& error);
63 unexpected(E&& error);
64
65 template <class... Args>
66 unexpected(Args&&... args);
67
68 template <class U, class... Args>
69 unexpected(std::initializer_list<U> il, Args&&... args);
70
71 E& error() noexcept USERVER_IMPL_LIFETIME_BOUND;
72 const E& error() const noexcept USERVER_IMPL_LIFETIME_BOUND;
73
74private:
75 E value_;
76};
77
78template <class E>
80
84template <class S, class E>
85class [[nodiscard]] expected {
86public:
87 constexpr expected();
88 expected(const S& success);
89 expected(S&& success);
90 expected(const unexpected<E>& error);
91 expected(unexpected<E>&& error);
92
93 template <class G>
94 requires std::is_convertible_v<G, E>
95 expected(const unexpected<G>& error);
96
97 template <class G>
98 requires std::is_convertible_v<G, E>
99 expected(unexpected<G>&& error);
100
102 bool has_value() const noexcept;
103
105 explicit operator bool() const noexcept;
106
110 S& value() & USERVER_IMPL_LIFETIME_BOUND;
111
113 S&& value() && USERVER_IMPL_LIFETIME_BOUND;
114
116 const S& value() const& USERVER_IMPL_LIFETIME_BOUND;
117
121 S& operator*() & noexcept USERVER_IMPL_LIFETIME_BOUND;
122
124 S&& operator*() && noexcept USERVER_IMPL_LIFETIME_BOUND;
125
127 const S& operator*() const& noexcept USERVER_IMPL_LIFETIME_BOUND;
128
132 S* operator->() noexcept USERVER_IMPL_LIFETIME_BOUND;
133
135 const S* operator->() const noexcept USERVER_IMPL_LIFETIME_BOUND;
136
140 E& error() USERVER_IMPL_LIFETIME_BOUND;
141
143 const E& error() const USERVER_IMPL_LIFETIME_BOUND;
144
145private:
146 std::variant<S, unexpected<E>> data_;
147};
148
149template <class E>
150class [[nodiscard]] expected<void, E> {
151public:
152 constexpr expected() noexcept;
153 expected(const unexpected<E>& error);
154 expected(unexpected<E>&& error);
155
156 template <class G>
157 requires std::is_convertible_v<G, E>
158 expected(const unexpected<G>& error);
159
160 template <class G>
161 requires std::is_convertible_v<G, E>
162 expected(unexpected<G>&& error);
163
164 bool has_value() const noexcept;
165 explicit operator bool() const noexcept;
166 void value() const;
167
168 E& error() USERVER_IMPL_LIFETIME_BOUND;
169 const E& error() const USERVER_IMPL_LIFETIME_BOUND;
170
171private:
172 std::variant<std::monostate, unexpected<E>> data_;
173};
174
175template <class E>
176unexpected<E>::unexpected(const E& error)
177 : value_{error}
178{}
179
180template <class E>
181unexpected<E>::unexpected(E&& error)
182 : value_{std::forward<E>(error)}
183{}
184
185template <class E>
186template <class... Args>
187unexpected<E>::unexpected(Args&&... args)
188 : value_(std::forward<Args>(args)...)
189{}
190
191template <class E>
192template <class U, class... Args>
193unexpected<E>::unexpected(std::initializer_list<U> il, Args&&... args)
194 : value_(il, std::forward<Args>(args)...)
195{}
196
197template <class E>
198E& unexpected<E>::error() noexcept USERVER_IMPL_LIFETIME_BOUND {
199 return value_;
200}
201
202template <class E>
203const E& unexpected<E>::error() const noexcept USERVER_IMPL_LIFETIME_BOUND {
204 return value_;
205}
206
207template <class S, class E>
208constexpr expected<S, E>::expected()
209 : data_(std::in_place_index<0>)
210{}
211
212template <class S, class E>
213expected<S, E>::expected(const S& success)
214 : data_(success)
215{}
216
217template <class S, class E>
218expected<S, E>::expected(S&& success)
219 : data_(std::forward<S>(success))
220{}
221
222template <class S, class E>
223expected<S, E>::expected(const unexpected<E>& error)
224 : data_(error.error())
225{}
226
227template <class S, class E>
228expected<S, E>::expected(unexpected<E>&& error)
229 : data_(std::forward<unexpected<E>>(error.error()))
230{}
231
232template <class S, class E>
233template <class G>
234requires std::is_convertible_v<G, E>
235expected<S, E>::expected(const unexpected<G>& error)
236 : data_(utils::unexpected<E>(std::forward<G>(error.error())))
237{}
238
239template <class S, class E>
240template <class G>
241requires std::is_convertible_v<G, E>
242expected<S, E>::expected(unexpected<G>&& error)
243 : data_(utils::unexpected<E>(std::forward<G>(error.error())))
244{}
245
246template <class S, class E>
247bool expected<S, E>::has_value() const noexcept {
248 return std::holds_alternative<S>(data_);
249}
250
251template <class S, class E>
252expected<S, E>::operator bool() const noexcept {
253 return has_value();
254}
255
256template <class S, class E>
257 S& expected<S, E>::value() & USERVER_IMPL_LIFETIME_BOUND {
258 S* result = std::get_if<S>(&data_);
259 if (result == nullptr) {
260 throw bad_expected_access("Trying to get undefined value from utils::expected");
261 }
262 return *result;
263}
264
265template <class S, class E>
266 S&& expected<S, E>::value() && USERVER_IMPL_LIFETIME_BOUND {
267 return std::move(value());
268}
269
270template <class S, class E>
271const S& expected<S, E>::value() const& USERVER_IMPL_LIFETIME_BOUND {
272 const S* result = std::get_if<S>(&data_);
273 if (result == nullptr) {
274 throw bad_expected_access("Trying to get undefined value from utils::expected");
275 }
276 return *result;
277}
278
279template <class S, class E>
280S& expected<S, E>::operator*() & noexcept USERVER_IMPL_LIFETIME_BOUND {
281 UASSERT_MSG(has_value(), "Trying to dereference utils::expected that does not contain a value");
282 return *std::get_if<S>(&data_);
283}
284
285template <class S, class E>
286S&& expected<S, E>::operator*() && noexcept USERVER_IMPL_LIFETIME_BOUND {
287 return std::move(**this);
288}
289
290template <class S, class E>
291const S& expected<S, E>::operator*() const& noexcept USERVER_IMPL_LIFETIME_BOUND {
292 UASSERT_MSG(has_value(), "Trying to dereference utils::expected that does not contain a value");
293 return *std::get_if<S>(&data_);
294}
295
296template <class S, class E>
297S* expected<S, E>::operator->() noexcept USERVER_IMPL_LIFETIME_BOUND {
298 UASSERT_MSG(has_value(), "Trying to dereference utils::expected that does not contain a value");
299 return std::get_if<S>(&data_);
300}
301
302template <class S, class E>
303const S* expected<S, E>::operator->() const noexcept USERVER_IMPL_LIFETIME_BOUND {
304 UASSERT_MSG(has_value(), "Trying to dereference utils::expected that does not contain a value");
305 return std::get_if<S>(&data_);
306}
307
308template <class S, class E>
309E& expected<S, E>::error() USERVER_IMPL_LIFETIME_BOUND {
310 auto* result = std::get_if<unexpected<E>>(&data_);
311 if (result == nullptr) {
312 throw bad_expected_access("Trying to get undefined error value from utils::expected");
313 }
314 return result->error();
315}
316
317template <class S, class E>
318const E& expected<S, E>::error() const USERVER_IMPL_LIFETIME_BOUND {
319 const auto* result = std::get_if<unexpected<E>>(&data_);
320 if (result == nullptr) {
321 throw bad_expected_access("Trying to get undefined error value from utils::expected");
322 }
323 return result->error();
324}
325
330template <class S, class E>
331requires std::equality_comparable<S> && std::equality_comparable<E>
332bool operator==(const expected<S, E>& lhs, const expected<S, E>& rhs) {
333 if (lhs.has_value() != rhs.has_value()) {
334 return false;
335 }
336 return lhs.has_value() ? impl::ExpectedEqual(*lhs, *rhs) : impl::ExpectedEqual(lhs.error(), rhs.error());
337}
338
343template <class S, class E, class T>
344requires(!std::same_as<T, expected<S, E>>) && std::equality_comparable_with<S, T>
345bool operator==(const expected<S, E>& lhs, const T& rhs) {
346 return lhs.has_value() && impl::ExpectedEqual(*lhs, rhs);
347}
348
353template <class S, class E, class G>
354requires std::equality_comparable_with<E, G>
355bool operator==(const expected<S, E>& lhs, const unexpected<G>& rhs) {
356 return !lhs.has_value() && impl::ExpectedEqual(lhs.error(), rhs.error());
357}
358
359template <class E>
360constexpr expected<void, E>::expected() noexcept: data_(std::in_place_index<0>) {}
361
362template <class E>
363expected<void, E>::expected(const unexpected<E>& error)
364 : data_(error.error())
365{}
366
367template <class E>
368expected<void, E>::expected(unexpected<E>&& error)
369 : data_(std::forward<unexpected<E>>(error.error()))
370{}
371
372template <class E>
373template <class G>
374requires std::is_convertible_v<G, E>
375expected<void, E>::expected(const unexpected<G>& error)
376 : data_(utils::unexpected<E>(std::forward<G>(error.error())))
377{}
378
379template <class E>
380template <class G>
381requires std::is_convertible_v<G, E>
382expected<void, E>::expected(unexpected<G>&& error)
383 : data_(utils::unexpected<E>(std::forward<G>(error.error())))
384{}
385
386template <class E>
387bool expected<void, E>::has_value() const noexcept {
388 return data_.index() == 0;
389}
390
391template <class E>
392expected<void, E>::operator bool() const noexcept {
393 return has_value();
394}
395
396template <class E>
397void expected<void, E>::value() const {
398 if (!has_value()) {
399 throw bad_expected_access("Trying to get undefined value from utils::expected");
400 }
401}
402
403template <class E>
404E& expected<void, E>::error() USERVER_IMPL_LIFETIME_BOUND {
405 auto* result = std::get_if<unexpected<E>>(&data_);
406 if (result == nullptr) {
407 throw bad_expected_access("Trying to get undefined error value from utils::expected");
408 }
409 return result->error();
410}
411
412template <class E>
413const E& expected<void, E>::error() const USERVER_IMPL_LIFETIME_BOUND {
414 const auto* result = std::get_if<unexpected<E>>(&data_);
415 if (result == nullptr) {
416 throw bad_expected_access("Trying to get undefined error value from utils::expected");
417 }
418 return result->error();
419}
420
425template <class E>
426requires std::equality_comparable<E>
427bool operator==(const expected<void, E>& lhs, const expected<void, E>& rhs) {
428 if (lhs.has_value() != rhs.has_value()) {
429 return false;
430 }
431 return lhs.has_value() || impl::ExpectedEqual(lhs.error(), rhs.error());
432}
433
439template <class E, class G>
440requires std::equality_comparable_with<E, G>
441bool operator==(const expected<void, E>& lhs, const unexpected<G>& rhs) {
442 return !lhs.has_value() && impl::ExpectedEqual(lhs.error(), rhs.error());
443}
444
445// NOLINTEND(readability-identifier-naming)
446
447} // namespace utils
448
449USERVER_NAMESPACE_END