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 // LCOV_EXCL_START
116 UNREACHABLE("ripple::STBase::add : not implemented");
117 // LCOV_EXCL_STOP
118}
119
120bool
122{
123 XRPL_ASSERT(
124 getSType() == STI_NOTPRESENT,
125 "ripple::STBase::isEquivalent : type not present");
126 return t.getSType() == STI_NOTPRESENT;
127}
128
129bool
131{
132 return true;
133}
134
135void
137{
138 fName = &n;
139 XRPL_ASSERT(fName, "ripple::STBase::setFName : field is set");
140}
141
142SField const&
144{
145 return *fName;
146}
147
148void
150{
151 XRPL_ASSERT(
152 fName->isBinary(), "ripple::STBase::addFieldID : field is binary");
154}
155
156//------------------------------------------------------------------------------
157
160{
161 return out << t.getFullText();
162}
163
164} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
Identifies fields.
Definition SField.h:146
bool hasName() const
Definition SField.h:217
std::string const fieldName
Definition SField.h:168
bool isBinary() const
Definition SField.h:246
int const fieldValue
Definition SField.h:167
bool isUseful() const
Definition SField.h:240
SerializedTypeID const fieldType
Definition SField.h:166
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:136
virtual STBase * move(std::size_t n, void *buf)
Definition STBase.cpp:69
virtual bool isEquivalent(STBase const &t) const
Definition STBase.cpp:121
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:143
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:149
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:130
int addFieldID(int type, int name)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
SerializedTypeID
Definition SField.h:110
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:647
SField const sfGeneric
Note, should be treated as flags that can be | and &.
Definition STBase.h:37