xrpld
Loading...
Searching...
No Matches
EitherAmount.h
1#pragma once
2
3#include <xrpl/basics/contract.h>
4#include <xrpl/protocol/Concepts.h>
5#include <xrpl/protocol/IOUAmount.h>
6#include <xrpl/protocol/MPTAmount.h> // IWYU pragma: keep
7#include <xrpl/protocol/XRPAmount.h>
8
9#include <ostream> // IWYU pragma: keep
10#include <stdexcept>
11#include <variant>
12
13namespace xrpl {
14
16{
18
19 explicit EitherAmount() = default;
20
21 template <StepAmount T>
22 explicit EitherAmount(T const& a) : amount(a)
23 {
24 }
25
26 template <StepAmount T>
27 [[nodiscard]] bool
28 holds() const
29 {
31 }
32
33 template <StepAmount T>
34 [[nodiscard]] T const&
35 get() const
36 {
37 if (!holds<T>())
38 Throw<std::logic_error>("EitherAmount doesn't hold requested amount");
39 return std::get<T>(amount);
40 }
41
42#ifndef NDEBUG
44 operator<<(std::ostream& stream, EitherAmount const& amt)
45 {
46 std::visit([&]<StepAmount T>(T const& a) { stream << to_string(a); }, amt.amount);
47 return stream;
48 }
49#endif
50};
51
52template <StepAmount T>
53T const&
54get(EitherAmount const& amt)
55{
56 return amt.get<T>();
57}
58
59} // namespace xrpl
T holds_alternative(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
std::variant< XRPAmount, IOUAmount, MPTAmount > amount
EitherAmount(T const &a)
bool holds() const
T const & get() const
EitherAmount()=default
friend std::ostream & operator<<(std::ostream &stream, EitherAmount const &amt)
T visit(T... args)