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