rippled
STInteger.h
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 #ifndef RIPPLE_PROTOCOL_STINTEGER_H_INCLUDED
21 #define RIPPLE_PROTOCOL_STINTEGER_H_INCLUDED
22 
23 #include <ripple/protocol/STBase.h>
24 
25 namespace ripple {
26 
27 template <typename Integer>
28 class STInteger : public STBase
29 {
30 public:
31  using value_type = Integer;
32 
33 private:
34  Integer value_;
35 
36 public:
37  explicit STInteger(Integer v);
38  STInteger(SField const& n, Integer v = 0);
39  STInteger(SerialIter& sit, SField const& name);
40 
42  getSType() const override;
43 
44  Json::Value getJson(JsonOptions) const override;
45 
47  getText() const override;
48 
49  void
50  add(Serializer& s) const override;
51 
52  bool
53  isDefault() const override;
54 
55  bool
56  isEquivalent(const STBase& t) const override;
57 
58  STInteger&
59  operator=(value_type const& v);
60 
62  value() const noexcept;
63 
64  void
65  setValue(Integer v);
66 
67  operator Integer() const;
68 
69 private:
70  STBase*
71  copy(std::size_t n, void* buf) const override;
72  STBase*
73  move(std::size_t n, void* buf) override;
74 
75  friend class ripple::detail::STVar;
76 };
77 
78 using STUInt8 = STInteger<unsigned char>;
79 using STUInt16 = STInteger<std::uint16_t>;
80 using STUInt32 = STInteger<std::uint32_t>;
81 using STUInt64 = STInteger<std::uint64_t>;
82 
83 template <typename Integer>
84 inline STInteger<Integer>::STInteger(Integer v) : value_(v)
85 {
86 }
87 
88 template <typename Integer>
89 inline STInteger<Integer>::STInteger(SField const& n, Integer v)
90  : STBase(n), value_(v)
91 {
92 }
93 
94 template <typename Integer>
95 inline STBase*
97 {
98  return emplace(n, buf, *this);
99 }
100 
101 template <typename Integer>
102 inline STBase*
104 {
105  return emplace(n, buf, std::move(*this));
106 }
107 
108 template <typename Integer>
109 inline void
111 {
112  assert(getFName().isBinary());
113  assert(getFName().fieldType == getSType());
114  s.addInteger(value_);
115 }
116 
117 template <typename Integer>
118 inline bool
120 {
121  return value_ == 0;
122 }
123 
124 template <typename Integer>
125 inline bool
127 {
128  const STInteger* v = dynamic_cast<const STInteger*>(&t);
129  return v && (value_ == v->value_);
130 }
131 
132 template <typename Integer>
133 inline STInteger<Integer>&
135 {
136  value_ = v;
137  return *this;
138 }
139 
140 template <typename Integer>
141 inline typename STInteger<Integer>::value_type
143 {
144  return value_;
145 }
146 
147 template <typename Integer>
148 inline void
150 {
151  value_ = v;
152 }
153 
154 template <typename Integer>
155 inline STInteger<Integer>::operator Integer() const
156 {
157  return value_;
158 }
159 
160 } // namespace ripple
161 
162 #endif
ripple::STInteger::add
void add(Serializer &s) const override
Definition: STInteger.h:110
std::string
STL class.
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::Serializer::addInteger
int addInteger(Integer)
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::STInteger::getJson
Json::Value getJson(JsonOptions) const override
Definition: STInteger.cpp:64
ripple::STInteger::isDefault
bool isDefault() const override
Definition: STInteger.h:119
ripple::STInteger::value
value_type value() const noexcept
Definition: STInteger.h:142
ripple::SerialIter
Definition: Serializer.h:310
ripple::STInteger
Definition: SField.h:49
ripple::STInteger::move
STBase * move(std::size_t n, void *buf) override
Definition: STInteger.h:103
ripple::STInteger::operator=
STInteger & operator=(value_type const &v)
Definition: STInteger.h:134
ripple::Serializer
Definition: Serializer.h:39
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::STInteger::value_type
Integer value_type
Definition: STInteger.h:31
ripple::STInteger::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STInteger.h:96
ripple::SField
Identifies fields.
Definition: SField.h:112
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:66
ripple::STInteger::setValue
void setValue(Integer v)
Definition: STInteger.h:149
std
STL namespace.
ripple::STInteger::getText
std::string getText() const override
Definition: STInteger.cpp:47
std::size_t
ripple::STInteger::STInteger
STInteger(Integer v)
Definition: STInteger.h:84
ripple::STInteger::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STInteger.h:126
ripple::STInteger::getSType
SerializedTypeID getSType() const override
Definition: STInteger.cpp:40
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::STInteger::value_
Integer value_
Definition: STInteger.h:34