xrpld
Loading...
Searching...
No Matches
STCurrency.cpp
1#include <xrpl/protocol/STCurrency.h>
2
3#include <xrpl/basics/contract.h>
4#include <xrpl/json/json_value.h>
5#include <xrpl/protocol/SField.h>
6#include <xrpl/protocol/STBase.h>
7#include <xrpl/protocol/Serializer.h>
8#include <xrpl/protocol/UintTypes.h>
9
10#include <cstddef>
11#include <memory>
12#include <stdexcept>
13#include <string>
14#include <utility>
15
16namespace xrpl {
17
19{
20}
21
23{
24 currency_ = sit.get160();
25}
26
28 : STBase{name}, currency_{currency}
29{
30}
31
34{
35 return STI_CURRENCY;
36}
37
40{
41 return to_string(currency_);
42}
43
49
50void
55
56bool
58{
59 STCurrency const* v = dynamic_cast<STCurrency const*>(&t);
60 return (v != nullptr) && (*v == *this);
61}
62
63bool
65{
66 return isXRP(currency_);
67}
68
71{
72 return std::make_unique<STCurrency>(sit, name);
73}
74
75STBase*
76STCurrency::copy(std::size_t n, void* buf) const
77{
78 return emplace(n, buf, *this);
79}
80
81STBase*
83{
84 return emplace(n, buf, std::move(*this));
85}
86
88currencyFromJson(SField const& name, json::Value const& v)
89{
90 if (!v.isString())
91 {
92 Throw<std::runtime_error>("currencyFromJson currency must be a string Json value");
93 }
94
95 auto const currency = toCurrency(v.asString());
96 if (currency == badCurrency() || currency == noCurrency())
97 {
98 Throw<std::runtime_error>("currencyFromJson currency must be a valid currency");
99 }
100
101 return STCurrency{name, currency};
102}
103
104} // 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:130
A type which can be exported to a well known binary format.
Definition STBase.h:117
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:215
std::string getText() const override
bool isEquivalent(STBase const &t) const override
Currency const & currency() const
Definition STCurrency.h:69
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)
json::Value getJson(JsonOptions) const override
uint160 get160()
Definition Serializer.h:382
int addBitString(BaseUInt< Bits, Tag > const &v)
Definition Serializer.h:105
T make_unique(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool isXRP(AccountID const &c)
Definition AccountID.h:70
STCurrency currencyFromJson(SField const &name, json::Value const &v)
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:36
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
SerializedTypeID
Definition SField.h:93
Currency const & noCurrency()
A placeholder for empty currencies.
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
Note, should be treated as flags that can be | and &.
Definition STBase.h:17