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