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/SField.h>
26#include <xrpl/protocol/STBase.h>
27#include <xrpl/protocol/STInteger.h>
28#include <xrpl/protocol/Serializer.h>
29#include <xrpl/protocol/TER.h>
30#include <xrpl/protocol/TxFormats.h>
31
32#include <charconv>
33#include <cstdint>
34#include <iterator>
35#include <string>
36#include <system_error>
37
38namespace ripple {
39
40template <>
42 : STInteger(name, sit.get8())
43{
44}
45
46template <>
49{
50 return STI_UINT8;
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 human;
63
64 JLOG(debugLog().error())
65 << "Unknown result code in metadata: " << value_;
66 }
67
68 return std::to_string(value_);
69}
70
71template <>
74{
75 if (getFName() == sfTransactionResult)
76 {
77 std::string token, human;
78
79 if (transResultInfo(TER::fromInt(value_), token, human))
80 return token;
81
82 JLOG(debugLog().error())
83 << "Unknown result code in metadata: " << value_;
84 }
85
86 return value_;
87}
88
89//------------------------------------------------------------------------------
90
91template <>
93 : STInteger(name, sit.get16())
94{
95}
96
97template <>
100{
101 return STI_UINT16;
102}
103
104template <>
106STUInt16::getText() const
107{
108 if (getFName() == sfLedgerEntryType)
109 {
111 safe_cast<LedgerEntryType>(value_));
112
113 if (item != nullptr)
114 return item->getName();
115 }
116
117 if (getFName() == sfTransactionType)
118 {
119 auto item =
120 TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
121
122 if (item != nullptr)
123 return item->getName();
124 }
125
126 return std::to_string(value_);
127}
128
129template <>
131STUInt16::getJson(JsonOptions) const
132{
133 if (getFName() == sfLedgerEntryType)
134 {
136 safe_cast<LedgerEntryType>(value_));
137
138 if (item != nullptr)
139 return item->getName();
140 }
141
142 if (getFName() == sfTransactionType)
143 {
144 auto item =
145 TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
146
147 if (item != nullptr)
148 return item->getName();
149 }
150
151 return value_;
152}
153
154//------------------------------------------------------------------------------
155
156template <>
158 : STInteger(name, sit.get32())
159{
160}
161
162template <>
164STUInt32::getSType() const
165{
166 return STI_UINT32;
167}
168
169template <>
171STUInt32::getText() const
172{
173 return std::to_string(value_);
174}
175
176template <>
178STUInt32::getJson(JsonOptions) const
179{
180 return value_;
181}
182
183//------------------------------------------------------------------------------
184
185template <>
187 : STInteger(name, sit.get64())
188{
189}
190
191template <>
193STUInt64::getSType() const
194{
195 return STI_UINT64;
196}
197
198template <>
200STUInt64::getText() const
201{
202 return std::to_string(value_);
203}
204
205template <>
207STUInt64::getJson(JsonOptions) const
208{
209 auto convertToString = [](uint64_t const value, int const base) {
210 XRPL_ASSERT(
211 base == 10 || base == 16,
212 "ripple::STUInt64::getJson : base 10 or 16");
213 std::string str(
214 base == 10 ? 20 : 16, 0); // Allocate space depending on base
215 auto ret =
216 std::to_chars(str.data(), str.data() + str.size(), value, base);
217 XRPL_ASSERT(
218 ret.ec == std::errc(),
219 "ripple::STUInt64::getJson : to_chars succeeded");
220 str.resize(std::distance(str.data(), ret.ptr));
221 return str;
222 };
223
224 if (auto const& fName = getFName(); fName.shouldMeta(SField::sMD_BaseTen))
225 {
226 return convertToString(value_, 10); // Convert to base 10
227 }
228
229 return convertToString(value_, 16); // Convert to base 16
230}
231
232} // namespace ripple
Represents a JSON value.
Definition: json_value.h:148
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
Definition: KnownFormats.h:129
static LedgerFormats const & getInstance()
Identifies fields.
Definition: SField.h:144
bool shouldMeta(int c) const
Definition: SField.h:274
SField const & getFName() const
Definition: STBase.cpp:141
SField const * fName
Definition: STBase.h:127
STInteger(Integer v)
Definition: STInteger.h:85
Json::Value getJson(JsonOptions) const override
Definition: STInteger.cpp:73
std::string getText() const override
Definition: STInteger.cpp:55
value_type value() const noexcept
Definition: STInteger.h:146
Integer value_
Definition: STInteger.h:35
SerializedTypeID getSType() const override
Definition: STInteger.cpp:48
static constexpr TERSubset fromInt(int from)
Definition: TER.h:411
static TxFormats const & getInstance()
Definition: TxFormats.cpp:69
T distance(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
SerializedTypeID
Definition: SField.h:108
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:468
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition: TER.cpp:242
Note, should be treated as flags that can be | and &.
Definition: STBase.h:38
T to_chars(T... args)
T to_string(T... args)