xrpld
Loading...
Searching...
No Matches
STBase.h
1#pragma once
2
3#include <xrpl/basics/contract.h>
4#include <xrpl/protocol/SField.h>
5#include <xrpl/protocol/Serializer.h>
6
7#include <ostream>
8#include <string>
9#include <type_traits>
10#include <typeinfo>
11#include <utility>
12
13namespace xrpl {
14
17{
18 using underlying_t = unsigned int;
20
21 enum class Values : underlying_t {
22 None = 0b0000'0000,
23 IncludeDate = 0b0000'0001,
24 DisableApiPriorV2 = 0b0000'0010,
25
26 // IMPORTANT `All` must be union of all of the above; see also operator~
28 };
29
30 constexpr JsonOptions(underlying_t v) noexcept : value(v)
31 {
32 }
33
34 constexpr JsonOptions(Values v) noexcept : value(static_cast<JsonOptions::underlying_t>(v))
35 {
36 }
37
38 [[nodiscard]] constexpr explicit
39 operator underlying_t() const noexcept
40 {
41 return value;
42 }
43 [[nodiscard]] constexpr explicit
44 operator bool() const noexcept
45 {
46 return value != 0u;
47 }
48 [[nodiscard]] constexpr auto friend
49 operator==(JsonOptions lh, JsonOptions rh) noexcept -> bool = default;
50 [[nodiscard]] constexpr auto friend
51 operator!=(JsonOptions lh, JsonOptions rh) noexcept -> bool = default;
52
54 [[nodiscard]] constexpr JsonOptions friend
56 {
57 return {lh.value | rh.value};
58 }
59
61 [[nodiscard]] constexpr JsonOptions friend
63 {
64 return {lh.value & rh.value};
65 }
66
69 [[nodiscard]] constexpr JsonOptions friend
71 {
72 return {~v.value & static_cast<underlying_t>(Values::All)};
73 }
74};
75
76template <typename T>
77 requires requires(T const& t) {
78 { t.getJson(JsonOptions::Values::None) } -> std::convertible_to<json::Value>;
79 }
80json::Value
81toJson(T const& t)
82{
83 return t.getJson(JsonOptions::Values::None);
84}
85
86namespace detail {
87class STVar;
88} // namespace detail
89
90// VFALCO TODO fix this restriction on copy assignment.
91//
92// CAUTION: Do not create a vector (or similar container) of any object derived
93// from STBase. Use Boost ptr_* containers. The copy assignment operator
94// of STBase has semantics that will cause contained types to change
95// their names when an object is deleted because copy assignment is used to
96// "slide down" the remaining types and this will not copy the field
97// name. Changing the copy assignment operator to copy the field name breaks the
98// use of copy assignment just to copy values, which is used in the transaction
99// engine code.
100
101//------------------------------------------------------------------------------
102
117{
119
120public:
121 virtual ~STBase() = default;
122 STBase();
123 STBase(STBase const&) = default;
124 STBase&
125 operator=(STBase const& t);
126
127 explicit STBase(SField const& n);
128
129 bool
130 operator==(STBase const& t) const;
131 bool
132 operator!=(STBase const& t) const;
133
134 template <class D>
135 D&
137
138 template <class D>
139 D const&
140 downcast() const;
141
142 [[nodiscard]] virtual SerializedTypeID
143 getSType() const;
144
145 [[nodiscard]] virtual std::string
146 getFullText() const;
147
148 [[nodiscard]] virtual std::string
149 getText() const;
150
151 [[nodiscard]] virtual json::Value getJson(JsonOptions = JsonOptions::Values::None) const;
152
153 virtual void
154 add(Serializer& s) const;
155
156 [[nodiscard]] virtual bool
157 isEquivalent(STBase const& t) const;
158
159 [[nodiscard]] virtual bool
160 isDefault() const;
161
165 void
166 setFName(SField const& n);
167
168 [[nodiscard]] SField const&
169 getFName() const;
170
171 void
172 addFieldID(Serializer& s) const;
173
174protected:
175 template <class T>
176 static STBase*
177 emplace(std::size_t n, void* buf, T&& val);
178
179private:
180 virtual STBase*
181 copy(std::size_t n, void* buf) const;
182 virtual STBase*
183 move(std::size_t n, void* buf);
184
185 friend class detail::STVar;
186};
187
188//------------------------------------------------------------------------------
189
191operator<<(std::ostream& out, STBase const& t);
192
193template <class D>
194D&
196{
197 D* ptr = dynamic_cast<D*>(this);
198 if (ptr == nullptr)
200 return *ptr;
201}
202
203template <class D>
204[[nodiscard]] D const&
206{
207 D const* ptr = dynamic_cast<D const*>(this);
208 if (ptr == nullptr)
210 return *ptr;
211}
212
213template <class T>
214STBase*
215STBase::emplace(std::size_t n, void* buf, T&& val)
216{
217 using U = std::decay_t<T>;
218 if (sizeof(U) > n)
219 return new U(std::forward<T>(val));
220 return new (buf) U(std::forward<T>(val));
221}
222
223} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Identifies fields.
Definition SField.h:130
A type which can be exported to a well known binary format.
Definition STBase.h:117
SField const & getFName() const
Definition STBase.cpp:126
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:215
D & downcast()
Definition STBase.h:195
virtual bool isEquivalent(STBase const &t) const
Definition STBase.cpp:106
virtual std::string getFullText() const
Definition STBase.cpp:66
STBase & operator=(STBase const &t)
Definition STBase.cpp:25
bool operator!=(STBase const &t) const
Definition STBase.cpp:42
D const & downcast() const
virtual STBase * copy(std::size_t n, void *buf) const
Definition STBase.cpp:48
virtual SerializedTypeID getSType() const
Definition STBase.cpp:60
SField const * fName_
Definition STBase.h:118
void setFName(SField const &n)
A STBase is a field.
Definition STBase.cpp:119
virtual bool isDefault() const
Definition STBase.cpp:113
bool operator==(STBase const &t) const
Definition STBase.cpp:36
virtual json::Value getJson(JsonOptions=JsonOptions::Values::None) const
Definition STBase.cpp:91
virtual STBase * move(std::size_t n, void *buf)
Definition STBase.cpp:54
virtual std::string getText() const
Definition STBase.cpp:85
STBase(STBase const &)=default
void addFieldID(Serializer &s) const
Definition STBase.cpp:132
virtual ~STBase()=default
virtual void add(Serializer &s) const
Definition STBase.cpp:97
T forward(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
Definition base_uint.h:648
json::Value toJson(Asset const &asset)
Definition Asset.h:157
SerializedTypeID
Definition SField.h:93
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
Note, should be treated as flags that can be | and &.
Definition STBase.h:17
constexpr JsonOptions friend operator|(JsonOptions lh, JsonOptions rh) noexcept
Returns JsonOptions union of lh and rh.
Definition STBase.h:55
constexpr auto friend operator!=(JsonOptions lh, JsonOptions rh) noexcept -> bool=default
constexpr JsonOptions friend operator&(JsonOptions lh, JsonOptions rh) noexcept
Returns JsonOptions intersection of lh and rh.
Definition STBase.h:62
underlying_t value
Definition STBase.h:19
constexpr JsonOptions(underlying_t v) noexcept
Definition STBase.h:30
constexpr JsonOptions friend operator~(JsonOptions v) noexcept
Returns JsonOptions binary negation, can be used with & (above) for set difference e....
Definition STBase.h:70
constexpr JsonOptions(Values v) noexcept
Definition STBase.h:34
constexpr auto friend operator==(JsonOptions lh, JsonOptions rh) noexcept -> bool=default
unsigned int underlying_t
Definition STBase.h:18