Bitcoin Core 30.99.0
P2P Digital Currency
expected.h
Go to the documentation of this file.
1// Copyright (c) The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://opensource.org/license/mit.
4
5#ifndef BITCOIN_UTIL_EXPECTED_H
6#define BITCOIN_UTIL_EXPECTED_H
7
8#include <attributes.h>
9
10#include <cassert>
11#include <type_traits>
12#include <utility>
13#include <variant>
14
15namespace util {
16
19template <class E>
21{
22public:
23 constexpr explicit Unexpected(E e) : err(std::move(e)) {}
25};
26
32template <class T, class E>
34{
35private:
36 using ValueType = std::conditional_t<std::is_same_v<T, void>, std::monostate, T>;
37 std::variant<ValueType, E> m_data;
38
39public:
40 constexpr Expected() : m_data{std::in_place_index_t<0>{}, ValueType{}} {}
41 constexpr Expected(ValueType v) : m_data{std::in_place_index_t<0>{}, std::move(v)} {}
42 template <class Err>
43 constexpr Expected(Unexpected<Err> u) : m_data{std::in_place_index_t<1>{}, std::move(u.err)}
44 {
45 }
46
47 constexpr bool has_value() const noexcept { return m_data.index() == 0; }
48 constexpr explicit operator bool() const noexcept { return has_value(); }
49
50 constexpr const ValueType& value() const LIFETIMEBOUND
51 {
52 assert(has_value());
53 return std::get<0>(m_data);
54 }
56 {
57 assert(has_value());
58 return std::get<0>(m_data);
59 }
60
61 template <class U>
62 ValueType value_or(U&& default_value) const&
63 {
64 return has_value() ? value() : std::forward<U>(default_value);
65 }
66 template <class U>
67 ValueType value_or(U&& default_value) &&
68 {
69 return has_value() ? std::move(value()) : std::forward<U>(default_value);
70 }
71
72 constexpr const E& error() const LIFETIMEBOUND
73 {
74 assert(!has_value());
75 return std::get<1>(m_data);
76 }
77 constexpr E& error() LIFETIMEBOUND
78 {
79 assert(!has_value());
80 return std::get<1>(m_data);
81 }
82
83 constexpr ValueType& operator*() LIFETIMEBOUND { return value(); }
84 constexpr const ValueType& operator*() const LIFETIMEBOUND { return value(); }
85
86 constexpr ValueType* operator->() LIFETIMEBOUND { return &value(); }
87 constexpr const ValueType* operator->() const LIFETIMEBOUND { return &value(); }
88};
89
90} // namespace util
91
92#endif // BITCOIN_UTIL_EXPECTED_H
#define LIFETIMEBOUND
Definition: attributes.h:16
The util::Expected class provides a standard way for low-level functions to return either error value...
Definition: expected.h:34
constexpr ValueType * operator->() LIFETIMEBOUND
Definition: expected.h:86
constexpr const ValueType & operator*() const LIFETIMEBOUND
Definition: expected.h:84
constexpr ValueType & value() LIFETIMEBOUND
Definition: expected.h:55
std::conditional_t< std::is_same_v< T, void >, std::monostate, T > ValueType
Definition: expected.h:36
constexpr const ValueType & value() const LIFETIMEBOUND
Definition: expected.h:50
constexpr E & error() LIFETIMEBOUND
Definition: expected.h:77
ValueType value_or(U &&default_value) &&
Definition: expected.h:67
std::variant< ValueType, E > m_data
Definition: expected.h:37
constexpr ValueType & operator*() LIFETIMEBOUND
Definition: expected.h:83
constexpr const ValueType * operator->() const LIFETIMEBOUND
Definition: expected.h:87
ValueType value_or(U &&default_value) const &
Definition: expected.h:62
constexpr const E & error() const LIFETIMEBOUND
Definition: expected.h:72
constexpr bool has_value() const noexcept
Definition: expected.h:47
constexpr Expected()
Definition: expected.h:40
constexpr Expected(Unexpected< Err > u)
Definition: expected.h:43
constexpr Expected(ValueType v)
Definition: expected.h:41
The util::Unexpected class represents an unexpected value stored in util::Expected.
Definition: expected.h:21
constexpr Unexpected(E e)
Definition: expected.h:23
#define T(expected, seed, data)
#define E
Definition: util_tests.cpp:546
assert(!tx.IsCoinBase())