rippled
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 <ripple/basics/Log.h>
21 #include <ripple/basics/StringUtilities.h>
22 #include <ripple/basics/safe_cast.h>
23 #include <ripple/beast/core/LexicalCast.h>
24 #include <ripple/protocol/LedgerFormats.h>
25 #include <ripple/protocol/STInteger.h>
26 #include <ripple/protocol/TER.h>
27 #include <ripple/protocol/TxFormats.h>
28 #include <charconv>
29 
30 namespace ripple {
31 
32 template <>
34  : STInteger(name, sit.get8())
35 {
36 }
37 
38 template <>
41 {
42  return STI_UINT8;
43 }
44 
45 template <>
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 
63 template <>
65 {
66  if (getFName() == sfTransactionResult)
67  {
68  std::string token, human;
69 
70  if (transResultInfo(TER::fromInt(value_), token, human))
71  return token;
72 
73  JLOG(debugLog().error())
74  << "Unknown result code in metadata: " << value_;
75  }
76 
77  return value_;
78 }
79 
80 //------------------------------------------------------------------------------
81 
82 template <>
84  : STInteger(name, sit.get16())
85 {
86 }
87 
88 template <>
90 STUInt16::getSType() const
91 {
92  return STI_UINT16;
93 }
94 
95 template <>
97 STUInt16::getText() const
98 {
99  if (getFName() == sfLedgerEntryType)
100  {
102  safe_cast<LedgerEntryType>(value_));
103 
104  if (item != nullptr)
105  return item->getName();
106  }
107 
108  if (getFName() == sfTransactionType)
109  {
110  auto item =
111  TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
112 
113  if (item != nullptr)
114  return item->getName();
115  }
116 
117  return std::to_string(value_);
118 }
119 
120 template <>
122 {
123  if (getFName() == sfLedgerEntryType)
124  {
126  safe_cast<LedgerEntryType>(value_));
127 
128  if (item != nullptr)
129  return item->getName();
130  }
131 
132  if (getFName() == sfTransactionType)
133  {
134  auto item =
135  TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
136 
137  if (item != nullptr)
138  return item->getName();
139  }
140 
141  return value_;
142 }
143 
144 //------------------------------------------------------------------------------
145 
146 template <>
148  : STInteger(name, sit.get32())
149 {
150 }
151 
152 template <>
154 STUInt32::getSType() const
155 {
156  return STI_UINT32;
157 }
158 
159 template <>
161 STUInt32::getText() const
162 {
163  return std::to_string(value_);
164 }
165 
166 template <>
168 {
169  return value_;
170 }
171 
172 //------------------------------------------------------------------------------
173 
174 template <>
176  : STInteger(name, sit.get64())
177 {
178 }
179 
180 template <>
182 STUInt64::getSType() const
183 {
184  return STI_UINT64;
185 }
186 
187 template <>
189 STUInt64::getText() const
190 {
191  return std::to_string(value_);
192 }
193 
194 template <>
196 {
197  std::string str(16, 0);
198  auto ret = std::to_chars(str.data(), str.data() + str.size(), value_, 16);
199  assert(ret.ec == std::errc());
200  str.resize(std::distance(str.data(), ret.ptr));
201  return str;
202 }
203 
204 } // namespace ripple
std::string
STL class.
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
charconv
ripple::STI_UINT8
@ STI_UINT8
Definition: SField.h:71
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
std::distance
T distance(T... args)
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
ripple::STInteger::getJson
Json::Value getJson(JsonOptions) const override
Definition: STInteger.cpp:64
ripple::sfTransactionType
const SF_UINT16 sfTransactionType
ripple::TxFormats::getInstance
static TxFormats const & getInstance()
Definition: TxFormats.cpp:334
std::to_string
T to_string(T... args)
ripple::STI_UINT16
@ STI_UINT16
Definition: SField.h:58
std::errc
ripple::SerialIter
Definition: Serializer.h:310
std::to_chars
T to_chars(T... args)
ripple::STInteger
Definition: SField.h:49
ripple::LedgerFormats::getInstance
static LedgerFormats const & getInstance()
Definition: LedgerFormats.cpp:274
ripple::STI_UINT32
@ STI_UINT32
Definition: SField.h:59
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::transResultInfo
bool transResultInfo(TER code, std::string &token, std::string &text)
Definition: TER.cpp:195
ripple::sfTransactionResult
const SF_UINT8 sfTransactionResult
ripple::sfLedgerEntryType
const SF_UINT16 sfLedgerEntryType
ripple::SField
Identifies fields.
Definition: SField.h:112
ripple::STI_UINT64
@ STI_UINT64
Definition: SField.h:60
ripple::TERSubset< CanCvtToTER >::fromInt
static constexpr TERSubset fromInt(int from)
Definition: TER.h:359
ripple::STInteger::getText
std::string getText() const override
Definition: STInteger.cpp:47
ripple::STInteger::STInteger
STInteger(Integer v)
Definition: STInteger.h:84
ripple::STInteger::getSType
SerializedTypeID getSType() const override
Definition: STInteger.cpp:40
ripple::KnownFormats::findByType
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
Definition: KnownFormats.h:127
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::STInteger::value_
Integer value_
Definition: STInteger.h:34