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