xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
protocol
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
14
namespace
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.
20
template
<
int
Bits>
21
class
STBitString
final :
public
STBase
,
public
CountedObject
<STBitString<Bits>>
22
{
23
static_assert
(Bits > 0,
"Number of bits must be positive"
);
24
25
public
:
26
using
value_type
=
BaseUInt<Bits>
;
27
28
private
:
29
value_type
value_
{};
30
31
public
:
32
STBitString
() =
default
;
33
34
STBitString
(
SField
const
& n);
35
STBitString
(
value_type
const
& v);
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
56
setValue
(
BaseUInt<Bits, Tag>
const
& v);
57
58
[[nodiscard]]
value_type
const
&
59
value
()
const
;
60
61
operator
value_type
()
const
;
62
63
private
:
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
72
using
STUInt128
=
STBitString<128>
;
73
using
STUInt160
=
STBitString<160>
;
74
using
STUInt192
=
STBitString<192>
;
75
using
STUInt256
=
STBitString<256>
;
76
77
template
<
int
Bits>
78
inline
STBitString<Bits>::STBitString
(
SField
const
& n) :
STBase
(n)
79
{
80
}
81
82
template
<
int
Bits>
83
inline
STBitString<Bits>::STBitString
(
value_type
const
& v) :
value_
(v)
84
{
85
}
86
87
template
<
int
Bits>
88
inline
STBitString<Bits>::STBitString
(
SField
const
& n,
value_type
const
& v) :
STBase
(n),
value_
(v)
89
{
90
}
91
92
template
<
int
Bits>
93
inline
STBitString<Bits>::STBitString
(
SerialIter
& sit,
SField
const
& name)
94
:
STBitString
(name, sit.getBitString<Bits>())
95
{
96
}
97
98
template
<
int
Bits>
99
STBase
*
100
STBitString<Bits>::copy
(
std::size_t
n,
void
* buf)
const
101
{
102
return
emplace
(n, buf, *
this
);
103
}
104
105
template
<
int
Bits>
106
STBase
*
107
STBitString<Bits>::move
(
std::size_t
n,
void
* buf)
108
{
109
return
emplace
(n, buf, std::move(*
this
));
110
}
111
112
template
<>
113
inline
SerializedTypeID
114
STUInt128::getSType
()
const
115
{
116
return
STI_UINT128;
117
}
118
119
template
<>
120
inline
SerializedTypeID
121
STUInt160::getSType
()
const
122
{
123
return
STI_UINT160;
124
}
125
126
template
<>
127
inline
SerializedTypeID
128
STUInt192::getSType
()
const
129
{
130
return
STI_UINT192;
131
}
132
133
template
<>
134
inline
SerializedTypeID
135
STUInt256::getSType
()
const
136
{
137
return
STI_UINT256;
138
}
139
140
template
<
int
Bits>
141
std::string
142
STBitString<Bits>::getText
()
const
143
{
144
return
to_string
(
value_
);
145
}
146
147
template
<
int
Bits>
148
bool
149
STBitString<Bits>::isEquivalent
(
STBase
const
& t)
const
150
{
151
auto
const
* v =
dynamic_cast<
STBitString
const*
>
(&t);
152
return
v && (
value_
== v->value_);
153
}
154
155
template
<
int
Bits>
156
void
157
STBitString<Bits>::add
(
Serializer
& s)
const
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
164
template
<
int
Bits>
165
template
<
typename
Tag>
166
void
167
STBitString<Bits>::setValue
(
BaseUInt<Bits, Tag>
const
& v)
168
{
169
value_
= v;
170
}
171
172
template
<
int
Bits>
173
STBitString<Bits>::value_type
const
&
174
STBitString<Bits>::value
()
const
175
{
176
return
value_
;
177
}
178
179
template
<
int
Bits>
180
STBitString<Bits>
::
181
operator
value_type
()
const
182
{
183
return
value_
;
184
}
185
186
template
<
int
Bits>
187
bool
188
STBitString<Bits>::isDefault
()
const
189
{
190
return
value_
==
beast::kZero
;
191
}
192
193
}
// namespace xrpl
std::string
xrpl::BaseUInt
Integers of any length that is a multiple of 32-bits.
Definition
base_uint.h:82
xrpl::CountedObject< STBitString< Bits > >::CountedObject
CountedObject() noexcept
Definition
CountedObject.h:119
xrpl::SField
Identifies fields.
Definition
SField.h:132
xrpl::STBase
A type which can be exported to a well known binary format.
Definition
STBase.h:129
xrpl::STBase::getFName
SField const & getFName() const
Definition
STBase.cpp:120
xrpl::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition
STBase.h:226
xrpl::STBase::STBase
STBase()
Definition
STBase.cpp:15
xrpl::STBitString
Definition
STBitString.h:22
xrpl::STBitString::STBitString
STBitString(SerialIter &sit, SField const &name)
Definition
STBitString.h:93
xrpl::STBitString< 128 >::value_
value_type value_
Definition
STBitString.h:29
xrpl::STBitString::getText
std::string getText() const override
Definition
STBitString.h:142
xrpl::STBitString::value_type
BaseUInt< Bits > value_type
Definition
STBitString.h:26
xrpl::STBitString::setValue
void setValue(BaseUInt< Bits, Tag > const &v)
Definition
STBitString.h:167
xrpl::STBitString::isDefault
bool isDefault() const override
Definition
STBitString.h:188
xrpl::STBitString::add
void add(Serializer &s) const override
Definition
STBitString.h:157
xrpl::STBitString::copy
STBase * copy(std::size_t n, void *buf) const override
Definition
STBitString.h:100
xrpl::STBitString::getSType
SerializedTypeID getSType() const override
xrpl::STBitString::STBitString
STBitString(value_type const &v)
Definition
STBitString.h:83
xrpl::STBitString::move
STBase * move(std::size_t n, void *buf) override
Definition
STBitString.h:107
xrpl::STBitString::STBitString
STBitString(SField const &n)
Definition
STBitString.h:78
xrpl::STBitString::STBitString
STBitString()=default
xrpl::STBitString::STBitString
STBitString(SField const &n, value_type const &v)
Definition
STBitString.h:88
xrpl::STBitString::isEquivalent
bool isEquivalent(STBase const &t) const override
Definition
STBitString.h:149
xrpl::STBitString::value
value_type const & value() const
Definition
STBitString.h:174
xrpl::SerialIter
Definition
Serializer.h:478
xrpl::Serializer
Definition
Serializer.h:23
xrpl::Serializer::addBitString
int addBitString(BaseUInt< Bits, Tag > const &v)
Definition
Serializer.h:202
xrpl::detail::STVar
Definition
STVar.h:36
cstddef
beast::kZero
constexpr Zero kZero
Definition
Zero.h:30
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::STUInt128
STBitString< 128 > STUInt128
Definition
STBitString.h:72
xrpl::STUInt160
STBitString< 160 > STUInt160
Definition
STBitString.h:73
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:657
xrpl::SerializedTypeID
SerializedTypeID
Definition
SField.h:94
xrpl::STUInt192
STBitString< 192 > STUInt192
Definition
STBitString.h:74
xrpl::STUInt256
STBitString< 256 > STUInt256
Definition
STBitString.h:75
std::size_t
string
Generated by
1.17.0