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