rippled
Loading...
Searching...
No Matches
STVar.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/contract.h>
21#include <xrpl/beast/utility/instrumentation.h>
22#include <xrpl/protocol/SField.h>
23#include <xrpl/protocol/STAccount.h>
24#include <xrpl/protocol/STAmount.h>
25#include <xrpl/protocol/STArray.h>
26#include <xrpl/protocol/STBase.h>
27#include <xrpl/protocol/STBitString.h>
28#include <xrpl/protocol/STBlob.h>
29#include <xrpl/protocol/STCurrency.h>
30#include <xrpl/protocol/STInteger.h>
31#include <xrpl/protocol/STIssue.h>
32#include <xrpl/protocol/STNumber.h>
33#include <xrpl/protocol/STObject.h>
34#include <xrpl/protocol/STPathSet.h>
35#include <xrpl/protocol/STVector256.h>
36#include <xrpl/protocol/STXChainBridge.h>
37#include <xrpl/protocol/Serializer.h>
38#include <xrpl/protocol/detail/STVar.h>
39
40#include <stdexcept>
41#include <tuple>
42#include <type_traits>
43
44namespace ripple {
45namespace detail {
46
49
50//------------------------------------------------------------------------------
51
53{
54 destroy();
55}
56
57STVar::STVar(STVar const& other)
58{
59 if (other.p_ != nullptr)
60 p_ = other.p_->copy(max_size, &d_);
61}
62
64{
65 if (other.on_heap())
66 {
67 p_ = other.p_;
68 other.p_ = nullptr;
69 }
70 else
71 {
72 p_ = other.p_->move(max_size, &d_);
73 }
74}
75
76STVar&
78{
79 if (&rhs != this)
80 {
81 destroy();
82 if (rhs.p_)
83 p_ = rhs.p_->copy(max_size, &d_);
84 else
85 p_ = nullptr;
86 }
87
88 return *this;
89}
90
91STVar&
93{
94 if (&rhs != this)
95 {
96 destroy();
97 if (rhs.on_heap())
98 {
99 p_ = rhs.p_;
100 rhs.p_ = nullptr;
101 }
102 else
103 {
104 p_ = rhs.p_->move(max_size, &d_);
105 }
106 }
107
108 return *this;
109}
110
111STVar::STVar(defaultObject_t, SField const& name) : STVar(name.fieldType, name)
112{
113}
114
116 : STVar(STI_NOTPRESENT, name)
117{
118}
119
120STVar::STVar(SerialIter& sit, SField const& name, int depth)
121{
122 if (depth > 10)
123 Throw<std::runtime_error>("Maximum nesting depth of STVar exceeded");
124 constructST(name.fieldType, depth, sit, name);
125}
126
128{
129 XRPL_ASSERT(
130 (id == STI_NOTPRESENT) || (id == name.fieldType),
131 "ripple::detail::STVar::STVar(SerializedTypeID) : valid type input");
132 constructST(id, 0, name);
133}
134
135void
137{
138 if (on_heap())
139 delete p_;
140 else
141 p_->~STBase();
142
143 p_ = nullptr;
144}
145
146template <typename... Args>
147 requires ValidConstructSTArgs<Args...>
148void
149STVar::constructST(SerializedTypeID id, int depth, Args&&... args)
150{
151 auto constructWithDepth = [&]<typename T>() {
152 if constexpr (std::is_same_v<
155 {
156 construct<T>(std::forward<Args>(args)...);
157 }
158 else if constexpr (std::is_same_v<
161 {
162 construct<T>(std::forward<Args>(args)..., depth);
163 }
164 else
165 {
166 constexpr bool alwaysFalse =
167 !std::is_same_v<std::tuple<Args...>, std::tuple<Args...>>;
168 static_assert(alwaysFalse, "Invalid STVar constructor arguments");
169 }
170 };
171
172 switch (id)
173 {
174 case STI_NOTPRESENT: {
175 // Last argument is always SField
176 SField const& field =
177 std::get<sizeof...(args) - 1>(std::forward_as_tuple(args...));
178 construct<STBase>(field);
179 return;
180 }
181 case STI_UINT8:
182 construct<STUInt8>(std::forward<Args>(args)...);
183 return;
184 case STI_UINT16:
185 construct<STUInt16>(std::forward<Args>(args)...);
186 return;
187 case STI_UINT32:
188 construct<STUInt32>(std::forward<Args>(args)...);
189 return;
190 case STI_UINT64:
191 construct<STUInt64>(std::forward<Args>(args)...);
192 return;
193 case STI_AMOUNT:
194 construct<STAmount>(std::forward<Args>(args)...);
195 return;
196 case STI_NUMBER:
197 construct<STNumber>(std::forward<Args>(args)...);
198 return;
199 case STI_UINT128:
200 construct<STUInt128>(std::forward<Args>(args)...);
201 return;
202 case STI_UINT160:
203 construct<STUInt160>(std::forward<Args>(args)...);
204 return;
205 case STI_UINT192:
206 construct<STUInt192>(std::forward<Args>(args)...);
207 return;
208 case STI_UINT256:
209 construct<STUInt256>(std::forward<Args>(args)...);
210 return;
211 case STI_INT32:
212 construct<STInt32>(std::forward<Args>(args)...);
213 return;
214 case STI_VECTOR256:
215 construct<STVector256>(std::forward<Args>(args)...);
216 return;
217 case STI_VL:
218 construct<STBlob>(std::forward<Args>(args)...);
219 return;
220 case STI_ACCOUNT:
221 construct<STAccount>(std::forward<Args>(args)...);
222 return;
223 case STI_PATHSET:
224 construct<STPathSet>(std::forward<Args>(args)...);
225 return;
226 case STI_OBJECT:
227 constructWithDepth.template operator()<STObject>();
228 return;
229 case STI_ARRAY:
230 constructWithDepth.template operator()<STArray>();
231 return;
232 case STI_ISSUE:
233 construct<STIssue>(std::forward<Args>(args)...);
234 return;
235 case STI_XCHAIN_BRIDGE:
236 construct<STXChainBridge>(std::forward<Args>(args)...);
237 return;
238 case STI_CURRENCY:
239 construct<STCurrency>(std::forward<Args>(args)...);
240 return;
241 default:
242 Throw<std::runtime_error>("Unknown object type");
243 }
244}
245
246} // namespace detail
247} // namespace ripple
Identifies fields.
Definition SField.h:146
virtual STBase * move(std::size_t n, void *buf)
Definition STBase.cpp:69
virtual ~STBase()=default
virtual STBase * copy(std::size_t n, void *buf) const
Definition STBase.cpp:63
STVar & operator=(STVar const &rhs)
Definition STVar.cpp:77
std::aligned_storage< max_size >::type d_
Definition STVar.h:67
void constructST(SerializedTypeID id, int depth, Args &&... arg)
Construct requested Serializable Type according to id.
Definition STVar.cpp:149
static std::size_t constexpr max_size
Definition STVar.h:65
bool on_heap() const
Definition STVar.h:156
T forward_as_tuple(T... args)
T is_same_v
nonPresentObject_t nonPresentObject
Definition STVar.cpp:48
defaultObject_t defaultObject
Definition STVar.cpp:47
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
SerializedTypeID
Definition SField.h:110