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