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
84template <typename Integer>
85inline STInteger<Integer>::STInteger(Integer v) : value_(v)
86{
87}
88
89template <typename Integer>
90inline STInteger<Integer>::STInteger(SField const& n, Integer v)
91 : STBase(n), value_(v)
92{
93}
94
95template <typename Integer>
96inline STBase*
98{
99 return emplace(n, buf, *this);
100}
101
102template <typename Integer>
103inline STBase*
105{
106 return emplace(n, buf, std::move(*this));
107}
108
109template <typename Integer>
110inline void
112{
113 XRPL_ASSERT(
114 getFName().isBinary(), "ripple::STInteger::add : field is binary");
115 XRPL_ASSERT(
116 getFName().fieldType == getSType(),
117 "ripple::STInteger::add : field type match");
118 s.addInteger(value_);
119}
120
121template <typename Integer>
122inline bool
124{
125 return value_ == 0;
126}
127
128template <typename Integer>
129inline bool
131{
132 STInteger const* v = dynamic_cast<STInteger const*>(&t);
133 return v && (value_ == v->value_);
134}
135
136template <typename Integer>
137inline STInteger<Integer>&
139{
140 value_ = v;
141 return *this;
142}
143
144template <typename Integer>
145inline typename STInteger<Integer>::value_type
147{
148 return value_;
149}
150
151template <typename Integer>
152inline void
154{
155 value_ = v;
156}
157
158template <typename Integer>
159inline STInteger<Integer>::operator Integer() const
160{
161 return value_;
162}
163
164} // namespace ripple
165
166#endif
Represents a JSON value.
Definition: json_value.h:149
Tracks the number of instances of an object.
Identifies fields.
Definition: SField.h:143
A type which can be exported to a well known binary format.
Definition: STBase.h:135
STInteger(Integer v)
Definition: STInteger.h:85
Json::Value getJson(JsonOptions) const override
Definition: STInteger.cpp:74
void add(Serializer &s) const override
Definition: STInteger.h:111
STInteger & operator=(value_type const &v)
Definition: STInteger.h:138
STBase * move(std::size_t n, void *buf) override
Definition: STInteger.h:104
std::string getText() const override
Definition: STInteger.cpp:56
Integer value_type
Definition: STInteger.h:32
value_type value() const noexcept
Definition: STInteger.h:146
STBase * copy(std::size_t n, void *buf) const override
Definition: STInteger.h:97
Integer value_
Definition: STInteger.h:35
friend class ripple::detail::STVar
Definition: STInteger.h:76
SerializedTypeID getSType() const override
Definition: STInteger.cpp:49
bool isEquivalent(STBase const &t) const override
Definition: STInteger.h:130
STInteger(SerialIter &sit, SField const &name)
void setValue(Integer v)
Definition: STInteger.h:153
bool isDefault() const override
Definition: STInteger.h:123
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:107
STL namespace.
Note, should be treated as flags that can be | and &.
Definition: STBase.h:37