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
95namespace detail {
96class STVar;
97}
98
99// VFALCO TODO fix this restriction on copy assignment.
100//
101// CAUTION: Do not create a vector (or similar container) of any object derived
102// from STBase. Use Boost ptr_* containers. The copy assignment operator
103// of STBase has semantics that will cause contained types to change
104// their names when an object is deleted because copy assignment is used to
105// "slide down" the remaining types and this will not copy the field
106// name. Changing the copy assignment operator to copy the field name breaks the
107// use of copy assignment just to copy values, which is used in the transaction
108// engine code.
109
110//------------------------------------------------------------------------------
111
126{
127 SField const* fName;
128
129public:
130 virtual ~STBase() = default;
131 STBase();
132 STBase(STBase const&) = default;
133 STBase&
134 operator=(STBase const& t);
135
136 explicit STBase(SField const& n);
137
138 bool
139 operator==(STBase const& t) const;
140 bool
141 operator!=(STBase const& t) const;
142
143 template <class D>
144 D&
145 downcast();
146
147 template <class D>
148 D const&
149 downcast() const;
150
151 virtual SerializedTypeID
152 getSType() const;
153
154 virtual std::string
155 getFullText() const;
156
157 virtual std::string
158 getText() const;
159
160 virtual Json::Value getJson(JsonOptions /*options*/) const;
161
162 virtual void
163 add(Serializer& s) const;
164
165 virtual bool
166 isEquivalent(STBase const& t) const;
167
168 virtual bool
169 isDefault() const;
170
174 void
175 setFName(SField const& n);
176
177 SField const&
178 getFName() const;
179
180 void
181 addFieldID(Serializer& s) const;
182
183protected:
184 template <class T>
185 static STBase*
186 emplace(std::size_t n, void* buf, T&& val);
187
188private:
189 virtual STBase*
190 copy(std::size_t n, void* buf) const;
191 virtual STBase*
192 move(std::size_t n, void* buf);
193
194 friend class detail::STVar;
195};
196
197//------------------------------------------------------------------------------
198
201
202template <class D>
203D&
205{
206 D* ptr = dynamic_cast<D*>(this);
207 if (ptr == nullptr)
208 Throw<std::bad_cast>();
209 return *ptr;
210}
211
212template <class D>
213D const&
215{
216 D const* ptr = dynamic_cast<D const*>(this);
217 if (ptr == nullptr)
218 Throw<std::bad_cast>();
219 return *ptr;
220}
221
222template <class T>
223STBase*
224STBase::emplace(std::size_t n, void* buf, T&& val)
225{
226 using U = std::decay_t<T>;
227 if (sizeof(U) > n)
228 return new U(std::forward<T>(val));
229 return new (buf) U(std::forward<T>(val));
230}
231
232} // namespace ripple
233
234#endif
Represents a JSON value.
Definition: json_value.h:148
Identifies fields.
Definition: SField.h:144
A type which can be exported to a well known binary format.
Definition: STBase.h:126
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:127
bool operator!=(STBase const &t) const
Definition: STBase.cpp:57
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:224
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:204
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
virtual Json::Value getJson(JsonOptions) const
Definition: STBase.cpp:106
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:108
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition: base_uint.h:637
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