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