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/StringUtilities.h>
22#include <xrpl/basics/safe_cast.h>
23#include <xrpl/beast/core/LexicalCast.h>
24#include <xrpl/protocol/LedgerFormats.h>
25#include <xrpl/protocol/STInteger.h>
26#include <xrpl/protocol/TER.h>
27#include <xrpl/protocol/TxFormats.h>
28#include <charconv>
29
30namespace ripple {
31
32template <>
34 : STInteger(name, sit.get8())
35{
36}
37
38template <>
41{
42 return STI_UINT8;
43}
44
45template <>
48{
49 if (getFName() == sfTransactionResult)
50 {
51 std::string token, human;
52
53 if (transResultInfo(TER::fromInt(value_), token, human))
54 return human;
55
56 JLOG(debugLog().error())
57 << "Unknown result code in metadata: " << value_;
58 }
59
60 return std::to_string(value_);
61}
62
63template <>
66{
67 if (getFName() == sfTransactionResult)
68 {
69 std::string token, human;
70
71 if (transResultInfo(TER::fromInt(value_), token, human))
72 return token;
73
74 JLOG(debugLog().error())
75 << "Unknown result code in metadata: " << value_;
76 }
77
78 return value_;
79}
80
81//------------------------------------------------------------------------------
82
83template <>
85 : STInteger(name, sit.get16())
86{
87}
88
89template <>
92{
93 return STI_UINT16;
94}
95
96template <>
99{
100 if (getFName() == sfLedgerEntryType)
101 {
103 safe_cast<LedgerEntryType>(value_));
104
105 if (item != nullptr)
106 return item->getName();
107 }
108
109 if (getFName() == sfTransactionType)
110 {
111 auto item =
112 TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
113
114 if (item != nullptr)
115 return item->getName();
116 }
117
118 return std::to_string(value_);
119}
120
121template <>
123STUInt16::getJson(JsonOptions) const
124{
125 if (getFName() == sfLedgerEntryType)
126 {
128 safe_cast<LedgerEntryType>(value_));
129
130 if (item != nullptr)
131 return item->getName();
132 }
133
134 if (getFName() == sfTransactionType)
135 {
136 auto item =
137 TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
138
139 if (item != nullptr)
140 return item->getName();
141 }
142
143 return value_;
144}
145
146//------------------------------------------------------------------------------
147
148template <>
150 : STInteger(name, sit.get32())
151{
152}
153
154template <>
156STUInt32::getSType() const
157{
158 return STI_UINT32;
159}
160
161template <>
163STUInt32::getText() const
164{
165 return std::to_string(value_);
166}
167
168template <>
170STUInt32::getJson(JsonOptions) const
171{
172 return value_;
173}
174
175//------------------------------------------------------------------------------
176
177template <>
179 : STInteger(name, sit.get64())
180{
181}
182
183template <>
185STUInt64::getSType() const
186{
187 return STI_UINT64;
188}
189
190template <>
192STUInt64::getText() const
193{
194 return std::to_string(value_);
195}
196
197template <>
199STUInt64::getJson(JsonOptions) const
200{
201 auto convertToString = [](uint64_t const value, int const base) {
202 XRPL_ASSERT(
203 base == 10 || base == 16,
204 "ripple::STUInt64::getJson : base 10 or 16");
205 std::string str(
206 base == 10 ? 20 : 16, 0); // Allocate space depending on base
207 auto ret =
208 std::to_chars(str.data(), str.data() + str.size(), value, base);
209 XRPL_ASSERT(
210 ret.ec == std::errc(),
211 "ripple::STUInt64::getJson : to_chars succeeded");
212 str.resize(std::distance(str.data(), ret.ptr));
213 return str;
214 };
215
216 if (auto const& fName = getFName(); fName.shouldMeta(SField::sMD_BaseTen))
217 {
218 return convertToString(value_, 10); // Convert to base 10
219 }
220
221 return convertToString(value_, 16); // Convert to base 16
222}
223
224} // namespace ripple
Represents a JSON value.
Definition: json_value.h:147
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
Definition: KnownFormats.h:127
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:134
SField const * fName
Definition: STBase.h:125
STInteger(Integer v)
Definition: STInteger.h:85
Json::Value getJson(JsonOptions) const override
Definition: STInteger.cpp:65
std::string getText() const override
Definition: STInteger.cpp:47
value_type value() const noexcept
Definition: STInteger.h:146
Integer value_
Definition: STInteger.h:35
SerializedTypeID getSType() const override
Definition: STInteger.cpp:40
static constexpr TERSubset fromInt(int from)
Definition: TER.h:411
static TxFormats const & getInstance()
Definition: TxFormats.cpp:68
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:452
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition: TER.cpp:236
Note, should be treated as flags that can be | and &.
Definition: STBase.h:36
T to_chars(T... args)
T to_string(T... args)