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