xrpld
Loading...
Searching...
No Matches
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
26namespace xrpl::detail {
27
30
31//------------------------------------------------------------------------------
32
34{
35 destroy();
36}
37
38STVar::STVar(STVar const& other)
39{
40 if (other.p_ != nullptr)
41 p_ = other.p_->copy(kMaxSize, &d_);
42}
43
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
57STVar&
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
76STVar&
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
96STVar::STVar(DefaultObjectT, SField const& name) : STVar(name.fieldType, name)
97{
98}
99
100STVar::STVar(NonPresentObjectT, SField const& name) : STVar(STI_NOTPRESENT, name)
101{
102}
103
104STVar::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
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
119void
121{
122 if (onHeap())
123 {
124 delete p_;
125 }
126 else
127 {
128 p_->~STBase();
129 }
130
131 p_ = nullptr;
132}
133
134template <typename... Args>
135 requires ValidConstructSTArgs<Args...>
136void
137STVar::constructST(SerializedTypeID id, int depth, Args&&... args)
138{
139 auto constructWithDepth = [&]<typename T>() {
141 {
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:
168 return;
169 case STI_UINT16:
171 return;
172 case STI_UINT32:
174 return;
175 case STI_UINT64:
177 return;
178 case STI_AMOUNT:
180 return;
181 case STI_NUMBER:
183 return;
184 case STI_UINT128:
186 return;
187 case STI_UINT160:
189 return;
190 case STI_UINT192:
192 return;
193 case STI_UINT256:
195 return;
196 case STI_INT32:
198 return;
199 case STI_VECTOR256:
201 return;
202 case STI_VL:
204 return;
205 case STI_ACCOUNT:
207 return;
208 case STI_PATHSET:
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:
219 return;
220 case STI_XCHAIN_BRIDGE:
222 return;
223 case STI_CURRENCY:
225 return;
226 default:
227 Throw<std::runtime_error>("Unknown object type");
228 }
229}
230
231} // namespace xrpl::detail
Identifies fields.
Definition SField.h:130
SerializedTypeID const fieldType
Definition SField.h:151
virtual STBase * copy(std::size_t n, void *buf) const
Definition STBase.cpp:48
void constructST(SerializedTypeID id, int depth, Args &&... arg)
Construct requested Serializable Type according to id.
Definition STVar.cpp:137
std::byte d_[kMaxSize]
Definition STVar.h:40
bool onHeap() const
Definition STVar.h:133
void construct(Args &&... args)
Definition STVar.h:111
STVar(STVar const &other)
Definition STVar.cpp:38
static constexpr std::size_t kMaxSize
Definition STVar.h:38
STBase * p_
Definition STVar.h:41
STVar & operator=(STVar const &rhs)
Definition STVar.cpp:58
T forward_as_tuple(T... args)
T forward(T... args)
T is_same_v
DefaultObjectT gDefaultObject
Definition STVar.cpp:28
NonPresentObjectT gNonPresentObject
Definition STVar.cpp:29
SerializedTypeID
Definition SField.h:93
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49