rippled
STBase.cpp
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 #include <ripple/protocol/STBase.h>
21 #include <boost/checked_delete.hpp>
22 #include <cassert>
23 #include <memory>
24 
25 namespace ripple {
26 
28 {
29 }
30 
31 STBase::STBase(SField const& n) : fName(&n)
32 {
33  assert(fName);
34 }
35 
36 STBase&
38 {
39  if (!fName->isUseful())
40  fName = t.fName;
41  return *this;
42 }
43 
44 bool
45 STBase::operator==(const STBase& t) const
46 {
47  return (getSType() == t.getSType()) && isEquivalent(t);
48 }
49 
50 bool
51 STBase::operator!=(const STBase& t) const
52 {
53  return (getSType() != t.getSType()) || !isEquivalent(t);
54 }
55 
56 STBase*
57 STBase::copy(std::size_t n, void* buf) const
58 {
59  return emplace(n, buf, *this);
60 }
61 
62 STBase*
64 {
65  return emplace(n, buf, std::move(*this));
66 }
67 
70 {
71  return STI_NOTPRESENT;
72 }
73 
76 {
77  std::string ret;
78 
79  if (getSType() != STI_NOTPRESENT)
80  {
81  if (fName->hasName())
82  {
83  ret = fName->fieldName;
84  ret += " = ";
85  }
86 
87  ret += getText();
88  }
89 
90  return ret;
91 }
92 
95 {
96  return std::string();
97 }
98 
100 {
101  return getText();
102 }
103 
104 void
106 {
107  // Should never be called
108  assert(false);
109 }
110 
111 bool
113 {
114  assert(getSType() == STI_NOTPRESENT);
115  return t.getSType() == STI_NOTPRESENT;
116 }
117 
118 bool
120 {
121  return true;
122 }
123 
124 void
126 {
127  fName = &n;
128  assert(fName);
129 }
130 
131 SField const&
133 {
134  return *fName;
135 }
136 
137 void
139 {
140  assert(fName->isBinary());
142 }
143 
144 //------------------------------------------------------------------------------
145 
148 {
149  return out << t.getFullText();
150 }
151 
152 } // namespace ripple
ripple::STBase::operator=
STBase & operator=(const STBase &t)
Definition: STBase.cpp:37
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
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::sfGeneric
const SField sfGeneric(access, 0)
Definition: SField.h:327
ripple::SField::fieldValue
const int fieldValue
Definition: SField.h:131
ripple::Serializer::addFieldID
int addFieldID(int type, int name)
Definition: Serializer.cpp:132
ripple::SField::isBinary
bool isBinary() const
Definition: SField.h:205
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::SField::fieldName
const std::string fieldName
Definition: SField.h:132
ripple::SField::hasName
bool hasName() const
Definition: SField.h:181
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::SField::isUseful
bool isUseful() const
Definition: SField.h:199
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:242
ripple::SField::fieldType
const SerializedTypeID fieldType
Definition: SField.h:130
ripple::STBase::isDefault
virtual bool isDefault() const
Definition: STBase.cpp:119
ripple::QualityDirection::out
@ out
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:165
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
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::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
cassert
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::getJson
virtual Json::Value getJson(JsonOptions) const
Definition: STBase.cpp:99
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::STI_NOTPRESENT
@ STI_NOTPRESENT
Definition: SField.h:55