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