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 <>
23STInteger<unsigned char>::STInteger(SerialIter& sit, SField const& name) : STInteger(name, sit.get8())
24{
25}
26
27template <>
30{
31 return STI_UINT8;
32}
33
34template <>
37{
38 if (getFName() == sfTransactionResult)
39 {
40 std::string token, human;
41
42 if (transResultInfo(TER::fromInt(value_), token, human))
43 return human;
44
45 // LCOV_EXCL_START
46 JLOG(debugLog().error()) << "Unknown result code in metadata: " << value_;
47 // LCOV_EXCL_STOP
48 }
49
50 return std::to_string(value_);
51}
52
53template <>
56{
57 if (getFName() == sfTransactionResult)
58 {
59 std::string token, human;
60
61 if (transResultInfo(TER::fromInt(value_), token, human))
62 return token;
63
64 // LCOV_EXCL_START
65 JLOG(debugLog().error()) << "Unknown result code in metadata: " << value_;
66 // LCOV_EXCL_STOP
67 }
68
69 return value_;
70}
71
72//------------------------------------------------------------------------------
73
74template <>
75STInteger<std::uint16_t>::STInteger(SerialIter& sit, SField const& name) : STInteger(name, sit.get16())
76{
77}
78
79template <>
82{
83 return STI_UINT16;
84}
85
86template <>
89{
90 if (getFName() == sfLedgerEntryType)
91 {
92 auto item = LedgerFormats::getInstance().findByType(safe_cast<LedgerEntryType>(value_));
93
94 if (item != nullptr)
95 return item->getName();
96 }
97
98 if (getFName() == sfTransactionType)
99 {
100 auto item = TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
101
102 if (item != nullptr)
103 return item->getName();
104 }
105
106 return std::to_string(value_);
107}
108
109template <>
111STUInt16::getJson(JsonOptions) const
112{
113 if (getFName() == sfLedgerEntryType)
114 {
115 auto item = LedgerFormats::getInstance().findByType(safe_cast<LedgerEntryType>(value_));
116
117 if (item != nullptr)
118 return item->getName();
119 }
120
121 if (getFName() == sfTransactionType)
122 {
123 auto item = TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
124
125 if (item != nullptr)
126 return item->getName();
127 }
128
129 return value_;
130}
131
132//------------------------------------------------------------------------------
133
134template <>
135STInteger<std::uint32_t>::STInteger(SerialIter& sit, SField const& name) : STInteger(name, sit.get32())
136{
137}
138
139template <>
141STUInt32::getSType() const
142{
143 return STI_UINT32;
144}
145
146template <>
148STUInt32::getText() const
149{
150 if (getFName() == sfPermissionValue)
151 {
152 auto const permissionName = Permission::getInstance().getPermissionName(value_);
153 if (permissionName)
154 return *permissionName;
155 }
156 return std::to_string(value_);
157}
158
159template <>
161STUInt32::getJson(JsonOptions) const
162{
163 if (getFName() == sfPermissionValue)
164 {
165 auto const permissionName = Permission::getInstance().getPermissionName(value_);
166 if (permissionName)
167 return *permissionName;
168 }
169
170 return value_;
171}
172
173//------------------------------------------------------------------------------
174
175template <>
176STInteger<std::uint64_t>::STInteger(SerialIter& sit, SField const& name) : STInteger(name, sit.get64())
177{
178}
179
180template <>
182STUInt64::getSType() const
183{
184 return STI_UINT64;
185}
186
187template <>
189STUInt64::getText() const
190{
191 return std::to_string(value_);
192}
193
194template <>
196STUInt64::getJson(JsonOptions) const
197{
198 auto convertToString = [](uint64_t const value, int const base) {
199 XRPL_ASSERT(base == 10 || base == 16, "xrpl::STUInt64::getJson : base 10 or 16");
200 std::string str(base == 10 ? 20 : 16, 0); // Allocate space depending on base
201 auto ret = std::to_chars(str.data(), str.data() + str.size(), value, base);
202 XRPL_ASSERT(ret.ec == std::errc(), "xrpl::STUInt64::getJson : to_chars succeeded");
203 str.resize(std::distance(str.data(), ret.ptr));
204 return str;
205 };
206
207 if (auto const& fName = getFName(); fName.shouldMeta(SField::sMD_BaseTen))
208 {
209 return convertToString(value_, 10); // Convert to base 10
210 }
211
212 return convertToString(value_, 16); // Convert to base 16
213}
214
215//------------------------------------------------------------------------------
216
217template <>
218STInteger<std::int32_t>::STInteger(SerialIter& sit, SField const& name) : STInteger(name, sit.get32())
219{
220}
221
222template <>
224STInt32::getSType() const
225{
226 return STI_INT32;
227}
228
229template <>
231STInt32::getText() const
232{
233 return std::to_string(value_);
234}
235
236template <>
238STInt32::getJson(JsonOptions) const
239{
240 return value_;
241}
242
243} // 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:122
SField const * fName
Definition STBase.h:116
value_type value() const noexcept
Definition STInteger.h:124
std::string getText() const override
Definition STInteger.cpp:36
SerializedTypeID getSType() const override
Definition STInteger.cpp:29
Json::Value getJson(JsonOptions) const override
Definition STInteger.cpp:55
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:51
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:445
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)