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/STObject.h>
33#include <xrpl/protocol/STPathSet.h>
34#include <xrpl/protocol/STVector256.h>
35#include <xrpl/protocol/STXChainBridge.h>
36#include <xrpl/protocol/Serializer.h>
37#include <xrpl/protocol/detail/STVar.h>
38
39#include <stdexcept>
40#include <tuple>
41#include <type_traits>
42
43namespace ripple {
44namespace detail {
45
48
49//------------------------------------------------------------------------------
50
52{
53 destroy();
54}
55
56STVar::STVar(STVar const& other)
57{
58 if (other.p_ != nullptr)
59 p_ = other.p_->copy(max_size, &d_);
60}
61
63{
64 if (other.on_heap())
65 {
66 p_ = other.p_;
67 other.p_ = nullptr;
68 }
69 else
70 {
71 p_ = other.p_->move(max_size, &d_);
72 }
73}
74
75STVar&
77{
78 if (&rhs != this)
79 {
80 destroy();
81 if (rhs.p_)
82 p_ = rhs.p_->copy(max_size, &d_);
83 else
84 p_ = nullptr;
85 }
86
87 return *this;
88}
89
90STVar&
92{
93 if (&rhs != this)
94 {
95 destroy();
96 if (rhs.on_heap())
97 {
98 p_ = rhs.p_;
99 rhs.p_ = nullptr;
100 }
101 else
102 {
103 p_ = rhs.p_->move(max_size, &d_);
104 }
105 }
106
107 return *this;
108}
109
110STVar::STVar(defaultObject_t, SField const& name) : STVar(name.fieldType, name)
111{
112}
113
115 : STVar(STI_NOTPRESENT, name)
116{
117}
118
119STVar::STVar(SerialIter& sit, SField const& name, int depth)
120{
121 if (depth > 10)
122 Throw<std::runtime_error>("Maximum nesting depth of STVar exceeded");
123 constructST(name.fieldType, depth, sit, name);
124}
125
127{
128 XRPL_ASSERT(
129 (id == STI_NOTPRESENT) || (id == name.fieldType),
130 "ripple::detail::STVar::STVar(SerializedTypeID) : valid type input");
131 constructST(id, 0, name);
132}
133
134void
136{
137 if (on_heap())
138 delete p_;
139 else
140 p_->~STBase();
141
142 p_ = nullptr;
143}
144
145template <typename... Args>
146 requires ValidConstructSTArgs<Args...>
147void
148STVar::constructST(SerializedTypeID id, int depth, Args&&... args)
149{
150 auto constructWithDepth = [&]<typename T>() {
151 if constexpr (std::is_same_v<
154 {
155 construct<T>(std::forward<Args>(args)...);
156 }
157 else if constexpr (std::is_same_v<
160 {
161 construct<T>(std::forward<Args>(args)..., depth);
162 }
163 else
164 {
165 constexpr bool alwaysFalse =
166 !std::is_same_v<std::tuple<Args...>, std::tuple<Args...>>;
167 static_assert(alwaysFalse, "Invalid STVar constructor arguments");
168 }
169 };
170
171 switch (id)
172 {
173 case STI_NOTPRESENT: {
174 // Last argument is always SField
175 SField const& field =
176 std::get<sizeof...(args) - 1>(std::forward_as_tuple(args...));
177 construct<STBase>(field);
178 return;
179 }
180 case STI_UINT8:
181 construct<STUInt8>(std::forward<Args>(args)...);
182 return;
183 case STI_UINT16:
184 construct<STUInt16>(std::forward<Args>(args)...);
185 return;
186 case STI_UINT32:
187 construct<STUInt32>(std::forward<Args>(args)...);
188 return;
189 case STI_UINT64:
190 construct<STUInt64>(std::forward<Args>(args)...);
191 return;
192 case STI_AMOUNT:
193 construct<STAmount>(std::forward<Args>(args)...);
194 return;
195 case STI_UINT128:
196 construct<STUInt128>(std::forward<Args>(args)...);
197 return;
198 case STI_UINT160:
199 construct<STUInt160>(std::forward<Args>(args)...);
200 return;
201 case STI_UINT192:
202 construct<STUInt192>(std::forward<Args>(args)...);
203 return;
204 case STI_UINT256:
205 construct<STUInt256>(std::forward<Args>(args)...);
206 return;
207 case STI_VECTOR256:
208 construct<STVector256>(std::forward<Args>(args)...);
209 return;
210 case STI_VL:
211 construct<STBlob>(std::forward<Args>(args)...);
212 return;
213 case STI_ACCOUNT:
214 construct<STAccount>(std::forward<Args>(args)...);
215 return;
216 case STI_PATHSET:
217 construct<STPathSet>(std::forward<Args>(args)...);
218 return;
219 case STI_OBJECT:
220 constructWithDepth.template operator()<STObject>();
221 return;
222 case STI_ARRAY:
223 constructWithDepth.template operator()<STArray>();
224 return;
225 case STI_ISSUE:
226 construct<STIssue>(std::forward<Args>(args)...);
227 return;
228 case STI_XCHAIN_BRIDGE:
229 construct<STXChainBridge>(std::forward<Args>(args)...);
230 return;
231 case STI_CURRENCY:
232 construct<STCurrency>(std::forward<Args>(args)...);
233 return;
234 default:
235 Throw<std::runtime_error>("Unknown object type");
236 }
237}
238
239} // namespace detail
240} // namespace ripple
Identifies fields.
Definition: SField.h:144
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:76
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:148
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:47
defaultObject_t defaultObject
Definition: STVar.cpp:46
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
SerializedTypeID
Definition: SField.h:108