xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
protocol
STVar.cpp
1
#include <xrpl/protocol/detail/STVar.h>
2
3
#include <xrpl/basics/contract.h>
4
#include <xrpl/beast/utility/instrumentation.h>
5
#include <xrpl/protocol/SField.h>
6
#include <xrpl/protocol/STAccount.h>
7
#include <xrpl/protocol/STAmount.h>
8
#include <xrpl/protocol/STArray.h>
9
#include <xrpl/protocol/STBase.h>
10
#include <xrpl/protocol/STBitString.h>
11
#include <xrpl/protocol/STBlob.h>
12
#include <xrpl/protocol/STCurrency.h>
13
#include <xrpl/protocol/STInteger.h>
14
#include <xrpl/protocol/STIssue.h>
15
#include <xrpl/protocol/STNumber.h>
16
#include <xrpl/protocol/STObject.h>
17
#include <xrpl/protocol/STPathSet.h>
18
#include <xrpl/protocol/STVector256.h>
19
#include <xrpl/protocol/Serializer.h>
20
21
#include <
stdexcept
>
22
#include <
tuple
>
23
#include <
type_traits
>
24
25
namespace
xrpl::detail
{
26
27
DefaultObjectT
gDefaultObject
;
28
NonPresentObjectT
gNonPresentObject
;
29
30
//------------------------------------------------------------------------------
31
32
STVar::~STVar
()
33
{
34
destroy
();
35
}
36
37
STVar::STVar
(
STVar
const
& other)
38
{
39
if
(other.
p_
!=
nullptr
)
40
p_
= other.
p_
->
copy
(
kMaxSize
, &
d_
);
41
}
42
43
STVar::STVar
(
STVar
&& other)
44
{
45
if
(other.onHeap())
46
{
47
p_
= other.p_;
48
other.p_ =
nullptr
;
49
}
50
else
51
{
52
p_
= other.p_->move(
kMaxSize
, &
d_
);
53
}
54
}
55
56
STVar
&
57
STVar::operator=
(
STVar
const
& rhs)
58
{
59
if
(&rhs !=
this
)
60
{
61
destroy
();
62
if
(rhs.
p_
!=
nullptr
)
63
{
64
p_
= rhs.
p_
->
copy
(
kMaxSize
, &
d_
);
65
}
66
else
67
{
68
p_
=
nullptr
;
69
}
70
}
71
72
return
*
this
;
73
}
74
75
STVar
&
76
STVar::operator=
(
STVar
&& rhs)
77
{
78
if
(&rhs !=
this
)
79
{
80
destroy
();
81
if
(rhs.onHeap())
82
{
83
p_
= rhs.p_;
84
rhs.p_ =
nullptr
;
85
}
86
else
87
{
88
p_
= rhs.p_->move(
kMaxSize
, &
d_
);
89
}
90
}
91
92
return
*
this
;
93
}
94
95
STVar::STVar
(
DefaultObjectT
,
SField
const
& name) :
STVar
(name.fieldType, name)
96
{
97
}
98
99
STVar::STVar
(
NonPresentObjectT
,
SField
const
& name) :
STVar
(STI_NOTPRESENT, name)
100
{
101
}
102
103
STVar::STVar
(
SerialIter
& sit,
SField
const
& name,
int
depth)
104
{
105
if
(depth > 10)
106
Throw<std::runtime_error>
(
"Maximum nesting depth of STVar exceeded"
);
107
constructST
(name.
fieldType
, depth, sit, name);
108
}
109
110
STVar::STVar
(
SerializedTypeID
id
,
SField
const
& name)
111
{
112
XRPL_ASSERT(
113
(
id
== STI_NOTPRESENT) || (
id
== name.
fieldType
),
114
"xrpl::detail::STVar::STVar(SerializedTypeID) : valid type input"
);
115
constructST
(
id
, 0, name);
116
}
117
118
void
119
STVar::destroy
()
120
{
121
if
(
onHeap
())
122
{
123
delete
p_
;
124
}
125
else
126
{
127
p_
->~STBase();
128
}
129
130
p_
=
nullptr
;
131
}
132
133
template
<
typename
... Args>
134
requires
ValidConstructSTArgs
<Args...>
135
void
136
STVar::constructST
(
SerializedTypeID
id
,
int
depth, Args&&... args)
137
{
138
auto
const
constructWithDepth = [&]<
typename
T> {
139
if
constexpr
(
std::is_same_v<std::tuple<std::remove_cvref_t<Args>
...>,
std::tuple<SField>
>)
140
{
141
construct<T>
(
std::forward<Args>
(args)...);
142
}
143
else
if
constexpr
(
144
std::
145
is_same_v<std::tuple<std::remove_cvref_t<Args>...>,
std::tuple<SerialIter, SField>
>)
146
{
147
construct<T>
(
std::forward<Args>
(args)..., depth);
148
}
149
else
150
{
151
static
constexpr
bool
kAlwaysFalse =
152
!
std::is_same_v
<
std::tuple
<Args...>,
std::tuple
<Args...>>;
153
static_assert
(kAlwaysFalse,
"Invalid STVar constructor arguments"
);
154
}
155
};
156
157
switch
(
id
)
158
{
159
case
STI_NOTPRESENT: {
160
// Last argument is always SField
161
SField
const
& field = std::get<
sizeof
...(args) - 1>(
std::forward_as_tuple
(args...));
162
construct<STBase>
(field);
163
return
;
164
}
165
case
STI_UINT8:
166
construct<STUInt8>
(
std::forward<Args>
(args)...);
167
return
;
168
case
STI_UINT16:
169
construct<STUInt16>
(
std::forward<Args>
(args)...);
170
return
;
171
case
STI_UINT32:
172
construct<STUInt32>
(
std::forward<Args>
(args)...);
173
return
;
174
case
STI_UINT64:
175
construct<STUInt64>
(
std::forward<Args>
(args)...);
176
return
;
177
case
STI_AMOUNT:
178
construct<STAmount>
(
std::forward<Args>
(args)...);
179
return
;
180
case
STI_NUMBER:
181
construct<STNumber>
(
std::forward<Args>
(args)...);
182
return
;
183
case
STI_UINT128:
184
construct<STUInt128>
(
std::forward<Args>
(args)...);
185
return
;
186
case
STI_UINT160:
187
construct<STUInt160>
(
std::forward<Args>
(args)...);
188
return
;
189
case
STI_UINT192:
190
construct<STUInt192>
(
std::forward<Args>
(args)...);
191
return
;
192
case
STI_UINT256:
193
construct<STUInt256>
(
std::forward<Args>
(args)...);
194
return
;
195
case
STI_INT32:
196
construct<STInt32>
(
std::forward<Args>
(args)...);
197
return
;
198
case
STI_VECTOR256:
199
construct<STVector256>
(
std::forward<Args>
(args)...);
200
return
;
201
case
STI_VL:
202
construct<STBlob>
(
std::forward<Args>
(args)...);
203
return
;
204
case
STI_ACCOUNT:
205
construct<STAccount>
(
std::forward<Args>
(args)...);
206
return
;
207
case
STI_PATHSET:
208
construct<STPathSet>
(
std::forward<Args>
(args)...);
209
return
;
210
case
STI_OBJECT:
211
constructWithDepth.template operator()<
STObject
>();
212
return
;
213
case
STI_ARRAY:
214
constructWithDepth.template operator()<
STArray
>();
215
return
;
216
case
STI_ISSUE:
217
construct<STIssue>
(
std::forward<Args>
(args)...);
218
return
;
219
case
STI_CURRENCY:
220
construct<STCurrency>
(
std::forward<Args>
(args)...);
221
return
;
222
default
:
223
Throw<std::runtime_error>
(
"Unknown object type"
);
224
}
225
}
226
227
}
// namespace xrpl::detail
xrpl::SField
Identifies fields.
Definition
SField.h:131
xrpl::SField::fieldType
SerializedTypeID const fieldType
Definition
SField.h:152
xrpl::STArray
Definition
STArray.h:20
xrpl::STBase::copy
virtual STBase * copy(std::size_t n, void *buf) const
Definition
STBase.cpp:42
xrpl::STObject
Definition
STObject.h:50
xrpl::SerialIter
Definition
Serializer.h:478
xrpl::detail::STVar
Definition
STVar.h:36
xrpl::detail::STVar::constructST
void constructST(SerializedTypeID id, int depth, Args &&... arg)
Construct requested Serializable Type according to id.
Definition
STVar.cpp:136
xrpl::detail::STVar::d_
std::byte d_[kMaxSize]
Definition
STVar.h:42
xrpl::detail::STVar::onHeap
bool onHeap() const
Definition
STVar.h:135
xrpl::detail::STVar::STVar
STVar()=default
xrpl::detail::STVar::construct
void construct(Args &&... args)
Definition
STVar.h:112
xrpl::detail::STVar::~STVar
~STVar()
Definition
STVar.cpp:32
xrpl::detail::STVar::STVar
STVar(STVar const &other)
Definition
STVar.cpp:37
xrpl::detail::STVar::kMaxSize
static constexpr std::size_t kMaxSize
Definition
STVar.h:39
xrpl::detail::STVar::destroy
void destroy()
Definition
STVar.cpp:119
xrpl::detail::STVar::p_
STBase * p_
Definition
STVar.h:43
xrpl::detail::STVar::operator=
STVar & operator=(STVar const &rhs)
Definition
STVar.cpp:57
xrpl::detail::ValidConstructSTArgs
Definition
STVar.h:29
std::forward_as_tuple
T forward_as_tuple(T... args)
std::forward
T forward(T... args)
std::is_same_v
T is_same_v
xrpl::detail
Definition
base_uint.h:37
xrpl::detail::gDefaultObject
DefaultObjectT gDefaultObject
Definition
STVar.cpp:27
xrpl::detail::gNonPresentObject
NonPresentObjectT gNonPresentObject
Definition
STVar.cpp:28
xrpl::SerializedTypeID
SerializedTypeID
Definition
SField.h:93
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
stdexcept
xrpl::detail::DefaultObjectT
Definition
STVar.h:14
xrpl::detail::NonPresentObjectT
Definition
STVar.h:19
tuple
type_traits
Generated by
1.17.0