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/protocol/LedgerFormats.h>
24 #include <ripple/protocol/STInteger.h>
25 #include <ripple/protocol/TxFormats.h>
26 #include <ripple/protocol/TER.h>
27 #include <ripple/beast/core/LexicalCast.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 <>
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  {
101  auto item = LedgerFormats::getInstance ().findByType (
102  safe_cast<LedgerEntryType> (value_));
103 
104  if (item != nullptr)
105  return item->getName ();
106  }
107 
108  if (getFName () == sfTransactionType)
109  {
110  auto item =TxFormats::getInstance().findByType (
111  safe_cast<TxType> (value_));
112 
113  if (item != nullptr)
114  return item->getName ();
115  }
116 
117  return beast::lexicalCastThrow <std::string> (value_);
118 }
119 
120 template <>
123 {
124  if (getFName () == sfLedgerEntryType)
125  {
126  auto item = LedgerFormats::getInstance ().findByType (
127  safe_cast<LedgerEntryType> (value_));
128 
129  if (item != nullptr)
130  return item->getName ();
131  }
132 
133  if (getFName () == sfTransactionType)
134  {
135  auto item = TxFormats::getInstance().findByType (
136  safe_cast<TxType> (value_));
137 
138  if (item != nullptr)
139  return item->getName ();
140  }
141 
142  return value_;
143 }
144 
145 //------------------------------------------------------------------------------
146 
147 template<>
149  : STInteger(name, sit.get32())
150 {
151 }
152 
153 template <>
155 STUInt32::getSType () const
156 {
157  return STI_UINT32;
158 }
159 
160 template <>
162 STUInt32::getText () const
163 {
164  return beast::lexicalCastThrow <std::string> (value_);
165 }
166 
167 template <>
170 {
171  return value_;
172 }
173 
174 //------------------------------------------------------------------------------
175 
176 template<>
178  : STInteger(name, sit.get64())
179 {
180 }
181 
182 template <>
184 STUInt64::getSType () const
185 {
186  return STI_UINT64;
187 }
188 
189 template <>
191 STUInt64::getText () const
192 {
193  return beast::lexicalCastThrow <std::string> (value_);
194 }
195 
196 template <>
199 {
200  return strHex (value_);
201 }
202 
203 } // 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:330
ripple::STI_UINT8
@ STI_UINT8
Definition: SField.h:73
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:417
ripple::STInteger::getJson
Json::Value getJson(JsonOptions) const override
Definition: STInteger.cpp:64
ripple::TxFormats::getInstance
static TxFormats const & getInstance()
Definition: TxFormats.cpp:236
ripple::KnownFormats::findByType
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
Definition: KnownFormats.h:120
ripple::STI_UINT16
@ STI_UINT16
Definition: SField.h:60
ripple::sfTransactionType
const SF_U16 sfTransactionType(access, STI_UINT16, 2, "TransactionType")
Definition: SField.h:331
ripple::SerialIter
Definition: Serializer.h:311
ripple::STInteger
Definition: SField.h:49
ripple::LedgerFormats::getInstance
static LedgerFormats const & getInstance()
Definition: LedgerFormats.cpp:217
ripple::STI_UINT32
@ STI_UINT32
Definition: SField.h:61
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:169
ripple::SField
Identifies fields.
Definition: SField.h:112
ripple::STI_UINT64
@ STI_UINT64
Definition: SField.h:62
ripple::TERSubset< CanCvtToTER >::fromInt
static constexpr TERSubset fromInt(int from)
Definition: TER.h:323
ripple::sfTransactionResult
const SF_U8 sfTransactionResult(access, STI_UINT8, 3, "TransactionResult")
Definition: SField.h:326
ripple::STInteger::getText
std::string getText() const override
Definition: STInteger.cpp:46
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:70
ripple::STInteger::STInteger
STInteger(Integer v)
Definition: STInteger.h:35
ripple::STInteger::getSType
SerializedTypeID getSType() const override
Definition: STInteger.cpp:39
Json::Value
Represents a JSON value.
Definition: json_value.h:141
ripple::STInteger::value_
Integer value_
Definition: STInteger.h:111