rippled
Loading...
Searching...
No Matches
STBitString.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_STBITSTRING_H_INCLUDED
21#define RIPPLE_PROTOCOL_STBITSTRING_H_INCLUDED
22
23#include <xrpl/basics/CountedObject.h>
24#include <xrpl/beast/utility/Zero.h>
25#include <xrpl/protocol/STBase.h>
26
27namespace ripple {
28
29// The template parameter could be an unsigned type, however there's a bug in
30// gdb (last checked in gdb 12.1) that prevents gdb from finding the RTTI
31// information of a template parameterized by an unsigned type. This RTTI
32// information is needed to write gdb pretty printers.
33template <int Bits>
34class STBitString final : public STBase, public CountedObject<STBitString<Bits>>
35{
36 static_assert(Bits > 0, "Number of bits must be positive");
37
38public:
40
41private:
43
44public:
45 STBitString() = default;
46
47 STBitString(SField const& n);
48 STBitString(value_type const& v);
49 STBitString(SField const& n, value_type const& v);
50 STBitString(SerialIter& sit, SField const& name);
51
53 getSType() const override;
54
56 getText() const override;
57
58 bool
59 isEquivalent(STBase const& t) const override;
60
61 void
62 add(Serializer& s) const override;
63
64 bool
65 isDefault() const override;
66
67 template <typename Tag>
68 void
70
71 value_type const&
72 value() const;
73
74 operator value_type() const;
75
76private:
77 STBase*
78 copy(std::size_t n, void* buf) const override;
79 STBase*
80 move(std::size_t n, void* buf) override;
81
82 friend class detail::STVar;
83};
84
89
90template <int Bits>
92{
93}
94
95template <int Bits>
96inline STBitString<Bits>::STBitString(value_type const& v) : value_(v)
97{
98}
99
100template <int Bits>
102 : STBase(n), value_(v)
103{
104}
105
106template <int Bits>
108 : STBitString(name, sit.getBitString<Bits>())
109{
110}
111
112template <int Bits>
113STBase*
115{
116 return emplace(n, buf, *this);
117}
118
119template <int Bits>
120STBase*
122{
123 return emplace(n, buf, std::move(*this));
124}
125
126template <>
127inline SerializedTypeID
129{
130 return STI_UINT128;
131}
132
133template <>
134inline SerializedTypeID
136{
137 return STI_UINT160;
138}
139
140template <>
141inline SerializedTypeID
143{
144 return STI_UINT192;
145}
146
147template <>
148inline SerializedTypeID
150{
151 return STI_UINT256;
152}
153
154template <int Bits>
157{
158 return to_string(value_);
159}
160
161template <int Bits>
162bool
164{
165 STBitString const* v = dynamic_cast<STBitString const*>(&t);
166 return v && (value_ == v->value_);
167}
168
169template <int Bits>
170void
172{
173 XRPL_ASSERT(
174 getFName().isBinary(), "ripple::STBitString::add : field is binary");
175 XRPL_ASSERT(
176 getFName().fieldType == getSType(),
177 "ripple::STBitString::add : field type match");
178 s.addBitString<Bits>(value_);
179}
180
181template <int Bits>
182template <typename Tag>
183void
185{
186 value_ = v;
187}
188
189template <int Bits>
190typename STBitString<Bits>::value_type const&
192{
193 return value_;
194}
195
196template <int Bits>
198{
199 return value_;
200}
201
202template <int Bits>
203bool
205{
206 return value_ == beast::zero;
207}
208
209} // namespace ripple
210
211#endif
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
bool isDefault() const override
STBase * move(std::size_t n, void *buf) override
std::string getText() const override
SerializedTypeID getSType() const override
STBase * copy(std::size_t n, void *buf) const override
bool isEquivalent(STBase const &t) const override
void setValue(base_uint< Bits, Tag > const &v)
value_type const & value() const
base_uint< Bits > value_type
Definition STBitString.h:39
void add(Serializer &s) const override
int addBitString(base_uint< Bits, Tag > const &v)
Definition Serializer.h:131
Integers of any length that is a multiple of 32-bits.
Definition base_uint.h:86
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
SerializedTypeID
Definition SField.h:110
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630