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 <tuple>
9#include <type_traits>
10
11namespace xrpl::detail {
12
14{
15 explicit DefaultObjectT() = default;
16};
17
19{
20 explicit NonPresentObjectT() = 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 constexpr std::size_t kMaxSize = 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 // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
54 STVar(STBase&& t) : p_(t.move(kMaxSize, &d_))
55 {
56 }
57
58 STVar(STBase const& t) : p_(t.copy(kMaxSize, &d_))
59 {
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
128 template <typename... Args>
129 requires ValidConstructSTArgs<Args...>
130 void
131 constructST(SerializedTypeID id, int depth, Args&&... arg);
132
133 [[nodiscard]] bool
134 onHeap() const
135 {
136 return static_cast<void const*>(p_) != static_cast<void const*>(&d_);
137 }
138};
139
140template <class T, class... Args>
141inline STVar
142makeStvar(Args&&... args)
143{
144 STVar st;
145 st.construct<T>(std::forward<Args>(args)...);
146 return st;
147}
148
149inline bool
150operator==(STVar const& lhs, STVar const& rhs)
151{
152 return lhs.get().isEquivalent(rhs.get());
153}
154
155} // namespace xrpl::detail
Identifies fields.
Definition SField.h:132
A type which can be exported to a well known binary format.
Definition STBase.h:129
virtual bool isEquivalent(STBase const &t) const
Definition STBase.cpp:100
friend STVar makeStvar(Args &&... args)
Definition STVar.h:142
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:58
std::byte d_[kMaxSize]
Definition STVar.h:41
bool onHeap() const
Definition STVar.h:134
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:54
static constexpr std::size_t kMaxSize
Definition STVar.h:39
STBase const & operator*() const
Definition STVar.h:87
STBase * operator->()
Definition STVar.h:77
STBase * p_
Definition STVar.h:42
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:150
NonPresentObjectT gNonPresentObject
Definition STVar.cpp:29
STVar makeStvar(Args &&... args)
Definition STVar.h:142
SerializedTypeID
Definition SField.h:94