rippled
Loading...
Searching...
No Matches
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 <xrpl/beast/utility/instrumentation.h>
21#include <xrpl/json/json_value.h>
22#include <xrpl/protocol/SField.h>
23#include <xrpl/protocol/STBase.h>
24#include <xrpl/protocol/Serializer.h>
25
26#include <cstddef>
27#include <ostream>
28#include <string>
29#include <utility>
30
31namespace ripple {
32
34{
35}
36
37STBase::STBase(SField const& n) : fName(&n)
38{
39 XRPL_ASSERT(fName, "ripple::STBase::STBase : field is set");
40}
41
42STBase&
44{
45 if (!fName->isUseful())
46 fName = t.fName;
47 return *this;
48}
49
50bool
52{
53 return (getSType() == t.getSType()) && isEquivalent(t);
54}
55
56bool
58{
59 return (getSType() != t.getSType()) || !isEquivalent(t);
60}
61
62STBase*
63STBase::copy(std::size_t n, void* buf) const
64{
65 return emplace(n, buf, *this);
66}
67
68STBase*
70{
71 return emplace(n, buf, std::move(*this));
72}
73
76{
77 return STI_NOTPRESENT;
78}
79
82{
83 std::string ret;
84
85 if (getSType() != STI_NOTPRESENT)
86 {
87 if (fName->hasName())
88 {
89 ret = fName->fieldName;
90 ret += " = ";
91 }
92
93 ret += getText();
94 }
95
96 return ret;
97}
98
101{
102 return std::string();
103}
104
106STBase::getJson(JsonOptions /*options*/) const
107{
108 return getText();
109}
110
111void
113{
114 // Should never be called
115 UNREACHABLE("ripple::STBase::add : not implemented");
116}
117
118bool
120{
121 XRPL_ASSERT(
122 getSType() == STI_NOTPRESENT,
123 "ripple::STBase::isEquivalent : type not present");
124 return t.getSType() == STI_NOTPRESENT;
125}
126
127bool
129{
130 return true;
131}
132
133void
135{
136 fName = &n;
137 XRPL_ASSERT(fName, "ripple::STBase::setFName : field is set");
138}
139
140SField const&
142{
143 return *fName;
144}
145
146void
148{
149 XRPL_ASSERT(
150 fName->isBinary(), "ripple::STBase::addFieldID : field is binary");
152}
153
154//------------------------------------------------------------------------------
155
158{
159 return out << t.getFullText();
160}
161
162} // namespace ripple
Represents a JSON value.
Definition: json_value.h:149
Identifies fields.
Definition: SField.h:143
bool hasName() const
Definition: SField.h:212
std::string const fieldName
Definition: SField.h:163
bool isBinary() const
Definition: SField.h:241
int const fieldValue
Definition: SField.h:162
bool isUseful() const
Definition: SField.h:235
SerializedTypeID const fieldType
Definition: SField.h:161
A type which can be exported to a well known binary format.
Definition: STBase.h:135
void setFName(SField const &n)
A STBase is a field.
Definition: STBase.cpp:134
virtual STBase * move(std::size_t n, void *buf)
Definition: STBase.cpp:69
virtual bool isEquivalent(STBase const &t) const
Definition: STBase.cpp:119
virtual SerializedTypeID getSType() const
Definition: STBase.cpp:75
virtual std::string getText() const
Definition: STBase.cpp:100
STBase & operator=(STBase const &t)
Definition: STBase.cpp:43
SField const & getFName() const
Definition: STBase.cpp:141
bool operator==(STBase const &t) const
Definition: STBase.cpp:51
SField const * fName
Definition: STBase.h:136
bool operator!=(STBase const &t) const
Definition: STBase.cpp:57
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:233
virtual Json::Value getJson(JsonOptions=JsonOptions::none) const
Definition: STBase.cpp:106
void addFieldID(Serializer &s) const
Definition: STBase.cpp:147
virtual std::string getFullText() const
Definition: STBase.cpp:81
virtual void add(Serializer &s) const
Definition: STBase.cpp:112
virtual STBase * copy(std::size_t n, void *buf) const
Definition: STBase.cpp:63
virtual bool isDefault() const
Definition: STBase.cpp:128
int addFieldID(int type, int name)
Definition: Serializer.cpp:120
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
SerializedTypeID
Definition: SField.h:107
SField const sfGeneric
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition: base_uint.h:637
Note, should be treated as flags that can be | and &.
Definition: STBase.h:37