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 ripple {
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_)
64 p_ = rhs.p_->copy(max_size, &d_);
65 else
66 p_ = nullptr;
67 }
68
69 return *this;
70}
71
72STVar&
74{
75 if (&rhs != this)
76 {
77 destroy();
78 if (rhs.on_heap())
79 {
80 p_ = rhs.p_;
81 rhs.p_ = nullptr;
82 }
83 else
84 {
85 p_ = rhs.p_->move(max_size, &d_);
86 }
87 }
88
89 return *this;
90}
91
92STVar::STVar(defaultObject_t, SField const& name) : STVar(name.fieldType, name)
93{
94}
95
97 : STVar(STI_NOTPRESENT, name)
98{
99}
100
101STVar::STVar(SerialIter& sit, SField const& name, int depth)
102{
103 if (depth > 10)
104 Throw<std::runtime_error>("Maximum nesting depth of STVar exceeded");
105 constructST(name.fieldType, depth, sit, name);
106}
107
109{
110 XRPL_ASSERT(
111 (id == STI_NOTPRESENT) || (id == name.fieldType),
112 "ripple::detail::STVar::STVar(SerializedTypeID) : valid type input");
113 constructST(id, 0, name);
114}
115
116void
118{
119 if (on_heap())
120 delete p_;
121 else
122 p_->~STBase();
123
124 p_ = nullptr;
125}
126
127template <typename... Args>
128 requires ValidConstructSTArgs<Args...>
129void
130STVar::constructST(SerializedTypeID id, int depth, Args&&... args)
131{
132 auto constructWithDepth = [&]<typename T>() {
133 if constexpr (std::is_same_v<
136 {
137 construct<T>(std::forward<Args>(args)...);
138 }
139 else if constexpr (std::is_same_v<
142 {
143 construct<T>(std::forward<Args>(args)..., depth);
144 }
145 else
146 {
147 constexpr bool alwaysFalse =
148 !std::is_same_v<std::tuple<Args...>, std::tuple<Args...>>;
149 static_assert(alwaysFalse, "Invalid STVar constructor arguments");
150 }
151 };
152
153 switch (id)
154 {
155 case STI_NOTPRESENT: {
156 // Last argument is always SField
157 SField const& field =
158 std::get<sizeof...(args) - 1>(std::forward_as_tuple(args...));
159 construct<STBase>(field);
160 return;
161 }
162 case STI_UINT8:
163 construct<STUInt8>(std::forward<Args>(args)...);
164 return;
165 case STI_UINT16:
166 construct<STUInt16>(std::forward<Args>(args)...);
167 return;
168 case STI_UINT32:
169 construct<STUInt32>(std::forward<Args>(args)...);
170 return;
171 case STI_UINT64:
172 construct<STUInt64>(std::forward<Args>(args)...);
173 return;
174 case STI_AMOUNT:
175 construct<STAmount>(std::forward<Args>(args)...);
176 return;
177 case STI_NUMBER:
178 construct<STNumber>(std::forward<Args>(args)...);
179 return;
180 case STI_UINT128:
181 construct<STUInt128>(std::forward<Args>(args)...);
182 return;
183 case STI_UINT160:
184 construct<STUInt160>(std::forward<Args>(args)...);
185 return;
186 case STI_UINT192:
187 construct<STUInt192>(std::forward<Args>(args)...);
188 return;
189 case STI_UINT256:
190 construct<STUInt256>(std::forward<Args>(args)...);
191 return;
192 case STI_INT32:
193 construct<STInt32>(std::forward<Args>(args)...);
194 return;
195 case STI_VECTOR256:
196 construct<STVector256>(std::forward<Args>(args)...);
197 return;
198 case STI_VL:
199 construct<STBlob>(std::forward<Args>(args)...);
200 return;
201 case STI_ACCOUNT:
202 construct<STAccount>(std::forward<Args>(args)...);
203 return;
204 case STI_PATHSET:
205 construct<STPathSet>(std::forward<Args>(args)...);
206 return;
207 case STI_OBJECT:
208 constructWithDepth.template operator()<STObject>();
209 return;
210 case STI_ARRAY:
211 constructWithDepth.template operator()<STArray>();
212 return;
213 case STI_ISSUE:
214 construct<STIssue>(std::forward<Args>(args)...);
215 return;
216 case STI_XCHAIN_BRIDGE:
217 construct<STXChainBridge>(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 detail
228} // namespace ripple
Identifies fields.
Definition SField.h:127
virtual STBase * move(std::size_t n, void *buf)
Definition STBase.cpp:50
virtual ~STBase()=default
virtual STBase * copy(std::size_t n, void *buf) const
Definition STBase.cpp:44
STVar & operator=(STVar const &rhs)
Definition STVar.cpp:58
std::aligned_storage< max_size >::type d_
Definition STVar.h:48
void constructST(SerializedTypeID id, int depth, Args &&... arg)
Construct requested Serializable Type according to id.
Definition STVar.cpp:130
static std::size_t constexpr max_size
Definition STVar.h:46
bool on_heap() const
Definition STVar.h:137
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:6
SerializedTypeID
Definition SField.h:91