xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
protocol
STInteger.cpp
1
#include <xrpl/protocol/STInteger.h>
2
3
#include <xrpl/basics/Log.h>
4
#include <xrpl/basics/safe_cast.h>
5
#include <xrpl/beast/utility/instrumentation.h>
6
#include <xrpl/json/json_value.h>
7
#include <xrpl/protocol/LedgerFormats.h>
8
#include <xrpl/protocol/Permissions.h>
9
#include <xrpl/protocol/SField.h>
10
#include <xrpl/protocol/STBase.h>
11
#include <xrpl/protocol/Serializer.h>
12
#include <xrpl/protocol/TER.h>
13
#include <xrpl/protocol/TxFormats.h>
14
15
#include <
charconv
>
16
#include <
cstdint
>
17
#include <
iterator
>
18
#include <
string
>
19
#include <
system_error
>
// IWYU pragma: keep
20
21
namespace
xrpl
{
22
23
template
<>
24
STInteger<unsigned char>::STInteger
(
SerialIter
& sit,
SField
const
& name)
25
:
STInteger
(name, sit.get8())
26
{
27
}
28
29
template
<>
30
SerializedTypeID
31
STUInt8::getSType
()
const
32
{
33
return
STI_UINT8;
34
}
35
36
template
<>
37
std::string
38
STUInt8::getText
()
const
39
{
40
if
(
getFName
() == sfTransactionResult)
41
{
42
std::string
token, human;
43
44
if
(
transResultInfo
(
TER::fromInt
(
value_
), token, human))
45
return
human;
46
47
// LCOV_EXCL_START
48
JLOG(
debugLog
().error()) <<
"Unknown result code in metadata: "
<<
value_
;
49
// LCOV_EXCL_STOP
50
}
51
52
return
std::to_string
(
value_
);
53
}
54
55
template
<>
56
json::Value
57
STUInt8::getJson
(
JsonOptions
)
const
58
{
59
if
(
getFName
() == sfTransactionResult)
60
{
61
std::string
token, human;
62
63
if
(
transResultInfo
(
TER::fromInt
(
value_
), token, human))
64
return
token;
65
66
// LCOV_EXCL_START
67
JLOG(
debugLog
().error()) <<
"Unknown result code in metadata: "
<<
value_
;
68
// LCOV_EXCL_STOP
69
}
70
71
return
value_
;
72
}
73
74
//------------------------------------------------------------------------------
75
76
template
<>
77
STInteger<std::uint16_t>::STInteger
(
SerialIter
& sit,
SField
const
& name)
78
:
STInteger
(name, sit.get16())
79
{
80
}
81
82
template
<>
83
SerializedTypeID
84
STUInt16::getSType
()
const
85
{
86
return
STI_UINT16;
87
}
88
89
template
<>
90
std::string
91
STUInt16::getText
()
const
92
{
93
if
(
getFName
() == sfLedgerEntryType)
94
{
95
auto
item =
LedgerFormats::getInstance
().
findByType
(
safeCast<LedgerEntryType>
(
value_
));
96
97
if
(item !=
nullptr
)
98
return
item->getName();
99
}
100
101
if
(
getFName
() == sfTransactionType)
102
{
103
auto
item =
TxFormats::getInstance
().
findByType
(
safeCast<TxType>
(
value_
));
104
105
if
(item !=
nullptr
)
106
return
item->getName();
107
}
108
109
return
std::to_string
(
value_
);
110
}
111
112
template
<>
113
json::Value
114
STUInt16::getJson
(
JsonOptions
)
const
115
{
116
if
(
getFName
() == sfLedgerEntryType)
117
{
118
auto
item =
LedgerFormats::getInstance
().
findByType
(
safeCast<LedgerEntryType>
(
value_
));
119
120
if
(item !=
nullptr
)
121
return
item->getName();
122
}
123
124
if
(
getFName
() == sfTransactionType)
125
{
126
auto
item =
TxFormats::getInstance
().
findByType
(
safeCast<TxType>
(
value_
));
127
128
if
(item !=
nullptr
)
129
return
item->getName();
130
}
131
132
return
value_
;
133
}
134
135
//------------------------------------------------------------------------------
136
137
template
<>
138
STInteger<std::uint32_t>::STInteger
(
SerialIter
& sit,
SField
const
& name)
139
:
STInteger
(name, sit.get32())
140
{
141
}
142
143
template
<>
144
SerializedTypeID
145
STUInt32::getSType
()
const
146
{
147
return
STI_UINT32;
148
}
149
150
template
<>
151
std::string
152
STUInt32::getText
()
const
153
{
154
if
(
getFName
() == sfPermissionValue)
155
{
156
auto
const
permissionName =
Permission::getInstance
().
getPermissionName
(
value_
);
157
if
(permissionName)
158
return
*permissionName;
159
}
160
return
std::to_string
(
value_
);
161
}
162
163
template
<>
164
json::Value
165
STUInt32::getJson
(
JsonOptions
)
const
166
{
167
if
(
getFName
() == sfPermissionValue)
168
{
169
auto
const
permissionName =
Permission::getInstance
().
getPermissionName
(
value_
);
170
if
(permissionName)
171
return
*permissionName;
172
}
173
174
return
value_
;
175
}
176
177
//------------------------------------------------------------------------------
178
179
template
<>
180
STInteger<std::uint64_t>::STInteger
(
SerialIter
& sit,
SField
const
& name)
181
:
STInteger
(name, sit.get64())
182
{
183
}
184
185
template
<>
186
SerializedTypeID
187
STUInt64::getSType
()
const
188
{
189
return
STI_UINT64;
190
}
191
192
template
<>
193
std::string
194
STUInt64::getText
()
const
195
{
196
return
std::to_string
(
value_
);
197
}
198
199
template
<>
200
json::Value
201
STUInt64::getJson
(
JsonOptions
)
const
202
{
203
auto
convertToString = [](
uint64_t
const
value
,
int
const
base) {
204
XRPL_ASSERT(base == 10 || base == 16,
"xrpl::STUInt64::getJson : base 10 or 16"
);
205
std::string
str(base == 10 ? 20 : 16, 0);
// Allocate space depending on base
206
auto
ret =
std::to_chars
(str.
data
(), str.
data
() + str.
size
(),
value
, base);
207
XRPL_ASSERT(ret.ec ==
std::errc
(),
"xrpl::STUInt64::getJson : to_chars succeeded"
);
208
str.
resize
(
std::distance
(str.
data
(), ret.ptr));
209
return
str;
210
};
211
212
if
(
auto
const
& fName =
getFName
(); fName.
shouldMeta
(
SField::kSmdBaseTen
))
213
{
214
return
convertToString(
value_
, 10);
// Convert to base 10
215
}
216
217
return
convertToString(
value_
, 16);
// Convert to base 16
218
}
219
220
//------------------------------------------------------------------------------
221
222
template
<>
223
STInteger<std::int32_t>::STInteger
(
SerialIter
& sit,
SField
const
& name)
224
:
STInteger
(name, sit.get32())
225
{
226
}
227
228
template
<>
229
SerializedTypeID
230
STInt32::getSType
()
const
231
{
232
return
STI_INT32;
233
}
234
235
template
<>
236
std::string
237
STInt32::getText
()
const
238
{
239
return
std::to_string
(
value_
);
240
}
241
242
template
<>
243
json::Value
244
STInt32::getJson
(
JsonOptions
)
const
245
{
246
return
value_
;
247
}
248
249
}
// namespace xrpl
std::string
charconv
json::Value
Represents a JSON value.
Definition
json_value.h:117
xrpl::KnownFormats::findByType
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
Definition
KnownFormats.h:124
xrpl::LedgerFormats::getInstance
static LedgerFormats const & getInstance()
Definition
LedgerFormats.cpp:43
xrpl::Permission::getPermissionName
std::optional< std::string > getPermissionName(std::uint32_t value) const
Definition
Permissions.cpp:157
xrpl::Permission::getInstance
static Permission const & getInstance()
Definition
Permissions.cpp:150
xrpl::SField
Identifies fields.
Definition
SField.h:132
xrpl::SField::shouldMeta
bool shouldMeta(int c) const
Definition
SField.h:265
xrpl::SField::kSmdBaseTen
static constexpr auto kSmdBaseTen
Definition
SField.h:140
xrpl::STBase::getFName
SField const & getFName() const
Definition
STBase.cpp:120
xrpl::STInteger< std::uint64_t >::value
value_type value() const noexcept
xrpl::STInteger< unsigned char >::getSType
SerializedTypeID getSType() const override
Definition
STInteger.cpp:31
xrpl::STInteger< unsigned char >::getJson
json::Value getJson(JsonOptions) const override
Definition
STInteger.cpp:57
xrpl::STInteger< unsigned char >::value_
unsigned char value_
Definition
STInteger.h:23
xrpl::STInteger< std::uint64_t >::uint64_t
operator std::uint64_t() const
xrpl::STInteger::STInteger
STInteger(Integer v)
Definition
STInteger.h:75
xrpl::STInteger< unsigned char >::getText
std::string getText() const override
Definition
STInteger.cpp:38
xrpl::SerialIter
Definition
Serializer.h:478
xrpl::TERSubset< CanCvtToTER >::fromInt
static constexpr TERSubset fromInt(int from)
Definition
TER.h:444
xrpl::TxFormats::getInstance
static TxFormats const & getInstance()
Definition
TxFormats.cpp:60
cstdint
std::string::data
T data(T... args)
std::distance
T distance(T... args)
std::errc
iterator
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition
Log.cpp:399
xrpl::transResultInfo
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition
TER.cpp:242
xrpl::safeCast
constexpr Dest safeCast(Src s) noexcept
Definition
safe_cast.h:21
xrpl::SerializedTypeID
SerializedTypeID
Definition
SField.h:94
std::string::resize
T resize(T... args)
std::string::size
T size(T... args)
string
xrpl::JsonOptions
Note, should be treated as flags that can be | and &.
Definition
STBase.h:22
system_error
std::to_chars
T to_chars(T... args)
std::to_string
T to_string(T... args)
Generated by
1.17.0