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 
29 namespace ripple {
30 
31 template <>
33  : STInteger(name, sit.get8())
34 {
35 }
36 
37 template <>
40 {
41  return STI_UINT8;
42 }
43 
44 template <>
47 {
48  if (getFName() == sfTransactionResult)
49  {
50  std::string token, human;
51 
52  if (transResultInfo(TER::fromInt(value_), token, human))
53  return human;
54 
55  JLOG(debugLog().error())
56  << "Unknown result code in metadata: " << value_;
57  }
58 
59  return beast::lexicalCastThrow<std::string>(value_);
60 }
61 
62 template <>
64 {
65  if (getFName() == sfTransactionResult)
66  {
67  std::string token, human;
68 
69  if (transResultInfo(TER::fromInt(value_), token, human))
70  return token;
71 
72  JLOG(debugLog().error())
73  << "Unknown result code in metadata: " << value_;
74  }
75 
76  return value_;
77 }
78 
79 //------------------------------------------------------------------------------
80 
81 template <>
83  : STInteger(name, sit.get16())
84 {
85 }
86 
87 template <>
89 STUInt16::getSType() const
90 {
91  return STI_UINT16;
92 }
93 
94 template <>
96 STUInt16::getText() const
97 {
98  if (getFName() == sfLedgerEntryType)
99  {
101  safe_cast<LedgerEntryType>(value_));
102 
103  if (item != nullptr)
104  return item->getName();
105  }
106 
107  if (getFName() == sfTransactionType)
108  {
109  auto item =
110  TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
111 
112  if (item != nullptr)
113  return item->getName();
114  }
115 
116  return beast::lexicalCastThrow<std::string>(value_);
117 }
118 
119 template <>
121 {
122  if (getFName() == sfLedgerEntryType)
123  {
125  safe_cast<LedgerEntryType>(value_));
126 
127  if (item != nullptr)
128  return item->getName();
129  }
130 
131  if (getFName() == sfTransactionType)
132  {
133  auto item =
134  TxFormats::getInstance().findByType(safe_cast<TxType>(value_));
135 
136  if (item != nullptr)
137  return item->getName();
138  }
139 
140  return value_;
141 }
142 
143 //------------------------------------------------------------------------------
144 
145 template <>
147  : STInteger(name, sit.get32())
148 {
149 }
150 
151 template <>
153 STUInt32::getSType() const
154 {
155  return STI_UINT32;
156 }
157 
158 template <>
160 STUInt32::getText() const
161 {
162  return beast::lexicalCastThrow<std::string>(value_);
163 }
164 
165 template <>
167 {
168  return value_;
169 }
170 
171 //------------------------------------------------------------------------------
172 
173 template <>
175  : STInteger(name, sit.get64())
176 {
177 }
178 
179 template <>
181 STUInt64::getSType() const
182 {
183  return STI_UINT64;
184 }
185 
186 template <>
188 STUInt64::getText() const
189 {
190  return beast::lexicalCastThrow<std::string>(value_);
191 }
192 
193 template <>
195 {
196  return strHex(value_);
197 }
198 
199 } // namespace ripple
std::string
STL class.
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::sfLedgerEntryType
const SF_U16 sfLedgerEntryType(access, STI_UINT16, 1, "LedgerEntryType", SField::sMD_Never)
Definition: SField.h:345
ripple::STI_UINT8
@ STI_UINT8
Definition: SField.h:72
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
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:63
ripple::TxFormats::getInstance
static TxFormats const & getInstance()
Definition: TxFormats.cpp:257
ripple::KnownFormats::findByType
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
Definition: KnownFormats.h:125
ripple::STI_UINT16
@ STI_UINT16
Definition: SField.h:59
ripple::sfTransactionType
const SF_U16 sfTransactionType(access, STI_UINT16, 2, "TransactionType")
Definition: SField.h:346
ripple::SerialIter
Definition: Serializer.h:308
ripple::STInteger
Definition: SField.h:49
ripple::LedgerFormats::getInstance
static LedgerFormats const & getInstance()
Definition: LedgerFormats.cpp:230
ripple::STI_UINT32
@ STI_UINT32
Definition: SField.h:60
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:264
ripple::SField
Identifies fields.
Definition: SField.h:109
ripple::STI_UINT64
@ STI_UINT64
Definition: SField.h:61
ripple::TERSubset< CanCvtToTER >::fromInt
static constexpr TERSubset fromInt(int from)
Definition: TER.h:341
ripple::sfTransactionResult
const SF_U8 sfTransactionResult(access, STI_UINT8, 3, "TransactionResult")
Definition: SField.h:341
ripple::STInteger::getText
std::string getText() const override
Definition: STInteger.cpp:46
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:67
ripple::STInteger::STInteger
STInteger(Integer v)
Definition: STInteger.h:33
ripple::STInteger::getSType
SerializedTypeID getSType() const override
Definition: STInteger.cpp:39
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::STInteger::value_
Integer value_
Definition: STInteger.h:109