Bitcoin ABC 0.31.6
P2P Digital Currency
result.h
Go to the documentation of this file.
1// Copyright (c) 2022 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_UTIL_RESULT_H
6#define BITCOIN_UTIL_RESULT_H
7
8#include <attributes.h>
9#include <util/translation.h>
10
11#include <variant>
12
13namespace util {
14
15struct Error {
17};
18
34// This can be replaced by
35// template<class M> using Result = std::expected<M, Error>;
36// after C++23 is enforced.
37template <class M> class Result {
38private:
39 using T = std::conditional_t<std::is_same_v<M, void>, std::monostate, M>;
40
41 std::variant<bilingual_str, T> m_variant;
42
44 Result(const Result &) = delete;
45
50 Result &operator=(const Result &) = delete;
51 Result &operator=(Result &&) = delete;
52
53 template <typename FT>
54 friend bilingual_str ErrorString(const Result<FT> &result);
55
56public:
57 // constructor for void
58 Result() : m_variant{std::in_place_index_t<1>{}, std::monostate{}} {}
59 Result(T obj) : m_variant{std::in_place_index_t<1>{}, std::move(obj)} {}
61 : m_variant{std::in_place_index_t<0>{}, std::move(error.message)} {}
62 Result(Result &&) = default;
63 ~Result() = default;
64
67 bool has_value() const noexcept { return m_variant.index() == 1; }
68 const T &value() const LIFETIMEBOUND {
69 assert(has_value());
70 return std::get<1>(m_variant);
71 }
73 assert(has_value());
74 return std::get<1>(m_variant);
75 }
76 template <class U> T value_or(U &&default_value) const & {
77 return has_value() ? value() : std::forward<U>(default_value);
78 }
79 template <class U> T value_or(U &&default_value) && {
80 return has_value() ? std::move(value())
81 : std::forward<U>(default_value);
82 }
83 explicit operator bool() const noexcept { return has_value(); }
84 const T *operator->() const LIFETIMEBOUND { return &value(); }
85 const T &operator*() const LIFETIMEBOUND { return value(); }
86 T *operator->() LIFETIMEBOUND { return &value(); }
87 T &operator*() LIFETIMEBOUND { return value(); }
88};
89
90template <typename T> bilingual_str ErrorString(const Result<T> &result) {
91 return result ? bilingual_str{} : std::get<0>(result.m_variant);
92}
93} // namespace util
94
95#endif // BITCOIN_UTIL_RESULT_H
#define LIFETIMEBOUND
Definition: attributes.h:16
Result & operator=(const Result &)=delete
Disallow operator= to avoid confusion in the future when the Result class gains support for richer er...
T & value() LIFETIMEBOUND
Definition: result.h:72
Result & operator=(Result &&)=delete
T & operator*() LIFETIMEBOUND
Definition: result.h:87
T value_or(U &&default_value) const &
Definition: result.h:76
Result(Error error)
Definition: result.h:60
bool has_value() const noexcept
std::optional methods, so functions returning optional<T> can change to return Result<T> with minimal...
Definition: result.h:67
const T & operator*() const LIFETIMEBOUND
Definition: result.h:85
const T & value() const LIFETIMEBOUND
Definition: result.h:68
const T * operator->() const LIFETIMEBOUND
Definition: result.h:84
Result(const Result &)=delete
Disallow copy constructor, require Result to be moved for efficiency.
T value_or(U &&default_value) &&
Definition: result.h:79
Result(T obj)
Definition: result.h:59
friend bilingual_str ErrorString(const Result< FT > &result)
std::conditional_t< std::is_same_v< M, void >, std::monostate, M > T
Definition: result.h:39
T * operator->() LIFETIMEBOUND
Definition: result.h:86
std::variant< bilingual_str, T > m_variant
Definition: result.h:41
Result(Result &&)=default
~Result()=default
bool error(const char *fmt, const Args &...args)
Definition: logging.h:263
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
Definition: any.h:10
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:90
Bilingual messages:
Definition: translation.h:17
bilingual_str message
Definition: result.h:16
assert(!tx.IsCoinBase())