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