rippled
Loading...
Searching...
No Matches
STInteger.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/Log.h>
21#include <xrpl/basics/safe_cast.h>
22#include <xrpl/beast/utility/instrumentation.h>
23#include <xrpl/json/json_value.h>
24#include <xrpl/protocol/LedgerFormats.h>
25#include <xrpl/protocol/Permissions.h>
26#include <xrpl/protocol/SField.h>
27#include <xrpl/protocol/STBase.h>
28#include <xrpl/protocol/STInteger.h>
29#include <xrpl/protocol/Serializer.h>
30#include <xrpl/protocol/TER.h>
31#include <xrpl/protocol/TxFormats.h>
32
33#include <charconv>
34#include <cstdint>
35#include <iterator>
36#include <string>
37#include <system_error>
38
39namespace ripple {
40
41template <>
43 : STInteger(name, sit.get8())
44{
45}
46
47template <>
50{
51 return STI_UINT8;
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 human;
64
65 // LCOV_EXCL_START
66 JLOG(debugLog().error())
67 << "Unknown result code in metadata: " << value_;
68 // LCOV_EXCL_STOP
69 }
70
71 return std::to_string(value_);
72}
73
74template <>
77{
78 if (getFName() == sfTransactionResult)
79 {
80 std::string token, human;
81
82 if (transResultInfo(TER::fromInt(value_), token, human))
83 return token;
84
85 // LCOV_EXCL_START
86 JLOG(debugLog().error())
87 << "Unknown result code in metadata: " << value_;
88 // LCOV_EXCL_STOP
89 }
90
91 return value_;
92}
93
94//------------------------------------------------------------------------------
95
96template <>
98 : STInteger(name, sit.get16())
99{
100}
101
102template <>
104STUInt16::getSType() const
105{
106 return STI_UINT16;
107}
108
109template <>
111STUInt16::getText() const
112{
113 if (getFName() == sfLedgerEntryType)
114 {
116 safe_cast<LedgerEntryType>(value_));
117
118 if (item != nullptr)
119 return item->getName();
120 }
121
122 if (getFName() == sfTransactionType)
123 {
124 auto item =
125 TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
126
127 if (item != nullptr)
128 return item->getName();
129 }
130
131 return std::to_string(value_);
132}
133
134template <>
136STUInt16::getJson(JsonOptions) const
137{
138 if (getFName() == sfLedgerEntryType)
139 {
141 safe_cast<LedgerEntryType>(value_));
142
143 if (item != nullptr)
144 return item->getName();
145 }
146
147 if (getFName() == sfTransactionType)
148 {
149 auto item =
150 TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
151
152 if (item != nullptr)
153 return item->getName();
154 }
155
156 return value_;
157}
158
159//------------------------------------------------------------------------------
160
161template <>
163 : STInteger(name, sit.get32())
164{
165}
166
167template <>
169STUInt32::getSType() const
170{
171 return STI_UINT32;
172}
173
174template <>
176STUInt32::getText() const
177{
178 if (getFName() == sfPermissionValue)
179 {
180 auto const permissionName =
182 if (permissionName)
183 return *permissionName;
184 }
185 return std::to_string(value_);
186}
187
188template <>
190STUInt32::getJson(JsonOptions) const
191{
192 if (getFName() == sfPermissionValue)
193 {
194 auto const permissionName =
196 if (permissionName)
197 return *permissionName;
198 }
199
200 return value_;
201}
202
203//------------------------------------------------------------------------------
204
205template <>
207 : STInteger(name, sit.get64())
208{
209}
210
211template <>
213STUInt64::getSType() const
214{
215 return STI_UINT64;
216}
217
218template <>
220STUInt64::getText() const
221{
222 return std::to_string(value_);
223}
224
225template <>
227STUInt64::getJson(JsonOptions) const
228{
229 auto convertToString = [](uint64_t const value, int const base) {
230 XRPL_ASSERT(
231 base == 10 || base == 16,
232 "ripple::STUInt64::getJson : base 10 or 16");
233 std::string str(
234 base == 10 ? 20 : 16, 0); // Allocate space depending on base
235 auto ret =
236 std::to_chars(str.data(), str.data() + str.size(), value, base);
237 XRPL_ASSERT(
238 ret.ec == std::errc(),
239 "ripple::STUInt64::getJson : to_chars succeeded");
240 str.resize(std::distance(str.data(), ret.ptr));
241 return str;
242 };
243
244 if (auto const& fName = getFName(); fName.shouldMeta(SField::sMD_BaseTen))
245 {
246 return convertToString(value_, 10); // Convert to base 10
247 }
248
249 return convertToString(value_, 16); // Convert to base 16
250}
251
252//------------------------------------------------------------------------------
253
254template <>
256 : STInteger(name, sit.get32())
257{
258}
259
260template <>
262STInt32::getSType() const
263{
264 return STI_INT32;
265}
266
267template <>
269STInt32::getText() const
270{
271 return std::to_string(value_);
272}
273
274template <>
276STInt32::getJson(JsonOptions) const
277{
278 return value_;
279}
280
281} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
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:146
bool shouldMeta(int c) const
Definition SField.h:278
SField const & getFName() const
Definition STBase.cpp:143
SField const * fName
Definition STBase.h:136
STInteger(Integer v)
Definition STInteger.h:87
std::string getText() const override
Definition STInteger.cpp:56
value_type value() const noexcept
Definition STInteger.h:148
Json::Value getJson(JsonOptions) const override
Definition STInteger.cpp:76
SerializedTypeID getSType() const override
Definition STInteger.cpp:49
static constexpr TERSubset fromInt(int from)
Definition TER.h:433
static TxFormats const & getInstance()
Definition TxFormats.cpp:71
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:25
SerializedTypeID
Definition SField.h:110
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:476
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition TER.cpp:249
T resize(T... args)
T size(T... args)
Note, should be treated as flags that can be | and &.
Definition STBase.h:37
T to_chars(T... args)
T to_string(T... args)