rippled
Loading...
Searching...
No Matches
json_valueiterator.cpp
1// included by json_value.cpp
2
3#include <xrpl/json/json_forwards.h>
4#include <xrpl/json/json_value.h>
5
6namespace Json {
7
8// //////////////////////////////////////////////////////////////////
9// //////////////////////////////////////////////////////////////////
10// //////////////////////////////////////////////////////////////////
11// class ValueIteratorBase
12// //////////////////////////////////////////////////////////////////
13// //////////////////////////////////////////////////////////////////
14// //////////////////////////////////////////////////////////////////
15
17{
18}
19
20ValueIteratorBase::ValueIteratorBase(Value::ObjectValues::iterator const& current)
21 : current_(current), isNull_(false)
22{
23}
24
25Value&
27{
28 return current_->second;
29}
30
31void
36
37void
42
45{
46 // Iterator for null value are initialized using the default
47 // constructor, which initialize current_ to the default
48 // std::map::iterator. As begin() and end() are two instance
49 // of the default std::map::iterator, they can not be compared.
50 // To allow this, we handle this comparison specifically.
51 if (isNull_ && other.isNull_)
52 {
53 return 0;
54 }
55
56 // Usage of std::distance is not portable (does not compile with Sun Studio
57 // 12 RogueWave STL, which is the one used by default). Using a portable
58 // hand-made version for non random iterator instead:
59 // return difference_type( std::distance( current_, other.current_ ) );
60 difference_type myDistance = 0;
61
62 for (Value::ObjectValues::iterator it = current_; it != other.current_; ++it)
63 {
64 ++myDistance;
65 }
66
67 return myDistance;
68}
69
70bool
72{
73 if (isNull_)
74 {
75 return other.isNull_;
76 }
77
78 return current_ == other.current_;
79}
80
81void
83{
84 current_ = other.current_;
85}
86
89{
90 Value::CZString const czString = (*current_).first;
91
92 if (czString.c_str() != nullptr)
93 {
94 if (czString.isStaticString())
95 return Value(StaticString(czString.c_str()));
96
97 return Value(czString.c_str());
98 }
99
100 return Value(czString.index());
101}
102
103UInt
105{
106 Value::CZString const czString = (*current_).first;
107
108 if (czString.c_str() == nullptr)
109 return czString.index();
110
111 return Value::UInt(-1);
112}
113
114char const*
116{
117 char const* name = (*current_).first.c_str();
118 return (name != nullptr) ? name : "";
119}
120
121// //////////////////////////////////////////////////////////////////
122// //////////////////////////////////////////////////////////////////
123// //////////////////////////////////////////////////////////////////
124// class ValueConstIterator
125// //////////////////////////////////////////////////////////////////
126// //////////////////////////////////////////////////////////////////
127// //////////////////////////////////////////////////////////////////
128
129ValueConstIterator::ValueConstIterator(Value::ObjectValues::iterator const& current)
130 : ValueIteratorBase(current)
131{
132}
133
136{
137 copy(other);
138 return *this;
139}
140
141// //////////////////////////////////////////////////////////////////
142// //////////////////////////////////////////////////////////////////
143// //////////////////////////////////////////////////////////////////
144// class ValueIterator
145// //////////////////////////////////////////////////////////////////
146// //////////////////////////////////////////////////////////////////
147// //////////////////////////////////////////////////////////////////
148
149ValueIterator::ValueIterator(Value::ObjectValues::iterator const& current)
150 : ValueIteratorBase(current)
151{
152}
153
157
161
164{
165 copy(other);
166 return *this;
167}
168
169} // namespace Json
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition json_value.h:44
const iterator for object and array value.
Definition json_value.h:557
SelfType & operator=(ValueIteratorBase const &other)
base class for Value iterators.
Definition json_value.h:492
void copy(SelfType const &other)
Value key() const
Return either the index or the member name of the referenced value as a Value.
bool isEqual(SelfType const &other) const
char const * memberName() const
Return the member name of the referenced Value.
UInt index() const
Return the index of the referenced Value. -1 if it is not an arrayValue.
difference_type computeDistance(SelfType const &other) const
Value::ObjectValues::iterator current_
Definition json_value.h:548
Iterator for object and array value.
Definition json_value.h:618
SelfType & operator=(SelfType const &other)
ValueIterator()=default
bool isStaticString() const
char const * c_str() const
Represents a JSON value.
Definition json_value.h:130
Json::UInt UInt
Definition json_value.h:137
JSON (JavaScript Object Notation).
Definition json_errors.h:5
unsigned int UInt