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 xrpl {
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()) << "Unknown result code in metadata: " << value_;
48 // LCOV_EXCL_STOP
49 }
50
51 return std::to_string(value_);
52}
53
54template <>
57{
58 if (getFName() == sfTransactionResult)
59 {
60 std::string token, human;
61
62 if (transResultInfo(TER::fromInt(value_), token, human))
63 return token;
64
65 // LCOV_EXCL_START
66 JLOG(debugLog().error()) << "Unknown result code in metadata: " << value_;
67 // LCOV_EXCL_STOP
68 }
69
70 return value_;
71}
72
73//------------------------------------------------------------------------------
74
75template <>
77 : STInteger(name, sit.get16())
78{
79}
80
81template <>
84{
85 return STI_UINT16;
86}
87
88template <>
91{
92 if (getFName() == sfLedgerEntryType)
93 {
94 auto item = LedgerFormats::getInstance().findByType(safe_cast<LedgerEntryType>(value_));
95
96 if (item != nullptr)
97 return item->getName();
98 }
99
100 if (getFName() == sfTransactionType)
101 {
102 auto item = TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
103
104 if (item != nullptr)
105 return item->getName();
106 }
107
108 return std::to_string(value_);
109}
110
111template <>
113STUInt16::getJson(JsonOptions) const
114{
115 if (getFName() == sfLedgerEntryType)
116 {
117 auto item = LedgerFormats::getInstance().findByType(safe_cast<LedgerEntryType>(value_));
118
119 if (item != nullptr)
120 return item->getName();
121 }
122
123 if (getFName() == sfTransactionType)
124 {
125 auto item = TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
126
127 if (item != nullptr)
128 return item->getName();
129 }
130
131 return value_;
132}
133
134//------------------------------------------------------------------------------
135
136template <>
138 : STInteger(name, sit.get32())
139{
140}
141
142template <>
144STUInt32::getSType() const
145{
146 return STI_UINT32;
147}
148
149template <>
151STUInt32::getText() const
152{
153 if (getFName() == sfPermissionValue)
154 {
155 auto const permissionName = Permission::getInstance().getPermissionName(value_);
156 if (permissionName)
157 return *permissionName;
158 }
159 return std::to_string(value_);
160}
161
162template <>
164STUInt32::getJson(JsonOptions) const
165{
166 if (getFName() == sfPermissionValue)
167 {
168 auto const permissionName = Permission::getInstance().getPermissionName(value_);
169 if (permissionName)
170 return *permissionName;
171 }
172
173 return value_;
174}
175
176//------------------------------------------------------------------------------
177
178template <>
180 : STInteger(name, sit.get64())
181{
182}
183
184template <>
186STUInt64::getSType() const
187{
188 return STI_UINT64;
189}
190
191template <>
193STUInt64::getText() const
194{
195 return std::to_string(value_);
196}
197
198template <>
200STUInt64::getJson(JsonOptions) const
201{
202 auto convertToString = [](uint64_t const value, int const base) {
203 XRPL_ASSERT(base == 10 || base == 16, "xrpl::STUInt64::getJson : base 10 or 16");
204 std::string str(base == 10 ? 20 : 16, 0); // Allocate space depending on base
205 auto ret = std::to_chars(str.data(), str.data() + str.size(), value, base);
206 XRPL_ASSERT(ret.ec == std::errc(), "xrpl::STUInt64::getJson : to_chars succeeded");
207 str.resize(std::distance(str.data(), ret.ptr));
208 return str;
209 };
210
211 if (auto const& fName = getFName(); fName.shouldMeta(SField::sMD_BaseTen))
212 {
213 return convertToString(value_, 10); // Convert to base 10
214 }
215
216 return convertToString(value_, 16); // Convert to base 16
217}
218
219//------------------------------------------------------------------------------
220
221template <>
223 : STInteger(name, sit.get32())
224{
225}
226
227template <>
229STInt32::getSType() const
230{
231 return STI_INT32;
232}
233
234template <>
236STInt32::getText() const
237{
238 return std::to_string(value_);
239}
240
241template <>
243STInt32::getJson(JsonOptions) const
244{
245 return value_;
246}
247
248} // 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 const value) const
static Permission const & getInstance()
Identifies fields.
Definition SField.h:126
bool shouldMeta(int c) const
Definition SField.h:260
SField const & getFName() const
Definition STBase.cpp:125
SField const * fName
Definition STBase.h:116
value_type value() const noexcept
Definition STInteger.h:124
std::string getText() const override
Definition STInteger.cpp:37
SerializedTypeID getSType() const override
Definition STInteger.cpp:30
Json::Value getJson(JsonOptions) const override
Definition STInteger.cpp:56
Integer value_
Definition STInteger.h:15
STInteger(Integer v)
Definition STInteger.h:67
static constexpr TERSubset fromInt(int from)
Definition TER.h:413
static TxFormats const & getInstance()
Definition TxFormats.cpp:55
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:453
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition TER.cpp:228
SerializedTypeID
Definition SField.h:90
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)