rippled
STVar.h
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 #ifndef RIPPLE_PROTOCOL_STVAR_H_INCLUDED
21 #define RIPPLE_PROTOCOL_STVAR_H_INCLUDED
22 
23 #include <ripple/protocol/SField.h>
24 #include <ripple/protocol/STBase.h>
25 #include <ripple/protocol/Serializer.h>
26 #include <cstddef>
27 #include <cstdint>
28 #include <typeinfo>
29 #include <utility>
30 
31 namespace ripple {
32 namespace detail {
33 
35 {
36  explicit defaultObject_t() = default;
37 };
38 
40 {
41  explicit nonPresentObject_t() = default;
42 };
43 
46 
47 // "variant" that can hold any type of serialized object
48 // and includes a small-object allocation optimization.
49 class STVar
50 {
51 private:
52  // The largest "small object" we can accomodate
53  static std::size_t constexpr max_size = 72;
54 
56  STBase* p_ = nullptr;
57 
58 public:
59  ~STVar();
60  STVar(STVar const& other);
61  STVar(STVar&& other);
62  STVar&
63  operator=(STVar const& rhs);
64  STVar&
65  operator=(STVar&& rhs);
66 
68  {
69  p_ = t.move(max_size, &d_);
70  }
71 
72  STVar(STBase const& t)
73  {
74  p_ = t.copy(max_size, &d_);
75  }
76 
77  STVar(defaultObject_t, SField const& name);
78  STVar(nonPresentObject_t, SField const& name);
79  STVar(SerialIter& sit, SField const& name, int depth = 0);
80 
81  STBase&
82  get()
83  {
84  return *p_;
85  }
86  STBase&
88  {
89  return get();
90  }
91  STBase*
93  {
94  return &get();
95  }
96  STBase const&
97  get() const
98  {
99  return *p_;
100  }
101  STBase const&
102  operator*() const
103  {
104  return get();
105  }
106  STBase const*
107  operator->() const
108  {
109  return &get();
110  }
111 
112  template <class T, class... Args>
113  friend STVar
114  make_stvar(Args&&... args);
115 
116 private:
117  STVar() = default;
118 
119  STVar(SerializedTypeID id, SField const& name);
120 
121  void
122  destroy();
123 
124  template <class T, class... Args>
125  void
126  construct(Args&&... args)
127  {
128  if (sizeof(T) > max_size)
129  p_ = new T(std::forward<Args>(args)...);
130  else
131  p_ = new (&d_) T(std::forward<Args>(args)...);
132  }
133 
134  bool
135  on_heap() const
136  {
137  return static_cast<void const*>(p_) != static_cast<void const*>(&d_);
138  }
139 };
140 
141 template <class T, class... Args>
142 inline STVar
143 make_stvar(Args&&... args)
144 {
145  STVar st;
146  st.construct<T>(std::forward<Args>(args)...);
147  return st;
148 }
149 
150 inline bool
151 operator==(STVar const& lhs, STVar const& rhs)
152 {
153  return lhs.get().isEquivalent(rhs.get());
154 }
155 
156 inline bool
157 operator!=(STVar const& lhs, STVar const& rhs)
158 {
159  return !(lhs == rhs);
160 }
161 
162 } // namespace detail
163 } // namespace ripple
164 
165 #endif
ripple::detail::defaultObject_t::defaultObject_t
defaultObject_t()=default
ripple::detail::STVar::max_size
static constexpr std::size_t max_size
Definition: STVar.h:53
ripple::detail::nonPresentObject_t::nonPresentObject_t
nonPresentObject_t()=default
ripple::detail::defaultObject
defaultObject_t defaultObject
Definition: STVar.cpp:36
ripple::detail::STVar::operator=
STVar & operator=(STVar const &rhs)
Definition: STVar.cpp:66
ripple::STBase::isEquivalent
virtual bool isEquivalent(STBase const &t) const
Definition: STBase.cpp:112
utility
ripple::detail::defaultObject_t
Definition: STVar.h:34
ripple::detail::STVar::operator->
STBase const * operator->() const
Definition: STVar.h:107
ripple::STBase::copy
virtual STBase * copy(std::size_t n, void *buf) const
Definition: STBase.cpp:57
ripple::detail::STVar::operator*
STBase const & operator*() const
Definition: STVar.h:102
ripple::detail::STVar::make_stvar
friend STVar make_stvar(Args &&... args)
Definition: STVar.h:143
ripple::detail::operator==
bool operator==(STVar const &lhs, STVar const &rhs)
Definition: STVar.h:151
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::detail::STVar::p_
STBase * p_
Definition: STVar.h:56
std::aligned_storage
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::STVar::operator*
STBase & operator*()
Definition: STVar.h:87
ripple::detail::nonPresentObject
nonPresentObject_t nonPresentObject
Definition: STVar.cpp:37
cstddef
ripple::detail::STVar::destroy
void destroy()
Definition: STVar.cpp:221
ripple::detail::STVar::operator->
STBase * operator->()
Definition: STVar.h:92
ripple::detail::STVar::construct
void construct(Args &&... args)
Definition: STVar.h:126
cstdint
ripple::SerialIter
Definition: Serializer.h:310
ripple::detail::STVar::STVar
STVar(STBase &&t)
Definition: STVar.h:67
ripple::detail::make_stvar
STVar make_stvar(Args &&... args)
Definition: STVar.h:143
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::detail::operator!=
bool operator!=(STVar const &lhs, STVar const &rhs)
Definition: STVar.h:157
ripple::SField
Identifies fields.
Definition: SField.h:112
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:66
ripple::detail::STVar::get
STBase & get()
Definition: STVar.h:82
typeinfo
ripple::detail::STVar::STVar
STVar(STBase const &t)
Definition: STVar.h:72
ripple::STBase::move
virtual STBase * move(std::size_t n, void *buf)
Definition: STBase.cpp:63
std::size_t
ripple::detail::STVar::get
STBase const & get() const
Definition: STVar.h:97
ripple::detail::STVar
Definition: STVar.h:49
ripple::detail::STVar::~STVar
~STVar()
Definition: STVar.cpp:41
ripple::detail::STVar::on_heap
bool on_heap() const
Definition: STVar.h:135
ripple::detail::STVar::STVar
STVar()=default