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 
101  : STVar(name.fieldType, name)
102 {
103 }
104 
106  : STVar(STI_NOTPRESENT, name)
107 {
108 }
109 
110 STVar::STVar (SerialIter& sit, SField const& name, int depth)
111 {
112  if (depth > 10)
113  Throw<std::runtime_error> ("Maximum nesting depth of STVar exceeded");
114  switch (name.fieldType)
115  {
116  case STI_NOTPRESENT: construct<STBase>(name); return;
117  case STI_UINT8: construct<STUInt8>(sit, name); return;
118  case STI_UINT16: construct<STUInt16>(sit, name); return;
119  case STI_UINT32: construct<STUInt32>(sit, name); return;
120  case STI_UINT64: construct<STUInt64>(sit, name); return;
121  case STI_AMOUNT: construct<STAmount>(sit, name); return;
122  case STI_HASH128: construct<STHash128>(sit, name); return;
123  case STI_HASH160: construct<STHash160>(sit, name); return;
124  case STI_HASH256: construct<STHash256>(sit, name); return;
125  case STI_VECTOR256: construct<STVector256>(sit, name); return;
126  case STI_VL: construct<STBlob>(sit, name); return;
127  case STI_ACCOUNT: construct<STAccount>(sit, name); return;
128  case STI_PATHSET: construct<STPathSet>(sit, name); return;
129  case STI_OBJECT: construct<STObject>(sit, name, depth); return;
130  case STI_ARRAY: construct<STArray>(sit, name, depth); return;
131  default:
132  Throw<std::runtime_error> ("Unknown object type");
133  }
134 }
135 
137 {
138  assert ((id == STI_NOTPRESENT) || (id == name.fieldType));
139  switch (id)
140  {
141  case STI_NOTPRESENT: construct<STBase>(name); return;
142  case STI_UINT8: construct<STUInt8>(name); return;
143  case STI_UINT16: construct<STUInt16>(name); return;
144  case STI_UINT32: construct<STUInt32>(name); return;
145  case STI_UINT64: construct<STUInt64>(name); return;
146  case STI_AMOUNT: construct<STAmount>(name); return;
147  case STI_HASH128: construct<STHash128>(name); return;
148  case STI_HASH160: construct<STHash160>(name); return;
149  case STI_HASH256: construct<STHash256>(name); return;
150  case STI_VECTOR256: construct<STVector256>(name); return;
151  case STI_VL: construct<STBlob>(name); return;
152  case STI_ACCOUNT: construct<STAccount>(name); return;
153  case STI_PATHSET: construct<STPathSet>(name); return;
154  case STI_OBJECT: construct<STObject>(name); return;
155  case STI_ARRAY: construct<STArray>(name); return;
156  default:
157  Throw<std::runtime_error> ("Unknown object type");
158  }
159 }
160 
161 void
163 {
164  if (on_heap())
165  delete p_;
166  else
167  p_->~STBase();
168 
169  p_ = nullptr;
170 }
171 
172 } // detail
173 } // 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::detail::defaultObject_t
Definition: STVar.h:34
ripple::STI_AMOUNT
@ STI_AMOUNT
Definition: SField.h:65
ripple::STI_UINT8
@ STI_UINT8
Definition: SField.h:73
ripple::STI_PATHSET
@ STI_PATHSET
Definition: SField.h:75
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::STBase::move
virtual STBase * move(std::size_t n, void *buf)
Definition: STBase.h:90
ripple::STI_ACCOUNT
@ STI_ACCOUNT
Definition: SField.h:67
ripple::STI_ARRAY
@ STI_ARRAY
Definition: SField.h:70
ripple::STI_HASH160
@ STI_HASH160
Definition: SField.h:74
ripple::detail::STVar::p_
STBase * p_
Definition: STVar.h:56
ripple::SField::fieldType
const SerializedTypeID fieldType
Definition: SField.h:134
ripple::STBase::copy
virtual STBase * copy(std::size_t n, void *buf) const
Definition: STBase.h:83
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:162
ripple::STI_VL
@ STI_VL
Definition: SField.h:66
ripple::STI_UINT16
@ STI_UINT16
Definition: SField.h:60
ripple::STI_HASH256
@ STI_HASH256
Definition: SField.h:64
ripple::SerialIter
Definition: Serializer.h:311
ripple::STI_VECTOR256
@ STI_VECTOR256
Definition: SField.h:76
ripple::STI_UINT32
@ STI_UINT32
Definition: SField.h:61
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:62
ripple::STI_HASH128
@ STI_HASH128
Definition: SField.h:63
ripple::STI_OBJECT
@ STI_OBJECT
Definition: SField.h:69
ripple::detail::STVar
Definition: STVar.h:49
ripple::detail::STVar::~STVar
~STVar()
Definition: STVar.cpp:41
ripple::STI_NOTPRESENT
@ STI_NOTPRESENT
Definition: SField.h:57
ripple::detail::STVar::on_heap
bool on_heap() const
Definition: STVar.h:109
ripple::detail::STVar::STVar
STVar()=default