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