rippled
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
22 // clang-format off
23 none = 0b0000'0000,
24 include_date = 0b0000'0001,
25 disable_API_prior_V2 = 0b0000'0010,
26
27 // IMPORTANT `_all` must be union of all of the above; see also operator~
28 _all = 0b0000'0011
29 // clang-format on
30 };
31
32 constexpr JsonOptions(underlying_t v) noexcept : value(v)
33 {
34 }
35
36 [[nodiscard]] constexpr explicit
37 operator underlying_t() const noexcept
38 {
39 return value;
40 }
41 [[nodiscard]] constexpr explicit
42 operator bool() const noexcept
43 {
44 return value != 0u;
45 }
46 [[nodiscard]] constexpr auto friend
47 operator==(JsonOptions lh, JsonOptions rh) noexcept -> bool = default;
48 [[nodiscard]] constexpr auto friend
49 operator!=(JsonOptions lh, JsonOptions rh) noexcept -> bool = default;
50
52 [[nodiscard]] constexpr JsonOptions friend
54 {
55 return {lh.value | rh.value};
56 }
57
59 [[nodiscard]] constexpr JsonOptions friend
61 {
62 return {lh.value & rh.value};
63 }
64
67 [[nodiscard]] constexpr JsonOptions friend
69 {
70 return {~v.value & static_cast<underlying_t>(_all)};
71 }
72};
73
74template <typename T>
75 requires requires(T const& t) {
77 }
79to_json(T const& t)
80{
81 return t.getJson(JsonOptions::none);
82}
83
84namespace detail {
85class STVar;
86}
87
88// VFALCO TODO fix this restriction on copy assignment.
89//
90// CAUTION: Do not create a vector (or similar container) of any object derived
91// from STBase. Use Boost ptr_* containers. The copy assignment operator
92// of STBase has semantics that will cause contained types to change
93// their names when an object is deleted because copy assignment is used to
94// "slide down" the remaining types and this will not copy the field
95// name. Changing the copy assignment operator to copy the field name breaks the
96// use of copy assignment just to copy values, which is used in the transaction
97// engine code.
98
99//------------------------------------------------------------------------------
100
115{
116 SField const* fName;
117
118public:
119 virtual ~STBase() = default;
120 STBase();
121 STBase(STBase const&) = default;
122 STBase&
123 operator=(STBase const& t);
124
125 explicit STBase(SField const& n);
126
127 bool
128 operator==(STBase const& t) const;
129 bool
130 operator!=(STBase const& t) const;
131
132 template <class D>
133 D&
134 downcast();
135
136 template <class D>
137 D const&
138 downcast() const;
139
140 virtual SerializedTypeID
141 getSType() const;
142
143 virtual std::string
144 getFullText() const;
145
146 virtual std::string
147 getText() const;
148
150
151 virtual void
152 add(Serializer& s) const;
153
154 virtual bool
155 isEquivalent(STBase const& t) const;
156
157 virtual bool
158 isDefault() const;
159
163 void
164 setFName(SField const& n);
165
166 SField const&
167 getFName() const;
168
169 void
170 addFieldID(Serializer& s) const;
171
172protected:
173 template <class T>
174 static STBase*
175 emplace(std::size_t n, void* buf, T&& val);
176
177private:
178 virtual STBase*
179 copy(std::size_t n, void* buf) const;
180 virtual STBase*
181 move(std::size_t n, void* buf);
182
183 friend class detail::STVar;
184};
185
186//------------------------------------------------------------------------------
187
190
191template <class D>
192D&
194{
195 D* ptr = dynamic_cast<D*>(this);
196 if (ptr == nullptr)
197 Throw<std::bad_cast>();
198 return *ptr;
199}
200
201template <class D>
202D const&
204{
205 D const* ptr = dynamic_cast<D const*>(this);
206 if (ptr == nullptr)
207 Throw<std::bad_cast>();
208 return *ptr;
209}
210
211template <class T>
212STBase*
213STBase::emplace(std::size_t n, void* buf, T&& val)
214{
215 using U = std::decay_t<T>;
216 if (sizeof(U) > n)
217 return new U(std::forward<T>(val));
218 return new (buf) U(std::forward<T>(val));
219}
220
221} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Identifies fields.
Definition SField.h:126
A type which can be exported to a well known binary format.
Definition STBase.h:115
SField const & getFName() const
Definition STBase.cpp:122
virtual Json::Value getJson(JsonOptions=JsonOptions::none) const
Definition STBase.cpp:87
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:213
D & downcast()
Definition STBase.h:193
virtual bool isEquivalent(STBase const &t) const
Definition STBase.cpp:102
virtual std::string getFullText() const
Definition STBase.cpp:62
STBase & operator=(STBase const &t)
Definition STBase.cpp:24
bool operator!=(STBase const &t) const
Definition STBase.cpp:38
D const & downcast() const
virtual STBase * copy(std::size_t n, void *buf) const
Definition STBase.cpp:44
virtual SerializedTypeID getSType() const
Definition STBase.cpp:56
void setFName(SField const &n)
A STBase is a field.
Definition STBase.cpp:115
virtual bool isDefault() const
Definition STBase.cpp:109
SField const * fName
Definition STBase.h:116
bool operator==(STBase const &t) const
Definition STBase.cpp:32
virtual STBase * move(std::size_t n, void *buf)
Definition STBase.cpp:50
virtual std::string getText() const
Definition STBase.cpp:81
STBase(STBase const &)=default
void addFieldID(Serializer &s) const
Definition STBase.cpp:128
virtual ~STBase()=default
virtual void add(Serializer &s) const
Definition STBase.cpp:93
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:612
SerializedTypeID
Definition SField.h:90
Json::Value to_json(Asset const &asset)
Definition Asset.h:121
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:53
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:60
underlying_t value
Definition STBase.h:19
constexpr JsonOptions(underlying_t v) noexcept
Definition STBase.h:32
constexpr JsonOptions friend operator~(JsonOptions v) noexcept
Returns JsonOptions binary negation, can be used with & (above) for set difference e....
Definition STBase.h:68
constexpr auto friend operator==(JsonOptions lh, JsonOptions rh) noexcept -> bool=default
unsigned int underlying_t
Definition STBase.h:18