Bitcoin ABC 0.30.5
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
43 template <typename FT>
44 friend bilingual_str ErrorString(const Result<FT> &result);
45
46public:
47 // constructor for void
48 Result() : m_variant{std::in_place_index_t<1>{}, std::monostate{}} {}
49 Result(T obj) : m_variant{std::in_place_index_t<1>{}, std::move(obj)} {}
51 : m_variant{std::in_place_index_t<0>{}, std::move(error.message)} {}
52
55 bool has_value() const noexcept { return m_variant.index() == 1; }
56 const T &value() const LIFETIMEBOUND {
57 assert(has_value());
58 return std::get<1>(m_variant);
59 }
61 assert(has_value());
62 return std::get<1>(m_variant);
63 }
64 template <class U> T value_or(U &&default_value) const & {
65 return has_value() ? value() : std::forward<U>(default_value);
66 }
67 template <class U> T value_or(U &&default_value) && {
68 return has_value() ? std::move(value())
69 : std::forward<U>(default_value);
70 }
71 explicit operator bool() const noexcept { return has_value(); }
72 const T *operator->() const LIFETIMEBOUND { return &value(); }
73 const T &operator*() const LIFETIMEBOUND { return value(); }
74 T *operator->() LIFETIMEBOUND { return &value(); }
75 T &operator*() LIFETIMEBOUND { return value(); }
76};
77
78template <typename T> bilingual_str ErrorString(const Result<T> &result) {
79 return result ? bilingual_str{} : std::get<0>(result.m_variant);
80}
81} // namespace util
82
83#endif // BITCOIN_UTIL_RESULT_H
#define LIFETIMEBOUND
Definition: attributes.h:16
T & value() LIFETIMEBOUND
Definition: result.h:60
T & operator*() LIFETIMEBOUND
Definition: result.h:75
T value_or(U &&default_value) const &
Definition: result.h:64
Result(Error error)
Definition: result.h:50
bool has_value() const noexcept
std::optional methods, so functions returning optional<T> can change to return Result<T> with minimal...
Definition: result.h:55
const T & operator*() const LIFETIMEBOUND
Definition: result.h:73
const T & value() const LIFETIMEBOUND
Definition: result.h:56
const T * operator->() const LIFETIMEBOUND
Definition: result.h:72
T value_or(U &&default_value) &&
Definition: result.h:67
Result(T obj)
Definition: result.h:49
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:74
std::variant< bilingual_str, T > m_variant
Definition: result.h:41
bool error(const char *fmt, const Args &...args)
Definition: logging.h:226
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:78
Bilingual messages:
Definition: translation.h:17
bilingual_str message
Definition: result.h:16
assert(!tx.IsCoinBase())