Bitcoin Core  24.99.0
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 
13 namespace util {
14 
15 struct Error {
17 };
18 
34 template <class T>
35 class Result
36 {
37 private:
38  std::variant<bilingual_str, T> m_variant;
39 
40  template <typename FT>
41  friend bilingual_str ErrorString(const Result<FT>& result);
42 
43 public:
44  Result(T obj) : m_variant{std::in_place_index_t<1>{}, std::move(obj)} {}
45  Result(Error error) : m_variant{std::in_place_index_t<0>{}, std::move(error.message)} {}
46 
49  bool has_value() const noexcept { return m_variant.index() == 1; }
50  const T& value() const LIFETIMEBOUND
51  {
52  assert(has_value());
53  return std::get<1>(m_variant);
54  }
56  {
57  assert(has_value());
58  return std::get<1>(m_variant);
59  }
60  template <class U>
61  T value_or(U&& default_value) const&
62  {
63  return has_value() ? value() : std::forward<U>(default_value);
64  }
65  template <class U>
66  T value_or(U&& default_value) &&
67  {
68  return has_value() ? std::move(value()) : std::forward<U>(default_value);
69  }
70  explicit operator bool() const noexcept { return has_value(); }
71  const T* operator->() const LIFETIMEBOUND { return &value(); }
72  const T& operator*() const LIFETIMEBOUND { return value(); }
73  T* operator->() LIFETIMEBOUND { return &value(); }
74  T& operator*() LIFETIMEBOUND { return value(); }
75 };
76 
77 template <typename T>
79 {
80  return result ? bilingual_str{} : std::get<0>(result.m_variant);
81 }
82 } // namespace util
83 
84 #endif // BITCOIN_UTIL_RESULT_H
#define LIFETIMEBOUND
Definition: attributes.h:16
const T & value() const LIFETIMEBOUND
Definition: result.h:50
const T * operator->() const LIFETIMEBOUND
Definition: result.h:71
Result(Error error)
Definition: result.h:45
T & value() LIFETIMEBOUND
Definition: result.h:55
T * operator->() LIFETIMEBOUND
Definition: result.h:73
const T & operator*() const LIFETIMEBOUND
Definition: result.h:72
bool has_value() const noexcept
std::optional methods, so functions returning optional<T> can change to return Result<T> with minimal...
Definition: result.h:49
friend bilingual_str ErrorString(const Result< FT > &result)
Result(T obj)
Definition: result.h:44
T value_or(U &&default_value) &&
Definition: result.h:66
T & operator*() LIFETIMEBOUND
Definition: result.h:74
T value_or(U &&default_value) const &
Definition: result.h:61
std::variant< bilingual_str, T > m_variant
Definition: result.h:38
#define T(expected, seed, data)
Definition: overloaded.h:8
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:78
Bilingual messages:
Definition: translation.h:18
bilingual_str message
Definition: result.h:16
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
assert(!tx.IsCoinBase())