xrpld
Loading...
Searching...
No Matches
EitherAmount.h
1#pragma once
2
3#include <xrpl/protocol/Concepts.h>
4#include <xrpl/protocol/IOUAmount.h>
5#include <xrpl/protocol/STAmount.h>
6#include <xrpl/protocol/XRPAmount.h>
7
8namespace xrpl {
9
11{
13
14 explicit EitherAmount() = default;
15
16 template <StepAmount T>
17 explicit EitherAmount(T const& a) : amount(a)
18 {
19 }
20
21 template <StepAmount T>
22 [[nodiscard]] bool
23 holds() const
24 {
26 }
27
28 template <StepAmount T>
29 [[nodiscard]] T const&
30 get() const
31 {
32 if (!holds<T>())
33 Throw<std::logic_error>("EitherAmount doesn't hold requested amount");
34 return std::get<T>(amount);
35 }
36
37#ifndef NDEBUG
39 operator<<(std::ostream& stream, EitherAmount const& amt)
40 {
41 std::visit([&]<StepAmount T>(T const& a) { stream << to_string(a); }, amt.amount);
42 return stream;
43 }
44#endif
45};
46
47template <StepAmount T>
48T const&
49get(EitherAmount const& amt)
50{
51 return amt.get<T>();
52}
53
54} // 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:633
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
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)