rippled
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 <ripple/protocol/impl/STVar.h>
21 
22 #include <ripple/basics/contract.h>
23 #include <ripple/protocol/STAccount.h>
24 #include <ripple/protocol/STAmount.h>
25 #include <ripple/protocol/STArray.h>
26 #include <ripple/protocol/STBase.h>
27 #include <ripple/protocol/STBitString.h>
28 #include <ripple/protocol/STBlob.h>
29 #include <ripple/protocol/STCurrency.h>
30 #include <ripple/protocol/STInteger.h>
31 #include <ripple/protocol/STIssue.h>
32 #include <ripple/protocol/STObject.h>
33 #include <ripple/protocol/STPathSet.h>
34 #include <ripple/protocol/STVector256.h>
35 #include <ripple/protocol/STXChainBridge.h>
36 #include <ripple/protocol/XChainAttestations.h>
37 
38 namespace ripple {
39 namespace detail {
40 
43 
44 //------------------------------------------------------------------------------
45 
47 {
48  destroy();
49 }
50 
51 STVar::STVar(STVar const& other)
52 {
53  if (other.p_ != nullptr)
54  p_ = other.p_->copy(max_size, &d_);
55 }
56 
58 {
59  if (other.on_heap())
60  {
61  p_ = other.p_;
62  other.p_ = nullptr;
63  }
64  else
65  {
66  p_ = other.p_->move(max_size, &d_);
67  }
68 }
69 
70 STVar&
72 {
73  if (&rhs != this)
74  {
75  destroy();
76  if (rhs.p_)
77  p_ = rhs.p_->copy(max_size, &d_);
78  else
79  p_ = nullptr;
80  }
81 
82  return *this;
83 }
84 
85 STVar&
87 {
88  if (&rhs != this)
89  {
90  destroy();
91  if (rhs.on_heap())
92  {
93  p_ = rhs.p_;
94  rhs.p_ = nullptr;
95  }
96  else
97  {
98  p_ = rhs.p_->move(max_size, &d_);
99  }
100  }
101 
102  return *this;
103 }
104 
105 STVar::STVar(defaultObject_t, SField const& name) : STVar(name.fieldType, name)
106 {
107 }
108 
110  : STVar(STI_NOTPRESENT, name)
111 {
112 }
113 
114 STVar::STVar(SerialIter& sit, SField const& name, int depth)
115 {
116  if (depth > 10)
117  Throw<std::runtime_error>("Maximum nesting depth of STVar exceeded");
118  switch (name.fieldType)
119  {
120  case STI_NOTPRESENT:
121  construct<STBase>(name);
122  return;
123  case STI_UINT8:
124  construct<STUInt8>(sit, name);
125  return;
126  case STI_UINT16:
127  construct<STUInt16>(sit, name);
128  return;
129  case STI_UINT32:
130  construct<STUInt32>(sit, name);
131  return;
132  case STI_UINT64:
133  construct<STUInt64>(sit, name);
134  return;
135  case STI_AMOUNT:
136  construct<STAmount>(sit, name);
137  return;
138  case STI_UINT128:
139  construct<STUInt128>(sit, name);
140  return;
141  case STI_UINT160:
142  construct<STUInt160>(sit, name);
143  return;
144  case STI_UINT256:
145  construct<STUInt256>(sit, name);
146  return;
147  case STI_VECTOR256:
148  construct<STVector256>(sit, name);
149  return;
150  case STI_VL:
151  construct<STBlob>(sit, name);
152  return;
153  case STI_ACCOUNT:
154  construct<STAccount>(sit, name);
155  return;
156  case STI_PATHSET:
157  construct<STPathSet>(sit, name);
158  return;
159  case STI_OBJECT:
160  construct<STObject>(sit, name, depth);
161  return;
162  case STI_ARRAY:
163  construct<STArray>(sit, name, depth);
164  return;
165  case STI_ISSUE:
166  construct<STIssue>(sit, name);
167  return;
168  case STI_XCHAIN_BRIDGE:
169  construct<STXChainBridge>(sit, name);
170  return;
171  case STI_CURRENCY:
172  construct<STCurrency>(sit, name);
173  return;
174  default:
175  Throw<std::runtime_error>("Unknown object type");
176  }
177 }
178 
180 {
181  assert((id == STI_NOTPRESENT) || (id == name.fieldType));
182  switch (id)
183  {
184  case STI_NOTPRESENT:
185  construct<STBase>(name);
186  return;
187  case STI_UINT8:
188  construct<STUInt8>(name);
189  return;
190  case STI_UINT16:
191  construct<STUInt16>(name);
192  return;
193  case STI_UINT32:
194  construct<STUInt32>(name);
195  return;
196  case STI_UINT64:
197  construct<STUInt64>(name);
198  return;
199  case STI_AMOUNT:
200  construct<STAmount>(name);
201  return;
202  case STI_UINT128:
203  construct<STUInt128>(name);
204  return;
205  case STI_UINT160:
206  construct<STUInt160>(name);
207  return;
208  case STI_UINT256:
209  construct<STUInt256>(name);
210  return;
211  case STI_VECTOR256:
212  construct<STVector256>(name);
213  return;
214  case STI_VL:
215  construct<STBlob>(name);
216  return;
217  case STI_ACCOUNT:
218  construct<STAccount>(name);
219  return;
220  case STI_PATHSET:
221  construct<STPathSet>(name);
222  return;
223  case STI_OBJECT:
224  construct<STObject>(name);
225  return;
226  case STI_ARRAY:
227  construct<STArray>(name);
228  return;
229  case STI_ISSUE:
230  construct<STIssue>(name);
231  return;
232  case STI_XCHAIN_BRIDGE:
233  construct<STXChainBridge>(name);
234  return;
235  case STI_CURRENCY:
236  construct<STCurrency>(name);
237  return;
238  default:
239  Throw<std::runtime_error>("Unknown object type");
240  }
241 }
242 
243 void
245 {
246  if (on_heap())
247  delete p_;
248  else
249  p_->~STBase();
250 
251  p_ = nullptr;
252 }
253 
254 } // namespace detail
255 } // namespace ripple
ripple::detail::STVar::max_size
static constexpr std::size_t max_size
Definition: STVar.h:53
ripple::detail::defaultObject
defaultObject_t defaultObject
Definition: STVar.cpp:41
ripple::detail::STVar::operator=
STVar & operator=(STVar const &rhs)
Definition: STVar.cpp:71
ripple::detail::defaultObject_t
Definition: STVar.h:34
ripple::STBase::copy
virtual STBase * copy(std::size_t n, void *buf) const
Definition: STBase.cpp:57
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:106
ripple::detail::STVar::p_
STBase * p_
Definition: STVar.h:56
ripple::detail::nonPresentObject_t
Definition: STVar.h:39
ripple::detail::STVar::d_
std::aligned_storage< max_size >::type d_
Definition: STVar.h:55
ripple::detail::nonPresentObject
nonPresentObject_t nonPresentObject
Definition: STVar.cpp:42
ripple::detail::STVar::destroy
void destroy()
Definition: STVar.cpp:244
ripple::SerialIter
Definition: Serializer.h:311
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::STBase::~STBase
virtual ~STBase()=default
ripple::SField
Identifies fields.
Definition: SField.h:141
ripple::STBase::move
virtual STBase * move(std::size_t n, void *buf)
Definition: STBase.cpp:63
ripple::detail::STVar
Definition: STVar.h:49
ripple::detail::STVar::~STVar
~STVar()
Definition: STVar.cpp:46
ripple::detail::STVar::on_heap
bool on_heap() const
Definition: STVar.h:135
ripple::detail::STVar::STVar
STVar()=default