rippled
Loading...
Searching...
No Matches
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 <xrpl/protocol/SField.h>
24#include <xrpl/protocol/STBase.h>
25#include <xrpl/protocol/Serializer.h>
26
27#include <cstddef>
28#include <type_traits>
29
30namespace ripple {
31namespace detail {
32
34{
35 explicit defaultObject_t() = default;
36};
37
39{
40 explicit nonPresentObject_t() = default;
41};
42
45
46// Concept to constrain STVar constructors, which
47// instantiate ST* types from SerializedTypeID
48// clang-format off
49template <typename... Args>
57// clang-format on
58
59// "variant" that can hold any type of serialized object
60// and includes a small-object allocation optimization.
61class STVar
62{
63private:
64 // The largest "small object" we can accomodate
65 static std::size_t constexpr max_size = 72;
66
68 STBase* p_ = nullptr;
69
70public:
71 ~STVar();
72 STVar(STVar const& other);
73 STVar(STVar&& other);
74 STVar&
75 operator=(STVar const& rhs);
76 STVar&
77 operator=(STVar&& rhs);
78
80 {
81 p_ = t.move(max_size, &d_);
82 }
83
84 STVar(STBase const& t)
85 {
86 p_ = t.copy(max_size, &d_);
87 }
88
89 STVar(defaultObject_t, SField const& name);
90 STVar(nonPresentObject_t, SField const& name);
91 STVar(SerialIter& sit, SField const& name, int depth = 0);
92
93 STBase&
95 {
96 return *p_;
97 }
98 STBase&
100 {
101 return get();
102 }
103 STBase*
105 {
106 return &get();
107 }
108 STBase const&
109 get() const
110 {
111 return *p_;
112 }
113 STBase const&
114 operator*() const
115 {
116 return get();
117 }
118 STBase const*
120 {
121 return &get();
122 }
123
124 template <class T, class... Args>
125 friend STVar
126 make_stvar(Args&&... args);
127
128private:
129 STVar() = default;
130
131 STVar(SerializedTypeID id, SField const& name);
132
133 void
134 destroy();
135
136 template <class T, class... Args>
137 void
138 construct(Args&&... args)
139 {
140 if constexpr (sizeof(T) > max_size)
141 p_ = new T(std::forward<Args>(args)...);
142 else
143 p_ = new (&d_) T(std::forward<Args>(args)...);
144 }
145
150 template <typename... Args>
151 requires ValidConstructSTArgs<Args...>
152 void
153 constructST(SerializedTypeID id, int depth, Args&&... arg);
154
155 bool
156 on_heap() const
157 {
158 return static_cast<void const*>(p_) != static_cast<void const*>(&d_);
159 }
160};
161
162template <class T, class... Args>
163inline STVar
164make_stvar(Args&&... args)
165{
166 STVar st;
167 st.construct<T>(std::forward<Args>(args)...);
168 return st;
169}
170
171inline bool
172operator==(STVar const& lhs, STVar const& rhs)
173{
174 return lhs.get().isEquivalent(rhs.get());
175}
176
177inline bool
178operator!=(STVar const& lhs, STVar const& rhs)
179{
180 return !(lhs == rhs);
181}
182
183} // namespace detail
184} // namespace ripple
185
186#endif
Identifies fields.
Definition SField.h:146
A type which can be exported to a well known binary format.
Definition STBase.h:135
virtual STBase * move(std::size_t n, void *buf)
Definition STBase.cpp:69
virtual bool isEquivalent(STBase const &t) const
Definition STBase.cpp:121
virtual STBase * copy(std::size_t n, void *buf) const
Definition STBase.cpp:63
STBase & operator*()
Definition STVar.h:99
STVar & operator=(STVar const &rhs)
Definition STVar.cpp:77
std::aligned_storage< max_size >::type d_
Definition STVar.h:67
void construct(Args &&... args)
Definition STVar.h:138
void constructST(SerializedTypeID id, int depth, Args &&... arg)
Construct requested Serializable Type according to id.
Definition STVar.cpp:149
STBase const & operator*() const
Definition STVar.h:114
STVar(STBase const &t)
Definition STVar.h:84
static std::size_t constexpr max_size
Definition STVar.h:65
STBase & get()
Definition STVar.h:94
STBase * operator->()
Definition STVar.h:104
bool on_heap() const
Definition STVar.h:156
STBase const * operator->() const
Definition STVar.h:119
friend STVar make_stvar(Args &&... args)
Definition STVar.h:164
STBase const & get() const
Definition STVar.h:109
STVar(STBase &&t)
Definition STVar.h:79
T is_same_v
STVar make_stvar(Args &&... args)
Definition STVar.h:164
bool operator==(STVar const &lhs, STVar const &rhs)
Definition STVar.h:172
nonPresentObject_t nonPresentObject
Definition STVar.cpp:48
bool operator!=(STVar const &lhs, STVar const &rhs)
Definition STVar.h:178
defaultObject_t defaultObject
Definition STVar.cpp:47
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
SerializedTypeID
Definition SField.h:110