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/Serializer.h>
20
21#include <stdexcept>
22#include <tuple>
23#include <type_traits>
24
25namespace xrpl::detail {
26
29
30//------------------------------------------------------------------------------
31
33{
34 destroy();
35}
36
37STVar::STVar(STVar const& other)
38{
39 if (other.p_ != nullptr)
40 p_ = other.p_->copy(kMaxSize, &d_);
41}
42
44{
45 if (other.onHeap())
46 {
47 p_ = other.p_;
48 other.p_ = nullptr;
49 }
50 else
51 {
52 p_ = other.p_->move(kMaxSize, &d_);
53 }
54}
55
56STVar&
58{
59 if (&rhs != this)
60 {
61 destroy();
62 if (rhs.p_ != nullptr)
63 {
64 p_ = rhs.p_->copy(kMaxSize, &d_);
65 }
66 else
67 {
68 p_ = nullptr;
69 }
70 }
71
72 return *this;
73}
74
75STVar&
77{
78 if (&rhs != this)
79 {
80 destroy();
81 if (rhs.onHeap())
82 {
83 p_ = rhs.p_;
84 rhs.p_ = nullptr;
85 }
86 else
87 {
88 p_ = rhs.p_->move(kMaxSize, &d_);
89 }
90 }
91
92 return *this;
93}
94
95STVar::STVar(DefaultObjectT, SField const& name) : STVar(name.fieldType, name)
96{
97}
98
99STVar::STVar(NonPresentObjectT, SField const& name) : STVar(STI_NOTPRESENT, name)
100{
101}
102
103STVar::STVar(SerialIter& sit, SField const& name, int depth)
104{
105 if (depth > 10)
106 Throw<std::runtime_error>("Maximum nesting depth of STVar exceeded");
107 constructST(name.fieldType, depth, sit, name);
108}
109
111{
112 XRPL_ASSERT(
113 (id == STI_NOTPRESENT) || (id == name.fieldType),
114 "xrpl::detail::STVar::STVar(SerializedTypeID) : valid type input");
115 constructST(id, 0, name);
116}
117
118void
120{
121 if (onHeap())
122 {
123 delete p_;
124 }
125 else
126 {
127 p_->~STBase();
128 }
129
130 p_ = nullptr;
131}
132
133template <typename... Args>
134 requires ValidConstructSTArgs<Args...>
135void
136STVar::constructST(SerializedTypeID id, int depth, Args&&... args)
137{
138 auto const constructWithDepth = [&]<typename T> {
140 {
142 }
143 else if constexpr (
144 std::
145 is_same_v<std::tuple<std::remove_cvref_t<Args>...>, std::tuple<SerialIter, SField>>)
146 {
147 construct<T>(std::forward<Args>(args)..., depth);
148 }
149 else
150 {
151 static constexpr bool kAlwaysFalse =
152 !std::is_same_v<std::tuple<Args...>, std::tuple<Args...>>;
153 static_assert(kAlwaysFalse, "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:
167 return;
168 case STI_UINT16:
170 return;
171 case STI_UINT32:
173 return;
174 case STI_UINT64:
176 return;
177 case STI_AMOUNT:
179 return;
180 case STI_NUMBER:
182 return;
183 case STI_UINT128:
185 return;
186 case STI_UINT160:
188 return;
189 case STI_UINT192:
191 return;
192 case STI_UINT256:
194 return;
195 case STI_INT32:
197 return;
198 case STI_VECTOR256:
200 return;
201 case STI_VL:
203 return;
204 case STI_ACCOUNT:
206 return;
207 case STI_PATHSET:
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:
218 return;
219 case STI_CURRENCY:
221 return;
222 default:
223 Throw<std::runtime_error>("Unknown object type");
224 }
225}
226
227} // namespace xrpl::detail
Identifies fields.
Definition SField.h:131
SerializedTypeID const fieldType
Definition SField.h:152
virtual STBase * copy(std::size_t n, void *buf) const
Definition STBase.cpp:42
void constructST(SerializedTypeID id, int depth, Args &&... arg)
Construct requested Serializable Type according to id.
Definition STVar.cpp:136
std::byte d_[kMaxSize]
Definition STVar.h:42
bool onHeap() const
Definition STVar.h:135
void construct(Args &&... args)
Definition STVar.h:112
STVar(STVar const &other)
Definition STVar.cpp:37
static constexpr std::size_t kMaxSize
Definition STVar.h:39
STBase * p_
Definition STVar.h:43
STVar & operator=(STVar const &rhs)
Definition STVar.cpp:57
T forward_as_tuple(T... args)
T forward(T... args)
T is_same_v
DefaultObjectT gDefaultObject
Definition STVar.cpp:27
NonPresentObjectT gNonPresentObject
Definition STVar.cpp:28
SerializedTypeID
Definition SField.h:93
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52