xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
protocol
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
16
namespace
xrpl
{
17
18
STCurrency::STCurrency
(
SField
const
& name) :
STBase
{name}
19
{
20
}
21
22
STCurrency::STCurrency
(
SerialIter
& sit,
SField
const
& name) :
STBase
{name}
23
{
24
currency_
= sit.
get160
();
25
}
26
27
STCurrency::STCurrency
(
SField
const
& name,
Currency
const
&
currency
)
28
:
STBase
{name},
currency_
{
currency
}
29
{
30
}
31
32
SerializedTypeID
33
STCurrency::getSType
()
const
34
{
35
return
STI_CURRENCY;
36
}
37
38
std::string
39
STCurrency::getText
()
const
40
{
41
return
to_string
(
currency_
);
42
}
43
44
json::Value
45
STCurrency::getJson
(
JsonOptions
)
const
46
{
47
return
to_string
(
currency_
);
48
}
49
50
void
51
STCurrency::add
(
Serializer
& s)
const
52
{
53
s.
addBitString
(
currency_
);
54
}
55
56
bool
57
STCurrency::isEquivalent
(
STBase
const
& t)
const
58
{
59
auto
const
* v =
dynamic_cast<
STCurrency
const*
>
(&t);
60
return
(v !=
nullptr
) && (*v == *
this
);
61
}
62
63
bool
64
STCurrency::isDefault
()
const
65
{
66
return
isXRP
(
currency_
);
67
}
68
69
std::unique_ptr<STCurrency>
70
STCurrency::construct
(
SerialIter
& sit,
SField
const
& name)
71
{
72
return
std::make_unique<STCurrency>
(sit, name);
73
}
74
75
STBase
*
76
STCurrency::copy
(
std::size_t
n,
void
* buf)
const
77
{
78
return
emplace
(n, buf, *
this
);
79
}
80
81
STBase
*
82
STCurrency::move
(
std::size_t
n,
void
* buf)
83
{
84
return
emplace
(n, buf, std::move(*
this
));
85
}
86
87
STCurrency
88
currencyFromJson
(
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
std::string
json::Value
Represents a JSON value.
Definition
json_value.h:117
json::Value::isString
bool isString() const
Definition
json_value.cpp:1134
json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition
json_value.cpp:494
xrpl::SField
Identifies fields.
Definition
SField.h:132
xrpl::STBase
A type which can be exported to a well known binary format.
Definition
STBase.h:129
xrpl::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition
STBase.h:226
xrpl::STBase::STBase
STBase()
Definition
STBase.cpp:15
xrpl::STCurrency
Definition
STCurrency.h:16
xrpl::STCurrency::getText
std::string getText() const override
Definition
STCurrency.cpp:39
xrpl::STCurrency::isEquivalent
bool isEquivalent(STBase const &t) const override
Definition
STCurrency.cpp:57
xrpl::STCurrency::currency
Currency const & currency() const
Definition
STCurrency.h:73
xrpl::STCurrency::copy
STBase * copy(std::size_t n, void *buf) const override
Definition
STCurrency.cpp:76
xrpl::STCurrency::isDefault
bool isDefault() const override
Definition
STCurrency.cpp:64
xrpl::STCurrency::move
STBase * move(std::size_t n, void *buf) override
Definition
STCurrency.cpp:82
xrpl::STCurrency::getSType
SerializedTypeID getSType() const override
Definition
STCurrency.cpp:33
xrpl::STCurrency::currency_
Currency currency_
Definition
STCurrency.h:18
xrpl::STCurrency::STCurrency
STCurrency()=default
xrpl::STCurrency::add
void add(Serializer &s) const override
Definition
STCurrency.cpp:51
xrpl::STCurrency::construct
static std::unique_ptr< STCurrency > construct(SerialIter &, SField const &name)
Definition
STCurrency.cpp:70
xrpl::STCurrency::getJson
json::Value getJson(JsonOptions) const override
Definition
STCurrency.cpp:45
xrpl::SerialIter
Definition
Serializer.h:478
xrpl::SerialIter::get160
UInt160 get160()
Definition
Serializer.h:541
xrpl::Serializer
Definition
Serializer.h:23
xrpl::Serializer::addBitString
int addBitString(BaseUInt< Bits, Tag > const &v)
Definition
Serializer.h:202
cstddef
std::make_unique
T make_unique(T... args)
memory
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::isXRP
bool isXRP(AccountID const &c)
Definition
AccountID.h:84
xrpl::currencyFromJson
STCurrency currencyFromJson(SField const &name, json::Value const &v)
Definition
STCurrency.cpp:88
xrpl::Currency
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition
UintTypes.h:42
xrpl::toCurrency
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition
UintTypes.cpp:65
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:657
xrpl::SerializedTypeID
SerializedTypeID
Definition
SField.h:94
xrpl::noCurrency
Currency const & noCurrency()
A placeholder for empty currencies.
Definition
UintTypes.cpp:106
xrpl::badCurrency
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Definition
UintTypes.cpp:113
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
std::size_t
stdexcept
string
xrpl::JsonOptions
Note, should be treated as flags that can be | and &.
Definition
STBase.h:22
std::unique_ptr
utility
Generated by
1.17.0