rippled
Loading...
Searching...
No Matches
STArray.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/basics/Log.h>
21#include <xrpl/basics/contract.h>
22#include <xrpl/json/json_value.h>
23#include <xrpl/protocol/SField.h>
24#include <xrpl/protocol/STArray.h>
25#include <xrpl/protocol/STBase.h>
26#include <xrpl/protocol/Serializer.h>
27
28#include <algorithm>
29#include <cstddef>
30#include <stdexcept>
31#include <string>
32#include <utility>
33
34namespace ripple {
35
37 : STBase(other.getFName()), v_(std::move(other.v_))
38{
39}
40
43{
44 setFName(other.getFName());
45 v_ = std::move(other.v_);
46 return *this;
47}
48
50{
51 v_.reserve(n);
52}
53
55{
56}
57
59{
60 v_.reserve(n);
61}
62
63STArray::STArray(SerialIter& sit, SField const& f, int depth) : STBase(f)
64{
65 while (!sit.empty())
66 {
67 int type, field;
68 sit.getFieldID(type, field);
69
70 if ((type == STI_ARRAY) && (field == 1))
71 break;
72
73 if ((type == STI_OBJECT) && (field == 1))
74 {
75 JLOG(debugLog().error())
76 << "Encountered array with end of object marker";
77 Throw<std::runtime_error>("Illegal terminator in array");
78 }
79
80 auto const& fn = SField::getField(type, field);
81
82 if (fn.isInvalid())
83 {
84 JLOG(debugLog().error())
85 << "Unknown field: " << type << "/" << field;
86 Throw<std::runtime_error>("Unknown field");
87 }
88
89 if (fn.fieldType != STI_OBJECT)
90 {
91 JLOG(debugLog().error()) << "Array contains non-object";
92 Throw<std::runtime_error>("Non-object in array");
93 }
94
95 v_.emplace_back(sit, fn, depth + 1);
96
97 v_.back().applyTemplateFromSField(fn); // May throw
98 }
99}
100
101STBase*
102STArray::copy(std::size_t n, void* buf) const
103{
104 return emplace(n, buf, *this);
105}
106
107STBase*
109{
110 return emplace(n, buf, std::move(*this));
111}
112
115{
116 std::string r = "[";
117
118 bool first = true;
119 for (auto const& obj : v_)
120 {
121 if (!first)
122 r += ",";
123
124 r += obj.getFullText();
125 first = false;
126 }
127
128 r += "]";
129 return r;
130}
131
134{
135 std::string r = "[";
136
137 bool first = true;
138 for (STObject const& o : v_)
139 {
140 if (!first)
141 r += ",";
142
143 r += o.getText();
144 first = false;
145 }
146
147 r += "]";
148 return r;
149}
150
153{
155 for (auto const& object : v_)
156 {
157 if (object.getSType() != STI_NOTPRESENT)
158 {
160 inner[object.getFName().getJsonName()] = object.getJson(p);
161 }
162 }
163 return v;
164}
165
166void
168{
169 for (STObject const& object : v_)
170 {
171 object.addFieldID(s);
172 object.add(s);
173 s.addFieldID(STI_OBJECT, 1);
174 }
175}
176
179{
180 return STI_ARRAY;
181}
182
183bool
185{
186 auto v = dynamic_cast<STArray const*>(&t);
187 return v != nullptr && v_ == v->v_;
188}
189
190bool
192{
193 return v_.empty();
194}
195
196void
197STArray::sort(bool (*compare)(STObject const&, STObject const&))
198{
199 std::sort(v_.begin(), v_.end(), compare);
200}
201
202} // namespace ripple
T back(T... args)
T begin(T... args)
Represents a JSON value.
Definition: json_value.h:148
Value & append(Value const &value)
Append value to array at the end.
Definition: json_value.cpp:897
Identifies fields.
Definition: SField.h:144
static SField const & getField(int fieldCode)
Definition: SField.cpp:118
void sort(bool(*compare)(STObject const &o1, STObject const &o2))
Definition: STArray.cpp:197
std::string getFullText() const override
Definition: STArray.cpp:114
STBase * move(std::size_t n, void *buf) override
Definition: STArray.cpp:108
STBase * copy(std::size_t n, void *buf) const override
Definition: STArray.cpp:102
STArray()=default
void add(Serializer &s) const override
Definition: STArray.cpp:167
bool isEquivalent(STBase const &t) const override
Definition: STArray.cpp:184
SerializedTypeID getSType() const override
Definition: STArray.cpp:178
Json::Value getJson(JsonOptions index) const override
Definition: STArray.cpp:152
bool isDefault() const override
Definition: STArray.cpp:191
list_type v_
Definition: STArray.h:33
STArray & operator=(STArray const &)=default
std::string getText() const override
Definition: STArray.cpp:133
A type which can be exported to a well known binary format.
Definition: STBase.h:126
void setFName(SField const &n)
A STBase is a field.
Definition: STBase.cpp:134
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:224
void getFieldID(int &type, int &name)
Definition: Serializer.cpp:429
std::size_t empty() const noexcept
Definition: Serializer.h:368
int addFieldID(int type, int name)
Definition: Serializer.cpp:120
T emplace_back(T... args)
T empty(T... args)
T end(T... args)
@ arrayValue
array value (ordered list)
Definition: json_value.h:43
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:44
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
SerializedTypeID
Definition: SField.h:108
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:468
STL namespace.
T reserve(T... args)
T sort(T... args)
Note, should be treated as flags that can be | and &.
Definition: STBase.h:38