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 
34 enum class JsonOptions { none = 0, include_date = 1 };
35 
36 namespace detail {
37 class STVar;
38 }
39 
40 // VFALCO TODO fix this restriction on copy assignment.
41 //
42 // CAUTION: Do not create a vector (or similar container) of any object derived
43 // from STBase. Use Boost ptr_* containers. The copy assignment operator
44 // of STBase has semantics that will cause contained types to change
45 // their names when an object is deleted because copy assignment is used to
46 // "slide down" the remaining types and this will not copy the field
47 // name. Changing the copy assignment operator to copy the field name breaks the
48 // use of copy assignment just to copy values, which is used in the transaction
49 // engine code.
50 
51 //------------------------------------------------------------------------------
52 
66 class STBase
67 {
68  SField const* fName;
69 
70 public:
71  virtual ~STBase() = default;
72  STBase();
73  STBase(const STBase&) = default;
74  STBase&
75  operator=(const STBase& t);
76 
77  explicit STBase(SField const& n);
78 
79  bool
80  operator==(const STBase& t) const;
81  bool
82  operator!=(const STBase& t) const;
83 
84  template <class D>
85  D&
86  downcast();
87 
88  template <class D>
89  D const&
90  downcast() const;
91 
92  virtual SerializedTypeID
93  getSType() const;
94 
95  virtual std::string
96  getFullText() const;
97 
98  virtual std::string
99  getText() const;
100 
101  virtual Json::Value getJson(JsonOptions /*options*/) const;
102 
103  virtual void
104  add(Serializer& s) const;
105 
106  virtual bool
107  isEquivalent(STBase const& t) const;
108 
109  virtual bool
110  isDefault() const;
111 
115  void
116  setFName(SField const& n);
117 
118  SField const&
119  getFName() const;
120 
121  void
122  addFieldID(Serializer& s) const;
123 
124 protected:
125  template <class T>
126  static STBase*
127  emplace(std::size_t n, void* buf, T&& val);
128 
129 private:
130  virtual STBase*
131  copy(std::size_t n, void* buf) const;
132  virtual STBase*
133  move(std::size_t n, void* buf);
134 
135  friend class detail::STVar;
136 };
137 
138 //------------------------------------------------------------------------------
139 
141 operator<<(std::ostream& out, const STBase& t);
142 
143 template <class D>
144 D&
146 {
147  D* ptr = dynamic_cast<D*>(this);
148  if (ptr == nullptr)
149  Throw<std::bad_cast>();
150  return *ptr;
151 }
152 
153 template <class D>
154 D const&
156 {
157  D const* ptr = dynamic_cast<D const*>(this);
158  if (ptr == nullptr)
159  Throw<std::bad_cast>();
160  return *ptr;
161 }
162 
163 template <class T>
164 STBase*
165 STBase::emplace(std::size_t n, void* buf, T&& val)
166 {
167  using U = std::decay_t<T>;
168  if (sizeof(U) > n)
169  return new U(std::forward<T>(val));
170  return new (buf) U(std::forward<T>(val));
171 }
172 
173 } // namespace ripple
174 
175 #endif
ripple::STBase::operator=
STBase & operator=(const STBase &t)
Definition: STBase.cpp:37
ripple::STBase::STBase
STBase()
Definition: STBase.cpp:27
ripple::JsonOptions::include_date
@ include_date
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::JsonOptions
JsonOptions
Definition: STBase.h:34
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:52
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:242
ripple::STBase::isDefault
virtual bool isDefault() const
Definition: STBase.cpp:119
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:165
std::ostream
STL class.
ripple::JsonOptions::none
@ none
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:39
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::STBase::~STBase
virtual ~STBase()=default
ripple::SField
Identifies fields.
Definition: SField.h:112
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:66
ripple::STBase::operator!=
bool operator!=(const STBase &t) const
Definition: STBase.cpp:51
typeinfo
ripple::STBase::fName
SField const * fName
Definition: STBase.h:68
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:145
ostream
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