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