xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
protocol
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
16
namespace
xrpl
{
17
18
STArray::STArray
(
STArray
&& other) :
STBase
(other.
getFName
()),
v_
(
std
::
move
(other.
v_
))
19
{
20
}
21
22
STArray
&
23
STArray::operator=
(
STArray
&& other)
24
{
25
setFName
(other.getFName());
26
v_
= std::move(other.v_);
27
return
*
this
;
28
}
29
30
STArray::STArray
(
int
n)
31
{
32
v_
.reserve(n);
33
}
34
35
STArray::STArray
(
SField
const
& f) :
STBase
(f)
36
{
37
}
38
39
STArray::STArray
(
SField
const
& f,
std::size_t
n) :
STBase
(f)
40
{
41
v_
.reserve(n);
42
}
43
44
STArray::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
80
STBase*
81
STArray::copy
(
std::size_t
n,
void
* buf)
const
82
{
83
return
emplace
(n, buf, *
this
);
84
}
85
86
STBase
*
87
STArray::move
(
std::size_t
n,
void
* buf)
88
{
89
return
emplace
(n, buf, std::move(*
this
));
90
}
91
92
std::string
93
STArray::getFullText
()
const
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
111
std::string
112
STArray::getText
()
const
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
130
json::Value
131
STArray::getJson
(
JsonOptions
p)
const
132
{
133
json::Value
v =
json::ValueType::Array
;
134
for
(
auto
const
&
object
:
v_
)
135
{
136
if
(
object
.
getSType
() != STI_NOTPRESENT)
137
{
138
json::Value
& inner = v.
append
(
json::ValueType::Object
);
139
inner[
object
.getFName().getJsonName()] =
object
.getJson(p);
140
}
141
}
142
return
v;
143
}
144
145
void
146
STArray::add
(
Serializer
& s)
const
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
156
SerializedTypeID
157
STArray::getSType
()
const
158
{
159
return
STI_ARRAY;
160
}
161
162
bool
163
STArray::isEquivalent
(
STBase
const
& t)
const
164
{
165
auto
v =
dynamic_cast<
STArray
const*
>
(&t);
166
return
v !=
nullptr
&&
v_
== v->v_;
167
}
168
169
bool
170
STArray::isDefault
()
const
171
{
172
return
v_
.empty();
173
}
174
175
void
176
STArray::sort
(
bool
(*compare)(
STObject
const
&,
STObject
const
&))
177
{
178
std::ranges::sort
(
v_
, compare);
179
}
180
181
}
// namespace xrpl
algorithm
std::string
json::Value
Represents a JSON value.
Definition
json_value.h:117
json::Value::append
Value & append(Value const &value)
Append value to array at the end.
Definition
json_value.cpp:997
xrpl::SField
Identifies fields.
Definition
SField.h:132
xrpl::SField::getField
static SField const & getField(int fieldCode)
Definition
SField.cpp:108
xrpl::STArray
Definition
STArray.h:20
xrpl::STArray::operator=
STArray & operator=(STArray const &)=default
xrpl::STArray::STArray
STArray()=default
xrpl::STArray::getFullText
std::string getFullText() const override
Definition
STArray.cpp:93
xrpl::STArray::isEquivalent
bool isEquivalent(STBase const &t) const override
Definition
STArray.cpp:163
xrpl::STArray::copy
STBase * copy(std::size_t n, void *buf) const override
Definition
STArray.cpp:81
xrpl::STArray::getText
std::string getText() const override
Definition
STArray.cpp:112
xrpl::STArray::getSType
SerializedTypeID getSType() const override
Definition
STArray.cpp:157
xrpl::STArray::v_
ListType v_
Definition
STArray.h:24
xrpl::STArray::add
void add(Serializer &s) const override
Definition
STArray.cpp:146
xrpl::STArray::isDefault
bool isDefault() const override
Definition
STArray.cpp:170
xrpl::STArray::move
STBase * move(std::size_t n, void *buf) override
Definition
STArray.cpp:87
xrpl::STArray::sort
void sort(bool(*compare)(STObject const &o1, STObject const &o2))
Definition
STArray.cpp:176
xrpl::STArray::getJson
json::Value getJson(JsonOptions index) const override
Definition
STArray.cpp:131
xrpl::STBase
A type which can be exported to a well known binary format.
Definition
STBase.h:129
xrpl::STBase::getFName
SField const & getFName() const
Definition
STBase.cpp:120
xrpl::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition
STBase.h:226
xrpl::STBase::STBase
STBase()
Definition
STBase.cpp:15
xrpl::STBase::setFName
void setFName(SField const &n)
A STBase is a field.
Definition
STBase.cpp:113
xrpl::STObject
Definition
STObject.h:49
xrpl::SerialIter
Definition
Serializer.h:478
xrpl::SerialIter::empty
bool empty() const noexcept
Definition
Serializer.h:499
xrpl::Serializer
Definition
Serializer.h:23
xrpl::Serializer::addFieldID
int addFieldID(int type, int name)
Definition
libxrpl/protocol/Serializer.cpp:107
cstddef
json::ValueType::Array
@ Array
array value (ordered list)
Definition
json_value.h:28
json::ValueType::Object
@ Object
object value (collection of name/value pairs).
Definition
json_value.h:29
std
STL namespace.
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::SerializedTypeID
SerializedTypeID
Definition
SField.h:94
std::size_t
std::ranges::sort
T sort(T... args)
stdexcept
string
xrpl::JsonOptions
Note, should be treated as flags that can be | and &.
Definition
STBase.h:22
utility
Generated by
1.17.0