xrpld
Loading...
Searching...
No Matches
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>
20
21namespace xrpl {
22
23template <>
25 : STInteger(name, sit.get8())
26{
27}
28
29template <>
32{
33 return STI_UINT8;
34}
35
36template <>
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
55template <>
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
76template <>
78 : STInteger(name, sit.get16())
79{
80}
81
82template <>
85{
86 return STI_UINT16;
87}
88
89template <>
92{
93 if (getFName() == sfLedgerEntryType)
94 {
96
97 if (item != nullptr)
98 return item->getName();
99 }
100
101 if (getFName() == sfTransactionType)
102 {
104
105 if (item != nullptr)
106 return item->getName();
107 }
108
109 return std::to_string(value_);
110}
111
112template <>
115{
116 if (getFName() == sfLedgerEntryType)
117 {
119
120 if (item != nullptr)
121 return item->getName();
122 }
123
124 if (getFName() == sfTransactionType)
125 {
127
128 if (item != nullptr)
129 return item->getName();
130 }
131
132 return value_;
133}
134
135//------------------------------------------------------------------------------
136
137template <>
139 : STInteger(name, sit.get32())
140{
141}
142
143template <>
146{
147 return STI_UINT32;
148}
149
150template <>
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
163template <>
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
179template <>
181 : STInteger(name, sit.get64())
182{
183}
184
185template <>
188{
189 return STI_UINT64;
190}
191
192template <>
195{
196 return std::to_string(value_);
197}
198
199template <>
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
222template <>
224 : STInteger(name, sit.get32())
225{
226}
227
228template <>
231{
232 return STI_INT32;
233}
234
235template <>
238{
239 return std::to_string(value_);
240}
241
242template <>
245{
246 return value_;
247}
248
249} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
static LedgerFormats const & getInstance()
std::optional< std::string > getPermissionName(std::uint32_t value) const
static Permission const & getInstance()
Identifies fields.
Definition SField.h:130
bool shouldMeta(int c) const
Definition SField.h:263
static constexpr auto kSmdBaseTen
Definition SField.h:138
SField const & getFName() const
Definition STBase.cpp:126
value_type value() const noexcept
SerializedTypeID getSType() const override
Definition STInteger.cpp:31
json::Value getJson(JsonOptions) const override
Definition STInteger.cpp:57
operator std::uint64_t() const
STInteger(Integer v)
Definition STInteger.h:67
std::string getText() const override
Definition STInteger.cpp:38
static constexpr TERSubset fromInt(int from)
Definition TER.h:431
static TxFormats const & getInstance()
Definition TxFormats.cpp:57
T data(T... args)
T distance(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:399
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safeCast(Src s) noexcept
Definition safe_cast.h:21
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition TER.cpp:232
SerializedTypeID
Definition SField.h:93
T resize(T... args)
T size(T... args)
Note, should be treated as flags that can be | and &.
Definition STBase.h:17
T to_chars(T... args)
T to_string(T... args)