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  explicit STInteger(Integer v) : value_(v)
34  {
35  }
36 
37  STInteger(SField const& n, Integer v = 0) : STBase(n), value_(v)
38  {
39  }
40 
41  STInteger(SerialIter& sit, SField const& name);
42 
43  STBase*
44  copy(std::size_t n, void* buf) const override
45  {
46  return emplace(n, buf, *this);
47  }
48 
49  STBase*
50  move(std::size_t n, void* buf) override
51  {
52  return emplace(n, buf, std::move(*this));
53  }
54 
56  getSType() const override;
57 
58  Json::Value getJson(JsonOptions) const override;
59 
61  getText() const override;
62 
63  void
64  add(Serializer& s) const override
65  {
66  assert(fName->isBinary());
67  assert(fName->fieldType == getSType());
68  s.addInteger(value_);
69  }
70 
71  STInteger&
73  {
74  value_ = v;
75  return *this;
76  }
77 
79  value() const noexcept
80  {
81  return value_;
82  }
83 
84  void
85  setValue(Integer v)
86  {
87  value_ = v;
88  }
89 
90  operator Integer() const
91  {
92  return value_;
93  }
94 
95  virtual bool
96  isDefault() const override
97  {
98  return value_ == 0;
99  }
100 
101  bool
102  isEquivalent(const STBase& t) const override
103  {
104  const STInteger* v = dynamic_cast<const STInteger*>(&t);
105  return v && (value_ == v->value_);
106  }
107 
108 private:
109  Integer value_;
110 };
111 
116 
117 } // namespace ripple
118 
119 #endif
ripple::STInteger::add
void add(Serializer &s) const override
Definition: STInteger.h:64
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::STInteger
STInteger(SField const &n, Integer v=0)
Definition: STInteger.h:37
ripple::STInteger::getJson
Json::Value getJson(JsonOptions) const override
Definition: STInteger.cpp:63
ripple::STInteger::operator=
STInteger & operator=(value_type const &v)
Definition: STInteger.h:72
ripple::SerialIter
Definition: Serializer.h:308
ripple::STInteger
Definition: SField.h:49
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::move
STBase * move(std::size_t n, void *buf) override
Definition: STInteger.h:50
ripple::STInteger::value_type
Integer value_type
Definition: STInteger.h:31
ripple::SField
Identifies fields.
Definition: SField.h:109
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:62
ripple::STInteger::setValue
void setValue(Integer v)
Definition: STInteger.h:85
ripple::STInteger::getText
std::string getText() const override
Definition: STInteger.cpp:46
std::size_t
ripple::STInteger::STInteger
STInteger(Integer v)
Definition: STInteger.h:33
ripple::STInteger::value
value_type value() const noexcept
Definition: STInteger.h:79
ripple::STInteger::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STInteger.h:102
ripple::STInteger::getSType
SerializedTypeID getSType() const override
Definition: STInteger.cpp:39
ripple::STInteger::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STInteger.h:44
ripple::STInteger::isDefault
virtual bool isDefault() const override
Definition: STInteger.h:96
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::STInteger::value_
Integer value_
Definition: STInteger.h:109