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