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