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
28template <typename... Args>
32
33// "variant" that can hold any type of serialized object
34// and includes a small-object allocation optimization.
35class STVar
36{
37private:
38 // The largest "small object" we can accommodate
39 static std::size_t constexpr max_size = 72;
40
42 STBase* p_ = nullptr;
43
44public:
45 ~STVar();
46 STVar(STVar const& other);
47 STVar(STVar&& other);
48 STVar&
49 operator=(STVar const& rhs);
50 STVar&
51 operator=(STVar&& rhs);
52
53 STVar(STBase&& t) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
54 {
55 p_ = t.move(max_size, &d_);
56 }
57
58 STVar(STBase const& t)
59 {
60 p_ = t.copy(max_size, &d_);
61 }
62
63 STVar(defaultObject_t, SField const& name);
64 STVar(nonPresentObject_t, SField const& name);
65 STVar(SerialIter& sit, SField const& name, int depth = 0);
66
67 STBase&
69 {
70 return *p_;
71 }
72 STBase&
74 {
75 return get();
76 }
77 STBase*
79 {
80 return &get();
81 }
82 STBase const&
83 get() const
84 {
85 return *p_;
86 }
87 STBase const&
88 operator*() const
89 {
90 return get();
91 }
92 STBase const*
93 operator->() const
94 {
95 return &get();
96 }
97
98 template <class T, class... Args>
99 friend STVar
100 make_stvar(Args&&... args);
101
102private:
103 STVar() = default;
104
105 STVar(SerializedTypeID id, SField const& name);
106
107 void
108 destroy();
109
110 template <class T, class... Args>
111 void
112 construct(Args&&... args)
113 {
114 if constexpr (sizeof(T) > max_size)
115 p_ = new T(std::forward<Args>(args)...);
116 else
117 p_ = new (&d_) T(std::forward<Args>(args)...);
118 }
119
124 template <typename... Args>
125 requires ValidConstructSTArgs<Args...>
126 void
127 constructST(SerializedTypeID id, int depth, Args&&... arg);
128
129 bool
130 on_heap() const
131 {
132 return static_cast<void const*>(p_) != static_cast<void const*>(&d_);
133 }
134};
135
136template <class T, class... Args>
137inline STVar
138make_stvar(Args&&... args)
139{
140 STVar st;
141 st.construct<T>(std::forward<Args>(args)...);
142 return st;
143}
144
145inline bool
146operator==(STVar const& lhs, STVar const& rhs)
147{
148 return lhs.get().isEquivalent(rhs.get());
149}
150
151inline bool
152operator!=(STVar const& lhs, STVar const& rhs)
153{
154 return !(lhs == rhs);
155}
156
157} // namespace detail
158} // 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:105
virtual STBase * copy(std::size_t n, void *buf) const
Definition STBase.cpp:47
virtual STBase * move(std::size_t n, void *buf)
Definition STBase.cpp:53
void constructST(SerializedTypeID id, int depth, Args &&... arg)
Construct requested Serializable Type according to id.
Definition STVar.cpp:137
STBase & get()
Definition STVar.h:68
STVar(STBase const &t)
Definition STVar.h:58
void construct(Args &&... args)
Definition STVar.h:112
STBase const & get() const
Definition STVar.h:83
STBase & operator*()
Definition STVar.h:73
std::aligned_storage< max_size >::type d_
Definition STVar.h:41
STVar(STBase &&t)
Definition STVar.h:53
STBase const & operator*() const
Definition STVar.h:88
STBase * operator->()
Definition STVar.h:78
STBase * p_
Definition STVar.h:42
bool on_heap() const
Definition STVar.h:130
STVar & operator=(STVar const &rhs)
Definition STVar.cpp:58
static std::size_t constexpr max_size
Definition STVar.h:39
STBase const * operator->() const
Definition STVar.h:93
friend STVar make_stvar(Args &&... args)
Definition STVar.h:138
T is_same_v
bool operator==(STVar const &lhs, STVar const &rhs)
Definition STVar.h:146
nonPresentObject_t nonPresentObject
Definition STVar.cpp:29
defaultObject_t defaultObject
Definition STVar.cpp:28
STVar make_stvar(Args &&... args)
Definition STVar.h:138
bool operator!=(STVar const &lhs, STVar const &rhs)
Definition STVar.h:152
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
SerializedTypeID
Definition SField.h:90