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