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{
37public:
38 // The largest "small object" we can accommodate
39 static constexpr std::size_t kMaxSize = 72;
40
41private:
43 STBase* p_ = nullptr;
44
45public:
46 ~STVar();
47 STVar(STVar const& other);
48 STVar(STVar&& other);
49 STVar&
50 operator=(STVar const& rhs);
51 STVar&
52 operator=(STVar&& rhs);
53
54 // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
55 STVar(STBase&& t) : p_(t.move(kMaxSize, &d_))
56 {
57 }
58
59 STVar(STBase const& t) : p_(t.copy(kMaxSize, &d_))
60 {
61 }
62
63 STVar(DefaultObjectT, SField const& name);
64 STVar(NonPresentObjectT, 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 [[nodiscard]] 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 makeStvar(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) > kMaxSize)
115 {
116 p_ = new T(std::forward<Args>(args)...);
117 }
118 else
119 {
120 p_ = new (&d_) T(std::forward<Args>(args)...);
121 }
122 }
123
129 template <typename... Args>
130 requires ValidConstructSTArgs<Args...>
131 void
132 constructST(SerializedTypeID id, int depth, Args&&... arg);
133
134 [[nodiscard]] bool
135 onHeap() const
136 {
137 return static_cast<void const*>(p_) != static_cast<void const*>(&d_);
138 }
139};
140
141template <class T, class... Args>
142inline STVar
143makeStvar(Args&&... args)
144{
145 STVar st;
146 st.construct<T>(std::forward<Args>(args)...);
147 return st;
148}
149
150inline bool
151operator==(STVar const& lhs, STVar const& rhs)
152{
153 return lhs.get().isEquivalent(rhs.get());
154}
155
156} // 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:143
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:59
std::byte d_[kMaxSize]
Definition STVar.h:42
bool onHeap() const
Definition STVar.h:135
void construct(Args &&... args)
Definition STVar.h:112
STBase const & get() const
Definition STVar.h:83
STVar(STVar const &other)
Definition STVar.cpp:38
STBase & operator*()
Definition STVar.h:73
STVar(STBase &&t)
Definition STVar.h:55
static constexpr std::size_t kMaxSize
Definition STVar.h:39
STBase const & operator*() const
Definition STVar.h:88
STBase * operator->()
Definition STVar.h:78
STBase * p_
Definition STVar.h:43
STVar & operator=(STVar const &rhs)
Definition STVar.cpp:58
STBase const * operator->() const
Definition STVar.h:93
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:151
NonPresentObjectT gNonPresentObject
Definition STVar.cpp:29
STVar makeStvar(Args &&... args)
Definition STVar.h:143
SerializedTypeID
Definition SField.h:94