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