rippled
Loading...
Searching...
No Matches
STVar.cpp
1#include <xrpl/basics/contract.h>
2#include <xrpl/beast/utility/instrumentation.h>
3#include <xrpl/protocol/SField.h>
4#include <xrpl/protocol/STAccount.h>
5#include <xrpl/protocol/STAmount.h>
6#include <xrpl/protocol/STArray.h>
7#include <xrpl/protocol/STBase.h>
8#include <xrpl/protocol/STBitString.h>
9#include <xrpl/protocol/STBlob.h>
10#include <xrpl/protocol/STCurrency.h>
11#include <xrpl/protocol/STInteger.h>
12#include <xrpl/protocol/STIssue.h>
13#include <xrpl/protocol/STNumber.h>
14#include <xrpl/protocol/STObject.h>
15#include <xrpl/protocol/STPathSet.h>
16#include <xrpl/protocol/STVector256.h>
17#include <xrpl/protocol/STXChainBridge.h>
18#include <xrpl/protocol/Serializer.h>
19#include <xrpl/protocol/detail/STVar.h>
20
21#include <stdexcept>
22#include <tuple>
23#include <type_traits>
24
25namespace xrpl {
26namespace 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(max_size, &d_);
42}
43
45{
46 if (other.on_heap())
47 {
48 p_ = other.p_;
49 other.p_ = nullptr;
50 }
51 else
52 {
53 p_ = other.p_->move(max_size, &d_);
54 }
55}
56
57STVar&
59{
60 if (&rhs != this)
61 {
62 destroy();
63 if (rhs.p_ != nullptr)
64 {
65 p_ = rhs.p_->copy(max_size, &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.on_heap())
83 {
84 p_ = rhs.p_;
85 rhs.p_ = nullptr;
86 }
87 else
88 {
89 p_ = rhs.p_->move(max_size, &d_);
90 }
91 }
92
93 return *this;
94}
95
96STVar::STVar(defaultObject_t, SField const& name) : STVar(name.fieldType, name)
97{
98}
99
100STVar::STVar(nonPresentObject_t, 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 (on_heap())
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 {
142 construct<T>(std::forward<Args>(args)...);
143 }
144 else if constexpr (
145 std::
147 {
148 construct<T>(std::forward<Args>(args)..., depth);
149 }
150 else
151 {
152 constexpr bool alwaysFalse = !std::is_same_v<std::tuple<Args...>, std::tuple<Args...>>;
153 static_assert(alwaysFalse, "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_XCHAIN_BRIDGE:
220 construct<STXChainBridge>(std::forward<Args>(args)...);
221 return;
222 case STI_CURRENCY:
223 construct<STCurrency>(std::forward<Args>(args)...);
224 return;
225 default:
226 Throw<std::runtime_error>("Unknown object type");
227 }
228}
229
230} // namespace detail
231} // namespace xrpl
Identifies fields.
Definition SField.h:126
virtual STBase * copy(std::size_t n, void *buf) const
Definition STBase.cpp:47
virtual STBase * move(std::size_t n, void *buf)
Definition STBase.cpp:53
virtual ~STBase()=default
void constructST(SerializedTypeID id, int depth, Args &&... arg)
Construct requested Serializable Type according to id.
Definition STVar.cpp:137
std::aligned_storage< max_size >::type d_
Definition STVar.h:41
STBase * p_
Definition STVar.h:42
bool on_heap() const
Definition STVar.h:130
STVar & operator=(STVar const &rhs)
Definition STVar.cpp:58
static std::size_t constexpr max_size
Definition STVar.h:39
T forward_as_tuple(T... args)
T is_same_v
nonPresentObject_t nonPresentObject
Definition STVar.cpp:29
defaultObject_t defaultObject
Definition STVar.cpp:28
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
SerializedTypeID
Definition SField.h:90