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/Serializer.h>
24 #include <ripple/protocol/SField.h>
25 #include <ripple/protocol/STBase.h>
26 #include <cstddef>
27 #include <cstdint>
28 #include <utility>
29 #include <typeinfo>
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& operator= (STVar const& rhs);
63  STVar& operator= (STVar&& rhs);
64 
65  STVar (STBase&& t)
66  {
67  p_ = t.move(max_size, &d_);
68  }
69 
70  STVar (STBase const& t)
71  {
72  p_ = t.copy(max_size, &d_);
73  }
74 
75  STVar (defaultObject_t, SField const& name);
76  STVar (nonPresentObject_t, SField const& name);
77  STVar (SerialIter& sit, SField const& name, int depth = 0);
78 
79  STBase& get() { return *p_; }
80  STBase& operator*() { return get(); }
81  STBase* operator->() { return &get(); }
82  STBase const& get() const { return *p_; }
83  STBase const& operator*() const { return get(); }
84  STBase const* operator->() const { return &get(); }
85 
86  template <class T, class... Args>
87  friend
88  STVar
89  make_stvar(Args&&... args);
90 
91 private:
92  STVar() = default;
93 
94  STVar (SerializedTypeID id, SField const& name);
95 
96  void destroy();
97 
98  template <class T, class... Args>
99  void
100  construct(Args&&... args)
101  {
102  if(sizeof(T) > max_size)
103  p_ = new T(std::forward<Args>(args)...);
104  else
105  p_ = new(&d_) T(std::forward<Args>(args)...);
106  }
107 
108  bool
109  on_heap() const
110  {
111  return static_cast<void const*>(p_) !=
112  static_cast<void const*>(&d_);
113  }
114 };
115 
116 template <class T, class... Args>
117 inline
118 STVar
119 make_stvar(Args&&... args)
120 {
121  STVar st;
122  st.construct<T>(std::forward<Args>(args)...);
123  return st;
124 }
125 
126 inline
127 bool
128 operator== (STVar const& lhs, STVar const& rhs)
129 {
130  return lhs.get().isEquivalent(rhs.get());
131 }
132 
133 inline
134 bool
135 operator!= (STVar const& lhs, STVar const& rhs)
136 {
137  return ! (lhs == rhs);
138 }
139 
140 } // detail
141 } // ripple
142 
143 #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:111
utility
ripple::detail::defaultObject_t
Definition: STVar.h:34
ripple::detail::STVar::operator->
STBase const * operator->() const
Definition: STVar.h:84
ripple::detail::STVar::operator*
STBase const & operator*() const
Definition: STVar.h:83
ripple::detail::STVar::make_stvar
friend STVar make_stvar(Args &&... args)
Definition: STVar.h:119
ripple::detail::operator==
bool operator==(STVar const &lhs, STVar const &rhs)
Definition: STVar.h:128
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::STBase::move
virtual STBase * move(std::size_t n, void *buf)
Definition: STBase.h:90
ripple::detail::STVar::p_
STBase * p_
Definition: STVar.h:56
std::aligned_storage
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::STVar::operator*
STBase & operator*()
Definition: STVar.h:80
ripple::detail::nonPresentObject
nonPresentObject_t nonPresentObject
Definition: STVar.cpp:37
cstddef
ripple::detail::STVar::destroy
void destroy()
Definition: STVar.cpp:162
ripple::detail::STVar::operator->
STBase * operator->()
Definition: STVar.h:81
ripple::detail::STVar::construct
void construct(Args &&... args)
Definition: STVar.h:100
cstdint
ripple::SerialIter
Definition: Serializer.h:311
ripple::detail::STVar::STVar
STVar(STBase &&t)
Definition: STVar.h:65
ripple::detail::make_stvar
STVar make_stvar(Args &&... args)
Definition: STVar.h:119
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:135
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:65
ripple::detail::STVar::get
STBase & get()
Definition: STVar.h:79
typeinfo
ripple::detail::STVar::STVar
STVar(STBase const &t)
Definition: STVar.h:70
std::size_t
ripple::detail::STVar::get
STBase const & get() const
Definition: STVar.h:82
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:109
ripple::detail::STVar::STVar
STVar()=default