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