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