xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
protocol
detail
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
11
namespace
xrpl::detail
{
12
13
struct
DefaultObjectT
14
{
15
explicit
DefaultObjectT
() =
default
;
16
};
17
18
struct
NonPresentObjectT
19
{
20
explicit
NonPresentObjectT
() =
default
;
21
};
22
23
extern
DefaultObjectT
gDefaultObject
;
24
extern
NonPresentObjectT
gNonPresentObject
;
25
26
// Concept to constrain STVar constructors, which
27
// instantiate ST* types from SerializedTypeID
28
template
<
typename
... Args>
29
concept
ValidConstructSTArgs
=
30
(
std::is_same_v<std::tuple<std::remove_cvref_t<Args>
...>,
std::tuple<SField>
> ||
31
std::is_same_v<std::tuple<std::remove_cvref_t<Args>
...>,
std::tuple<SerialIter, SField>
>);
32
33
// "variant" that can hold any type of serialized object
34
// and includes a small-object allocation optimization.
35
class
STVar
36
{
37
public
:
38
// The largest "small object" we can accommodate
39
static
constexpr
std::size_t
kMaxSize
= 72;
40
41
private
:
42
alignas
(
std::max_align_t
)
std::byte
d_
[
kMaxSize
] = {};
43
STBase
*
p_
=
nullptr
;
44
45
public
:
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
&
68
get
()
69
{
70
return
*
p_
;
71
}
72
STBase
&
73
operator*
()
74
{
75
return
get
();
76
}
77
STBase
*
78
operator->
()
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
102
private
:
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
141
template
<
class
T,
class
... Args>
142
inline
STVar
143
makeStvar
(Args&&... args)
144
{
145
STVar
st;
146
st.
construct
<T>(
std::forward<Args>
(args)...);
147
return
st;
148
}
149
150
inline
bool
151
operator==
(
STVar
const
& lhs,
STVar
const
& rhs)
152
{
153
return
lhs.
get
().
isEquivalent
(rhs.
get
());
154
}
155
156
}
// namespace xrpl::detail
std::byte
xrpl::SField
Identifies fields.
Definition
SField.h:132
xrpl::STBase
A type which can be exported to a well known binary format.
Definition
STBase.h:129
xrpl::STBase::isEquivalent
virtual bool isEquivalent(STBase const &t) const
Definition
STBase.cpp:100
xrpl::SerialIter
Definition
Serializer.h:478
xrpl::detail::STVar
Definition
STVar.h:36
xrpl::detail::STVar::makeStvar
friend STVar makeStvar(Args &&... args)
Definition
STVar.h:143
xrpl::detail::STVar::constructST
void constructST(SerializedTypeID id, int depth, Args &&... arg)
Construct requested Serializable Type according to id.
Definition
STVar.cpp:137
xrpl::detail::STVar::get
STBase & get()
Definition
STVar.h:68
xrpl::detail::STVar::STVar
STVar(STBase const &t)
Definition
STVar.h:59
xrpl::detail::STVar::d_
std::byte d_[kMaxSize]
Definition
STVar.h:42
xrpl::detail::STVar::onHeap
bool onHeap() const
Definition
STVar.h:135
xrpl::detail::STVar::STVar
STVar()=default
xrpl::detail::STVar::construct
void construct(Args &&... args)
Definition
STVar.h:112
xrpl::detail::STVar::get
STBase const & get() const
Definition
STVar.h:83
xrpl::detail::STVar::~STVar
~STVar()
Definition
STVar.cpp:33
xrpl::detail::STVar::STVar
STVar(STVar const &other)
Definition
STVar.cpp:38
xrpl::detail::STVar::operator*
STBase & operator*()
Definition
STVar.h:73
xrpl::detail::STVar::STVar
STVar(STBase &&t)
Definition
STVar.h:55
xrpl::detail::STVar::kMaxSize
static constexpr std::size_t kMaxSize
Definition
STVar.h:39
xrpl::detail::STVar::operator*
STBase const & operator*() const
Definition
STVar.h:88
xrpl::detail::STVar::destroy
void destroy()
Definition
STVar.cpp:120
xrpl::detail::STVar::operator->
STBase * operator->()
Definition
STVar.h:78
xrpl::detail::STVar::p_
STBase * p_
Definition
STVar.h:43
xrpl::detail::STVar::operator=
STVar & operator=(STVar const &rhs)
Definition
STVar.cpp:58
xrpl::detail::STVar::operator->
STBase const * operator->() const
Definition
STVar.h:93
xrpl::detail::ValidConstructSTArgs
Definition
STVar.h:29
cstddef
std::forward
T forward(T... args)
std::is_same_v
T is_same_v
std::max_align_t
xrpl::detail
Definition
base_uint.h:37
xrpl::detail::gDefaultObject
DefaultObjectT gDefaultObject
Definition
STVar.cpp:28
xrpl::detail::operator==
bool operator==(STVar const &lhs, STVar const &rhs)
Definition
STVar.h:151
xrpl::detail::gNonPresentObject
NonPresentObjectT gNonPresentObject
Definition
STVar.cpp:29
xrpl::detail::makeStvar
STVar makeStvar(Args &&... args)
Definition
STVar.h:143
xrpl::SerializedTypeID
SerializedTypeID
Definition
SField.h:94
std::size_t
xrpl::detail::DefaultObjectT
Definition
STVar.h:14
xrpl::detail::DefaultObjectT::DefaultObjectT
DefaultObjectT()=default
xrpl::detail::NonPresentObjectT
Definition
STVar.h:19
xrpl::detail::NonPresentObjectT::NonPresentObjectT
NonPresentObjectT()=default
tuple
type_traits
Generated by
1.17.0