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