xrpld
Loading...
Searching...
No Matches
STBitString.h
1#pragma once
2
3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/protocol/SField.h>
8#include <xrpl/protocol/STBase.h>
9#include <xrpl/protocol/Serializer.h>
10
11#include <cstddef>
12#include <string>
13
14namespace xrpl {
15
16// The template parameter could be an unsigned type, however there's a bug in
17// gdb (last checked in gdb 12.1) that prevents gdb from finding the RTTI
18// information of a template parameterized by an unsigned type. This RTTI
19// information is needed to write gdb pretty printers.
20template <int Bits>
21class STBitString final : public STBase, public CountedObject<STBitString<Bits>>
22{
23 static_assert(Bits > 0, "Number of bits must be positive");
24
25public:
27
28private:
30
31public:
32 STBitString() = default;
33
36 STBitString(SField const& n, value_type const& v);
37 STBitString(SerialIter& sit, SField const& name);
38
39 [[nodiscard]] SerializedTypeID
40 getSType() const override;
41
42 [[nodiscard]] std::string
43 getText() const override;
44
45 [[nodiscard]] bool
46 isEquivalent(STBase const& t) const override;
47
48 void
49 add(Serializer& s) const override;
50
51 [[nodiscard]] bool
52 isDefault() const override;
53
54 template <typename Tag>
55 void
57
58 [[nodiscard]] value_type const&
59 value() const;
60
61 operator value_type() const;
62
63private:
64 STBase*
65 copy(std::size_t n, void* buf) const override;
66 STBase*
67 move(std::size_t n, void* buf) override;
68
69 friend class detail::STVar;
70};
71
76
77template <int Bits>
79{
80}
81
82template <int Bits>
84{
85}
86
87template <int Bits>
89{
90}
91
92template <int Bits>
94 : STBitString(name, sit.getBitString<Bits>())
95{
96}
97
98template <int Bits>
99STBase*
101{
102 return emplace(n, buf, *this);
103}
104
105template <int Bits>
106STBase*
108{
109 return emplace(n, buf, std::move(*this));
110}
111
112template <>
113inline SerializedTypeID
115{
116 return STI_UINT128;
117}
118
119template <>
120inline SerializedTypeID
122{
123 return STI_UINT160;
124}
125
126template <>
127inline SerializedTypeID
129{
130 return STI_UINT192;
131}
132
133template <>
134inline SerializedTypeID
136{
137 return STI_UINT256;
138}
139
140template <int Bits>
143{
144 return to_string(value_);
145}
146
147template <int Bits>
148bool
150{
151 auto const* v = dynamic_cast<STBitString const*>(&t);
152 return v && (value_ == v->value_);
153}
154
155template <int Bits>
156void
158{
159 XRPL_ASSERT(getFName().isBinary(), "xrpl::STBitString::add : field is binary");
160 XRPL_ASSERT(getFName().fieldType == getSType(), "xrpl::STBitString::add : field type match");
161 s.addBitString<Bits>(value_);
162}
163
164template <int Bits>
165template <typename Tag>
166void
171
172template <int Bits>
175{
176 return value_;
177}
178
179template <int Bits>
181operator value_type() const
182{
183 return value_;
184}
185
186template <int Bits>
187bool
189{
190 return value_ == beast::kZero;
191}
192
193} // namespace xrpl
Integers of any length that is a multiple of 32-bits.
Definition base_uint.h:82
Identifies fields.
Definition SField.h:132
A type which can be exported to a well known binary format.
Definition STBase.h:129
SField const & getFName() const
Definition STBase.cpp:120
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:226
STBitString(SerialIter &sit, SField const &name)
Definition STBitString.h:93
std::string getText() const override
BaseUInt< Bits > value_type
Definition STBitString.h:26
void setValue(BaseUInt< Bits, Tag > const &v)
bool isDefault() const override
void add(Serializer &s) const override
STBase * copy(std::size_t n, void *buf) const override
SerializedTypeID getSType() const override
STBitString(value_type const &v)
Definition STBitString.h:83
STBase * move(std::size_t n, void *buf) override
STBitString(SField const &n)
Definition STBitString.h:78
STBitString()=default
STBitString(SField const &n, value_type const &v)
Definition STBitString.h:88
bool isEquivalent(STBase const &t) const override
value_type const & value() const
int addBitString(BaseUInt< Bits, Tag > const &v)
Definition Serializer.h:106
constexpr Zero kZero
Definition Zero.h:30
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
STBitString< 128 > STUInt128
Definition STBitString.h:72
STBitString< 160 > STUInt160
Definition STBitString.h:73
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
SerializedTypeID
Definition SField.h:94
STBitString< 192 > STUInt192
Definition STBitString.h:74
STBitString< 256 > STUInt256
Definition STBitString.h:75