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