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 ripple {
16
18{
19}
20
22{
23 currency_ = sit.get160();
24}
25
26STCurrency::STCurrency(SField const& name, Currency const& currency)
27 : STBase{name}, currency_{currency}
28{
29}
30
33{
34 return STI_CURRENCY;
35}
36
39{
40 return to_string(currency_);
41}
42
48
49void
54
55bool
57{
58 STCurrency const* v = dynamic_cast<STCurrency const*>(&t);
59 return v && (*v == *this);
60}
61
62bool
64{
65 return isXRP(currency_);
66}
67
70{
71 return std::make_unique<STCurrency>(sit, name);
72}
73
74STBase*
75STCurrency::copy(std::size_t n, void* buf) const
76{
77 return emplace(n, buf, *this);
78}
79
80STBase*
82{
83 return emplace(n, buf, std::move(*this));
84}
85
87currencyFromJson(SField const& name, Json::Value const& v)
88{
89 if (!v.isString())
90 {
91 Throw<std::runtime_error>(
92 "currencyFromJson currency must be a string Json value");
93 }
94
95 auto const currency = to_currency(v.asString());
96 if (currency == badCurrency() || currency == noCurrency())
97 {
98 Throw<std::runtime_error>(
99 "currencyFromJson currency must be a valid currency");
100 }
101
102 return STCurrency{name, currency};
103}
104
105} // namespace ripple
Represents a JSON value.
Definition json_value.h:131
bool isString() const
std::string asString() const
Returns the unquoted string value.
Identifies fields.
Definition SField.h:127
A type which can be exported to a well known binary format.
Definition STBase.h:116
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:214
bool isEquivalent(STBase const &t) const override
bool isDefault() const override
void add(Serializer &s) const override
SerializedTypeID getSType() const override
Json::Value getJson(JsonOptions) const override
STCurrency()=default
std::string getText() const override
static std::unique_ptr< STCurrency > construct(SerialIter &, SField const &name)
STBase * move(std::size_t n, void *buf) override
STBase * copy(std::size_t n, void *buf) const override
int addBitString(base_uint< Bits, Tag > const &v)
Definition Serializer.h:112
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
bool isXRP(AccountID const &c)
Definition AccountID.h:71
STCurrency currencyFromJson(SField const &name, Json::Value const &v)
SerializedTypeID
Definition SField.h:91
Currency const & noCurrency()
A placeholder for empty currencies.
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
Note, should be treated as flags that can be | and &.
Definition STBase.h:18