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 <ostream>
28#include <string>
29#include <type_traits>
30#include <typeinfo>
31#include <utility>
32
33namespace ripple {
34
37{
38 using underlying_t = unsigned int;
40
42 // clang-format off
43 none = 0b0000'0000,
44 include_date = 0b0000'0001,
45 disable_API_prior_V2 = 0b0000'0010,
46
47 // IMPORTANT `_all` must be union of all of the above; see also operator~
48 _all = 0b0000'0011
49 // clang-format on
50 };
51
52 constexpr JsonOptions(underlying_t v) noexcept : value(v)
53 {
54 }
55
56 [[nodiscard]] constexpr explicit
57 operator underlying_t() const noexcept
58 {
59 return value;
60 }
61 [[nodiscard]] constexpr explicit
62 operator bool() const noexcept
63 {
64 return value != 0u;
65 }
66 [[nodiscard]] constexpr auto friend
67 operator==(JsonOptions lh, JsonOptions rh) noexcept -> bool = default;
68 [[nodiscard]] constexpr auto friend
69 operator!=(JsonOptions lh, JsonOptions rh) noexcept -> bool = default;
70
72 [[nodiscard]] constexpr JsonOptions friend
74 {
75 return {lh.value | rh.value};
76 }
77
79 [[nodiscard]] constexpr JsonOptions friend
81 {
82 return {lh.value & rh.value};
83 }
84
87 [[nodiscard]] constexpr JsonOptions friend
89 {
90 return {~v.value & static_cast<underlying_t>(_all)};
91 }
92};
93
94template <typename T>
95 requires requires(T const& t) {
96 { t.getJson(JsonOptions::none) } -> std::convertible_to<Json::Value>;
97 }
99to_json(T const& t)
100{
101 return t.getJson(JsonOptions::none);
102}
103
104namespace detail {
105class STVar;
106}
107
108// VFALCO TODO fix this restriction on copy assignment.
109//
110// CAUTION: Do not create a vector (or similar container) of any object derived
111// from STBase. Use Boost ptr_* containers. The copy assignment operator
112// of STBase has semantics that will cause contained types to change
113// their names when an object is deleted because copy assignment is used to
114// "slide down" the remaining types and this will not copy the field
115// name. Changing the copy assignment operator to copy the field name breaks the
116// use of copy assignment just to copy values, which is used in the transaction
117// engine code.
118
119//------------------------------------------------------------------------------
120
135{
136 SField const* fName;
137
138public:
139 virtual ~STBase() = default;
140 STBase();
141 STBase(STBase const&) = default;
142 STBase&
143 operator=(STBase const& t);
144
145 explicit STBase(SField const& n);
146
147 bool
148 operator==(STBase const& t) const;
149 bool
150 operator!=(STBase const& t) const;
151
152 template <class D>
153 D&
154 downcast();
155
156 template <class D>
157 D const&
158 downcast() const;
159
160 virtual SerializedTypeID
161 getSType() const;
162
163 virtual std::string
164 getFullText() const;
165
166 virtual std::string
167 getText() const;
168
170
171 virtual void
172 add(Serializer& s) const;
173
174 virtual bool
175 isEquivalent(STBase const& t) const;
176
177 virtual bool
178 isDefault() const;
179
183 void
184 setFName(SField const& n);
185
186 SField const&
187 getFName() const;
188
189 void
190 addFieldID(Serializer& s) const;
191
192protected:
193 template <class T>
194 static STBase*
195 emplace(std::size_t n, void* buf, T&& val);
196
197private:
198 virtual STBase*
199 copy(std::size_t n, void* buf) const;
200 virtual STBase*
201 move(std::size_t n, void* buf);
202
203 friend class detail::STVar;
204};
205
206//------------------------------------------------------------------------------
207
210
211template <class D>
212D&
214{
215 D* ptr = dynamic_cast<D*>(this);
216 if (ptr == nullptr)
217 Throw<std::bad_cast>();
218 return *ptr;
219}
220
221template <class D>
222D const&
224{
225 D const* ptr = dynamic_cast<D const*>(this);
226 if (ptr == nullptr)
227 Throw<std::bad_cast>();
228 return *ptr;
229}
230
231template <class T>
232STBase*
233STBase::emplace(std::size_t n, void* buf, T&& val)
234{
235 using U = std::decay_t<T>;
236 if (sizeof(U) > n)
237 return new U(std::forward<T>(val));
238 return new (buf) U(std::forward<T>(val));
239}
240
241} // namespace ripple
242
243#endif
Represents a JSON value.
Definition: json_value.h:149
Identifies fields.
Definition: SField.h:143
A type which can be exported to a well known binary format.
Definition: STBase.h:135
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:136
bool operator!=(STBase const &t) const
Definition: STBase.cpp:57
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:233
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:213
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:25
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:37
constexpr JsonOptions friend operator~(JsonOptions v) noexcept
Returns JsonOptions binary negation, can be used with & (above) for set difference e....
Definition: STBase.h:88
constexpr JsonOptions friend operator|(JsonOptions lh, JsonOptions rh) noexcept
Returns JsonOptions union of lh and rh.
Definition: STBase.h:73
constexpr JsonOptions(underlying_t v) noexcept
Definition: STBase.h:52
constexpr auto friend operator==(JsonOptions lh, JsonOptions rh) noexcept -> bool=default
underlying_t value
Definition: STBase.h:39
unsigned int underlying_t
Definition: STBase.h:38
constexpr JsonOptions friend operator&(JsonOptions lh, JsonOptions rh) noexcept
Returns JsonOptions intersection of lh and rh.
Definition: STBase.h:80
constexpr auto friend operator!=(JsonOptions lh, JsonOptions rh) noexcept -> bool=default