rippled
Loading...
Searching...
No Matches
STInteger.cpp
1#include <xrpl/basics/Log.h>
2#include <xrpl/basics/safe_cast.h>
3#include <xrpl/beast/utility/instrumentation.h>
4#include <xrpl/json/json_value.h>
5#include <xrpl/protocol/LedgerFormats.h>
6#include <xrpl/protocol/Permissions.h>
7#include <xrpl/protocol/SField.h>
8#include <xrpl/protocol/STBase.h>
9#include <xrpl/protocol/STInteger.h>
10#include <xrpl/protocol/Serializer.h>
11#include <xrpl/protocol/TER.h>
12#include <xrpl/protocol/TxFormats.h>
13
14#include <charconv>
15#include <cstdint>
16#include <iterator>
17#include <string>
18#include <system_error>
19
20namespace ripple {
21
22template <>
24 : STInteger(name, sit.get8())
25{
26}
27
28template <>
31{
32 return STI_UINT8;
33}
34
35template <>
38{
39 if (getFName() == sfTransactionResult)
40 {
41 std::string token, human;
42
43 if (transResultInfo(TER::fromInt(value_), token, human))
44 return human;
45
46 // LCOV_EXCL_START
47 JLOG(debugLog().error())
48 << "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())
68 << "Unknown result code in metadata: " << value_;
69 // LCOV_EXCL_STOP
70 }
71
72 return value_;
73}
74
75//------------------------------------------------------------------------------
76
77template <>
79 : STInteger(name, sit.get16())
80{
81}
82
83template <>
86{
87 return STI_UINT16;
88}
89
90template <>
93{
94 if (getFName() == sfLedgerEntryType)
95 {
97 safe_cast<LedgerEntryType>(value_));
98
99 if (item != nullptr)
100 return item->getName();
101 }
102
103 if (getFName() == sfTransactionType)
104 {
105 auto item =
106 TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
107
108 if (item != nullptr)
109 return item->getName();
110 }
111
112 return std::to_string(value_);
113}
114
115template <>
117STUInt16::getJson(JsonOptions) const
118{
119 if (getFName() == sfLedgerEntryType)
120 {
122 safe_cast<LedgerEntryType>(value_));
123
124 if (item != nullptr)
125 return item->getName();
126 }
127
128 if (getFName() == sfTransactionType)
129 {
130 auto item =
131 TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
132
133 if (item != nullptr)
134 return item->getName();
135 }
136
137 return value_;
138}
139
140//------------------------------------------------------------------------------
141
142template <>
144 : STInteger(name, sit.get32())
145{
146}
147
148template <>
150STUInt32::getSType() const
151{
152 return STI_UINT32;
153}
154
155template <>
157STUInt32::getText() const
158{
159 if (getFName() == sfPermissionValue)
160 {
161 auto const permissionName =
163 if (permissionName)
164 return *permissionName;
165 }
166 return std::to_string(value_);
167}
168
169template <>
171STUInt32::getJson(JsonOptions) const
172{
173 if (getFName() == sfPermissionValue)
174 {
175 auto const permissionName =
177 if (permissionName)
178 return *permissionName;
179 }
180
181 return value_;
182}
183
184//------------------------------------------------------------------------------
185
186template <>
188 : STInteger(name, sit.get64())
189{
190}
191
192template <>
194STUInt64::getSType() const
195{
196 return STI_UINT64;
197}
198
199template <>
201STUInt64::getText() const
202{
203 return std::to_string(value_);
204}
205
206template <>
208STUInt64::getJson(JsonOptions) const
209{
210 auto convertToString = [](uint64_t const value, int const base) {
211 XRPL_ASSERT(
212 base == 10 || base == 16,
213 "ripple::STUInt64::getJson : base 10 or 16");
214 std::string str(
215 base == 10 ? 20 : 16, 0); // Allocate space depending on base
216 auto ret =
217 std::to_chars(str.data(), str.data() + str.size(), value, base);
218 XRPL_ASSERT(
219 ret.ec == std::errc(),
220 "ripple::STUInt64::getJson : to_chars succeeded");
221 str.resize(std::distance(str.data(), ret.ptr));
222 return str;
223 };
224
225 if (auto const& fName = getFName(); fName.shouldMeta(SField::sMD_BaseTen))
226 {
227 return convertToString(value_, 10); // Convert to base 10
228 }
229
230 return convertToString(value_, 16); // Convert to base 16
231}
232
233//------------------------------------------------------------------------------
234
235template <>
237 : STInteger(name, sit.get32())
238{
239}
240
241template <>
243STInt32::getSType() const
244{
245 return STI_INT32;
246}
247
248template <>
250STInt32::getText() const
251{
252 return std::to_string(value_);
253}
254
255template <>
257STInt32::getJson(JsonOptions) const
258{
259 return value_;
260}
261
262} // namespace ripple
Represents a JSON value.
Definition json_value.h:131
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 const value) const
static Permission const & getInstance()
Identifies fields.
Definition SField.h:127
bool shouldMeta(int c) const
Definition SField.h:259
SField const & getFName() const
Definition STBase.cpp:124
SField const * fName
Definition STBase.h:117
STInteger(Integer v)
Definition STInteger.h:68
std::string getText() const override
Definition STInteger.cpp:37
value_type value() const noexcept
Definition STInteger.h:129
Json::Value getJson(JsonOptions) const override
Definition STInteger.cpp:57
SerializedTypeID getSType() const override
Definition STInteger.cpp:30
static constexpr TERSubset fromInt(int from)
Definition TER.h:414
static TxFormats const & getInstance()
Definition TxFormats.cpp:52
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:6
SerializedTypeID
Definition SField.h:91
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:457
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition TER.cpp:230
T resize(T... args)
T size(T... args)
Note, should be treated as flags that can be | and &.
Definition STBase.h:18
T to_chars(T... args)
T to_string(T... args)