rippled
Loading...
Searching...
No Matches
STBase.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_STBASE_H_INCLUDED
21#define RIPPLE_PROTOCOL_STBASE_H_INCLUDED
22
23#include <xrpl/basics/contract.h>
24#include <xrpl/protocol/SField.h>
25#include <xrpl/protocol/Serializer.h>
26
27#include <memory>
28#include <ostream>
29#include <string>
30#include <type_traits>
31#include <typeinfo>
32#include <utility>
33
34namespace ripple {
35
38{
39 using underlying_t = unsigned int;
41
43 // clang-format off
44 none = 0b0000'0000,
45 include_date = 0b0000'0001,
46 disable_API_prior_V2 = 0b0000'0010,
47
48 // IMPORTANT `_all` must be union of all of the above; see also operator~
49 _all = 0b0000'0011
50 // clang-format on
51 };
52
53 constexpr JsonOptions(underlying_t v) noexcept : value(v)
54 {
55 }
56
57 [[nodiscard]] constexpr explicit
58 operator underlying_t() const noexcept
59 {
60 return value;
61 }
62 [[nodiscard]] constexpr explicit
63 operator bool() const noexcept
64 {
65 return value != 0u;
66 }
67 [[nodiscard]] constexpr auto friend
68 operator==(JsonOptions lh, JsonOptions rh) noexcept -> bool = default;
69 [[nodiscard]] constexpr auto friend
70 operator!=(JsonOptions lh, JsonOptions rh) noexcept -> bool = default;
71
73 [[nodiscard]] constexpr JsonOptions friend
75 {
76 return {lh.value | rh.value};
77 }
78
80 [[nodiscard]] constexpr JsonOptions friend
82 {
83 return {lh.value & rh.value};
84 }
85
88 [[nodiscard]] constexpr JsonOptions friend
90 {
91 return {~v.value & static_cast<underlying_t>(_all)};
92 }
93};
94
95template <typename T>
96 requires requires(T const& t) {
97 { t.getJson(JsonOptions::none) } -> std::convertible_to<Json::Value>;
98 }
100to_json(T const& t)
101{
102 return t.getJson(JsonOptions::none);
103}
104
105namespace detail {
106class STVar;
107}
108
109// VFALCO TODO fix this restriction on copy assignment.
110//
111// CAUTION: Do not create a vector (or similar container) of any object derived
112// from STBase. Use Boost ptr_* containers. The copy assignment operator
113// of STBase has semantics that will cause contained types to change
114// their names when an object is deleted because copy assignment is used to
115// "slide down" the remaining types and this will not copy the field
116// name. Changing the copy assignment operator to copy the field name breaks the
117// use of copy assignment just to copy values, which is used in the transaction
118// engine code.
119
120//------------------------------------------------------------------------------
121
136{
137 SField const* fName;
138
139public:
140 virtual ~STBase() = default;
141 STBase();
142 STBase(STBase const&) = default;
143 STBase&
144 operator=(STBase const& t);
145
146 explicit STBase(SField const& n);
147
148 bool
149 operator==(STBase const& t) const;
150 bool
151 operator!=(STBase const& t) const;
152
153 template <class D>
154 D&
155 downcast();
156
157 template <class D>
158 D const&
159 downcast() const;
160
161 virtual SerializedTypeID
162 getSType() const;
163
164 virtual std::string
165 getFullText() const;
166
167 virtual std::string
168 getText() const;
169
171
172 virtual void
173 add(Serializer& s) const;
174
175 virtual bool
176 isEquivalent(STBase const& t) const;
177
178 virtual bool
179 isDefault() const;
180
184 void
185 setFName(SField const& n);
186
187 SField const&
188 getFName() const;
189
190 void
191 addFieldID(Serializer& s) const;
192
193protected:
194 template <class T>
195 static STBase*
196 emplace(std::size_t n, void* buf, T&& val);
197
198private:
199 virtual STBase*
200 copy(std::size_t n, void* buf) const;
201 virtual STBase*
202 move(std::size_t n, void* buf);
203
204 friend class detail::STVar;
205};
206
207//------------------------------------------------------------------------------
208
211
212template <class D>
213D&
215{
216 D* ptr = dynamic_cast<D*>(this);
217 if (ptr == nullptr)
218 Throw<std::bad_cast>();
219 return *ptr;
220}
221
222template <class D>
223D const&
225{
226 D const* ptr = dynamic_cast<D const*>(this);
227 if (ptr == nullptr)
228 Throw<std::bad_cast>();
229 return *ptr;
230}
231
232template <class T>
233STBase*
234STBase::emplace(std::size_t n, void* buf, T&& val)
235{
236 using U = std::decay_t<T>;
237 if (sizeof(U) > n)
238 return new U(std::forward<T>(val));
239 return new (buf) U(std::forward<T>(val));
240}
241
242} // namespace ripple
243
244#endif
Represents a JSON value.
Definition: json_value.h:150
Identifies fields.
Definition: SField.h:143
A type which can be exported to a well known binary format.
Definition: STBase.h:136
void setFName(SField const &n)
A STBase is a field.
Definition: STBase.cpp:134
virtual STBase * move(std::size_t n, void *buf)
Definition: STBase.cpp:69
virtual ~STBase()=default
virtual bool isEquivalent(STBase const &t) const
Definition: STBase.cpp:119
virtual SerializedTypeID getSType() const
Definition: STBase.cpp:75
virtual std::string getText() const
Definition: STBase.cpp:100
STBase & operator=(STBase const &t)
Definition: STBase.cpp:43
SField const & getFName() const
Definition: STBase.cpp:141
bool operator==(STBase const &t) const
Definition: STBase.cpp:51
SField const * fName
Definition: STBase.h:137
bool operator!=(STBase const &t) const
Definition: STBase.cpp:57
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:234
virtual Json::Value getJson(JsonOptions=JsonOptions::none) const
Definition: STBase.cpp:106
void addFieldID(Serializer &s) const
Definition: STBase.cpp:147
D const & downcast() const
virtual std::string getFullText() const
Definition: STBase.cpp:81
D & downcast()
Definition: STBase.h:214
virtual void add(Serializer &s) const
Definition: STBase.cpp:112
virtual STBase * copy(std::size_t n, void *buf) const
Definition: STBase.cpp:63
virtual bool isDefault() const
Definition: STBase.cpp:128
STBase(STBase const &)=default
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
SerializedTypeID
Definition: SField.h:107
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition: base_uint.h:637
Json::Value to_json(Asset const &asset)
Definition: Asset.h:123
Note, should be treated as flags that can be | and &.
Definition: STBase.h:38
constexpr JsonOptions friend operator~(JsonOptions v) noexcept
Returns JsonOptions binary negation, can be used with & (above) for set difference e....
Definition: STBase.h:89
constexpr JsonOptions friend operator|(JsonOptions lh, JsonOptions rh) noexcept
Returns JsonOptions union of lh and rh.
Definition: STBase.h:74
constexpr JsonOptions(underlying_t v) noexcept
Definition: STBase.h:53
constexpr auto friend operator==(JsonOptions lh, JsonOptions rh) noexcept -> bool=default
underlying_t value
Definition: STBase.h:40
unsigned int underlying_t
Definition: STBase.h:39
constexpr JsonOptions friend operator&(JsonOptions lh, JsonOptions rh) noexcept
Returns JsonOptions intersection of lh and rh.
Definition: STBase.h:81
constexpr auto friend operator!=(JsonOptions lh, JsonOptions rh) noexcept -> bool=default