rippled
Loading...
Searching...
No Matches
STCurrency.cpp
1#include <xrpl/basics/contract.h>
2#include <xrpl/json/json_value.h>
3#include <xrpl/protocol/SField.h>
4#include <xrpl/protocol/STBase.h>
5#include <xrpl/protocol/STCurrency.h>
6#include <xrpl/protocol/Serializer.h>
7#include <xrpl/protocol/UintTypes.h>
8
9#include <cstddef>
10#include <memory>
11#include <stdexcept>
12#include <string>
13#include <utility>
14
15namespace xrpl {
16
18{
19}
20
22{
23 currency_ = sit.get160();
24}
25
26STCurrency::STCurrency(SField const& name, Currency const& currency) : STBase{name}, currency_{currency}
27{
28}
29
32{
33 return STI_CURRENCY;
34}
35
38{
39 return to_string(currency_);
40}
41
47
48void
53
54bool
56{
57 STCurrency const* v = dynamic_cast<STCurrency const*>(&t);
58 return v && (*v == *this);
59}
60
61bool
63{
64 return isXRP(currency_);
65}
66
69{
70 return std::make_unique<STCurrency>(sit, name);
71}
72
73STBase*
74STCurrency::copy(std::size_t n, void* buf) const
75{
76 return emplace(n, buf, *this);
77}
78
79STBase*
81{
82 return emplace(n, buf, std::move(*this));
83}
84
86currencyFromJson(SField const& name, Json::Value const& v)
87{
88 if (!v.isString())
89 {
90 Throw<std::runtime_error>("currencyFromJson currency must be a string Json value");
91 }
92
93 auto const currency = to_currency(v.asString());
94 if (currency == badCurrency() || currency == noCurrency())
95 {
96 Throw<std::runtime_error>("currencyFromJson currency must be a valid currency");
97 }
98
99 return STCurrency{name, currency};
100}
101
102} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
bool isString() const
std::string asString() const
Returns the unquoted string value.
Identifies fields.
Definition SField.h:126
A type which can be exported to a well known binary format.
Definition STBase.h:115
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:213
std::string getText() const override
Json::Value getJson(JsonOptions) const override
bool isEquivalent(STBase const &t) const override
STBase * copy(std::size_t n, void *buf) const override
bool isDefault() const override
STBase * move(std::size_t n, void *buf) override
SerializedTypeID getSType() const override
Currency currency_
Definition STCurrency.h:14
STCurrency()=default
void add(Serializer &s) const override
static std::unique_ptr< STCurrency > construct(SerialIter &, SField const &name)
uint160 get160()
Definition Serializer.h:382
int addBitString(base_uint< Bits, Tag > const &v)
Definition Serializer.h:105
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
STCurrency currencyFromJson(SField const &name, Json::Value const &v)
bool isXRP(AccountID const &c)
Definition AccountID.h:70
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:597
SerializedTypeID
Definition SField.h:90
Currency const & noCurrency()
A placeholder for empty currencies.
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:62
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Note, should be treated as flags that can be | and &.
Definition STBase.h:17