xrpld
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::detail {
11
13{
14 explicit DefaultObjectT() = default;
15};
16
18{
19 explicit NonPresentObjectT() = default;
20};
21
24
25// Concept to constrain STVar constructors, which
26// instantiate ST* types from SerializedTypeID
27template <typename... Args>
31
32// "variant" that can hold any type of serialized object
33// and includes a small-object allocation optimization.
34class STVar
35{
36private:
37 // The largest "small object" we can accommodate
38 static constexpr std::size_t kMaxSize = 72;
39
41 STBase* p_ = nullptr;
42
43public:
44 ~STVar();
45 STVar(STVar const& other);
46 STVar(STVar&& other);
47 STVar&
48 operator=(STVar const& rhs);
49 STVar&
50 operator=(STVar&& rhs);
51
52 STVar(STBase&& t) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
53 {
54 p_ = t.move(kMaxSize, &d_);
55 }
56
57 STVar(STBase const& t)
58 {
59 p_ = t.copy(kMaxSize, &d_);
60 }
61
62 STVar(DefaultObjectT, SField const& name);
63 STVar(NonPresentObjectT, SField const& name);
64 STVar(SerialIter& sit, SField const& name, int depth = 0);
65
66 STBase&
68 {
69 return *p_;
70 }
71 STBase&
73 {
74 return get();
75 }
76 STBase*
78 {
79 return &get();
80 }
81 [[nodiscard]] STBase const&
82 get() const
83 {
84 return *p_;
85 }
86 STBase const&
87 operator*() const
88 {
89 return get();
90 }
91 STBase const*
92 operator->() const
93 {
94 return &get();
95 }
96
97 template <class T, class... Args>
98 friend STVar
99 makeStvar(Args&&... args);
100
101private:
102 STVar() = default;
103
104 STVar(SerializedTypeID id, SField const& name);
105
106 void
107 destroy();
108
109 template <class T, class... Args>
110 void
111 construct(Args&&... args)
112 {
113 if constexpr (sizeof(T) > kMaxSize)
114 {
115 p_ = new T(std::forward<Args>(args)...);
116 }
117 else
118 {
119 p_ = new (&d_) T(std::forward<Args>(args)...);
120 }
121 }
122
127 template <typename... Args>
128 requires ValidConstructSTArgs<Args...>
129 void
130 constructST(SerializedTypeID id, int depth, Args&&... arg);
131
132 [[nodiscard]] bool
133 onHeap() const
134 {
135 return static_cast<void const*>(p_) != static_cast<void const*>(&d_);
136 }
137};
138
139template <class T, class... Args>
140inline STVar
141makeStvar(Args&&... args)
142{
143 STVar st;
144 st.construct<T>(std::forward<Args>(args)...);
145 return st;
146}
147
148inline bool
149operator==(STVar const& lhs, STVar const& rhs)
150{
151 return lhs.get().isEquivalent(rhs.get());
152}
153
154inline bool
155operator!=(STVar const& lhs, STVar const& rhs)
156{
157 return !(lhs == rhs);
158}
159
160} // namespace xrpl::detail
Identifies fields.
Definition SField.h:130
A type which can be exported to a well known binary format.
Definition STBase.h:117
virtual bool isEquivalent(STBase const &t) const
Definition STBase.cpp:106
virtual STBase * copy(std::size_t n, void *buf) const
Definition STBase.cpp:48
friend STVar makeStvar(Args &&... args)
Definition STVar.h:141
STBase & get()
Definition STVar.h:67
void constructST(SerializedTypeID id, int depth, Args &&... arg)
Construct requested Serializable Type according to id.
Definition STVar.cpp:137
STVar(STBase const &t)
Definition STVar.h:57
std::byte d_[kMaxSize]
Definition STVar.h:40
bool onHeap() const
Definition STVar.h:133
void construct(Args &&... args)
Definition STVar.h:111
STBase const & get() const
Definition STVar.h:82
STVar(STVar const &other)
Definition STVar.cpp:38
STBase & operator*()
Definition STVar.h:72
STVar(STBase &&t)
Definition STVar.h:52
static constexpr std::size_t kMaxSize
Definition STVar.h:38
STBase const & operator*() const
Definition STVar.h:87
STBase * operator->()
Definition STVar.h:77
STBase * p_
Definition STVar.h:41
STVar & operator=(STVar const &rhs)
Definition STVar.cpp:58
STBase const * operator->() const
Definition STVar.h:92
T forward(T... args)
T is_same_v
DefaultObjectT gDefaultObject
Definition STVar.cpp:28
bool operator==(STVar const &lhs, STVar const &rhs)
Definition STVar.h:149
bool operator!=(STVar const &lhs, STVar const &rhs)
Definition STVar.h:155
NonPresentObjectT gNonPresentObject
Definition STVar.cpp:29
STVar makeStvar(Args &&... args)
Definition STVar.h:141
SerializedTypeID
Definition SField.h:93