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