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  : fName(&sfGeneric)
29 {
30 }
31 
33  : fName(&n)
34 {
35  assert(fName);
36 }
37 
38 STBase&
40 {
41  if ((t.fName != fName) && fName->isUseful() && t.fName->isUseful())
42  {
43  // VFALCO We shouldn't be logging at this low a level
44  /*
45  WriteLog ((t.getSType () == STI_AMOUNT) ? lsTRACE : lsWARNING, STBase) // This is common for amounts
46  << "Caution: " << t.fName->getName () << " not replacing " << fName->getName ();
47  */
48  }
49  if (!fName->isUseful())
50  fName = t.fName;
51  return *this;
52 }
53 
54 bool
55 STBase::operator== (const STBase& t) const
56 {
57  return (getSType () == t.getSType ()) && isEquivalent (t);
58 }
59 
60 bool
61 STBase::operator!= (const STBase& t) const
62 {
63  return (getSType () != t.getSType ()) || !isEquivalent (t);
64 }
65 
68 {
69  return STI_NOTPRESENT;
70 }
71 
74 {
75  std::string ret;
76 
77  if (getSType () != STI_NOTPRESENT)
78  {
79  if (fName->hasName ())
80  {
81  ret = fName->fieldName;
82  ret += " = ";
83  }
84 
85  ret += getText ();
86  }
87 
88  return ret;
89 }
90 
93 {
94  return std::string();
95 }
96 
98 STBase::getJson (JsonOptions /*options*/) const
99 {
100  return getText();
101 }
102 
103 void
105 {
106  // Should never be called
107  assert(false);
108 }
109 
110 bool
111 STBase::isEquivalent (const STBase& t) const
112 {
113  assert (getSType () == STI_NOTPRESENT);
114  if (t.getSType () == STI_NOTPRESENT)
115  return true;
116  // VFALCO We shouldn't be logging at this low a level
117  //WriteLog (lsDEBUG, STBase) << "notEquiv " << getFullText() << " not STI_NOTPRESENT";
118  return false;
119 }
120 
121 bool
123 {
124  return true;
125 }
126 
127 void
129 {
130  fName = &n;
131  assert (fName);
132 }
133 
134 SField const&
136 {
137  return *fName;
138 }
139 
140 void
142 {
143  assert (fName->isBinary ());
145 }
146 
147 //------------------------------------------------------------------------------
148 
151 {
152  return out << t.getFullText ();
153 }
154 
155 } // ripple
ripple::STBase::operator=
STBase & operator=(const STBase &t)
Definition: STBase.cpp:39
ripple::STBase::STBase
STBase()
Definition: STBase.cpp:27
ripple::STBase::getSType
virtual SerializedTypeID getSType() const
Definition: STBase.cpp:67
std::string
STL class.
ripple::STBase::isEquivalent
virtual bool isEquivalent(STBase const &t) const
Definition: STBase.cpp:111
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::sfGeneric
const SField sfGeneric(access, 0)
Definition: SField.h:317
ripple::SField::fieldValue
const int fieldValue
Definition: SField.h:135
ripple::Serializer::addFieldID
int addFieldID(int type, int name)
Definition: Serializer.cpp:129
ripple::SField::isBinary
bool isBinary() const
Definition: SField.h:199
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::SField::fieldName
const std::string fieldName
Definition: SField.h:136
ripple::SField::hasName
bool hasName() const
Definition: SField.h:173
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::SField::isUseful
bool isUseful() const
Definition: SField.h:191
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:237
ripple::SField::fieldType
const SerializedTypeID fieldType
Definition: SField.h:134
ripple::STBase::isDefault
virtual bool isDefault() const
Definition: STBase.cpp:122
ripple::QualityDirection::out
@ out
std::ostream
STL class.
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
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::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
cassert
ripple::STBase::fName
SField const * fName
Definition: STBase.h:156
ripple::STBase::operator==
bool operator==(const STBase &t) const
Definition: STBase.cpp:55
ripple::STBase::getJson
virtual Json::Value getJson(JsonOptions) const
Definition: STBase.cpp:98
Json::Value
Represents a JSON value.
Definition: json_value.h:141
ripple::STI_NOTPRESENT
@ STI_NOTPRESENT
Definition: SField.h:57