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
16ValueIteratorBase::ValueIteratorBase() : current_(), isNull_(true)
17{
18}
19
20ValueIteratorBase::ValueIteratorBase(Value::ObjectValues::iterator const& current) : current_(current), isNull_(false)
21{
22}
23
24Value&
26{
27 return current_->second;
28}
29
30void
35
36void
41
44{
45 // Iterator for null value are initialized using the default
46 // constructor, which initialize current_ to the default
47 // std::map::iterator. As begin() and end() are two instance
48 // of the default std::map::iterator, they can not be compared.
49 // To allow this, we handle this comparison specifically.
50 if (isNull_ && other.isNull_)
51 {
52 return 0;
53 }
54
55 // Usage of std::distance is not portable (does not compile with Sun Studio
56 // 12 RogueWave STL, which is the one used by default). Using a portable
57 // hand-made version for non random iterator instead:
58 // return difference_type( std::distance( current_, other.current_ ) );
59 difference_type myDistance = 0;
60
61 for (Value::ObjectValues::iterator it = current_; it != other.current_; ++it)
62 {
63 ++myDistance;
64 }
65
66 return myDistance;
67}
68
69bool
71{
72 if (isNull_)
73 {
74 return other.isNull_;
75 }
76
77 return current_ == other.current_;
78}
79
80void
82{
83 current_ = other.current_;
84}
85
88{
89 Value::CZString const czString = (*current_).first;
90
91 if (czString.c_str())
92 {
93 if (czString.isStaticString())
94 return Value(StaticString(czString.c_str()));
95
96 return Value(czString.c_str());
97 }
98
99 return Value(czString.index());
100}
101
102UInt
104{
105 Value::CZString const czString = (*current_).first;
106
107 if (!czString.c_str())
108 return czString.index();
109
110 return Value::UInt(-1);
111}
112
113char const*
115{
116 char const* name = (*current_).first.c_str();
117 return name ? name : "";
118}
119
120// //////////////////////////////////////////////////////////////////
121// //////////////////////////////////////////////////////////////////
122// //////////////////////////////////////////////////////////////////
123// class ValueConstIterator
124// //////////////////////////////////////////////////////////////////
125// //////////////////////////////////////////////////////////////////
126// //////////////////////////////////////////////////////////////////
127
128ValueConstIterator::ValueConstIterator(Value::ObjectValues::iterator const& current) : ValueIteratorBase(current)
129{
130}
131
134{
135 copy(other);
136 return *this;
137}
138
139// //////////////////////////////////////////////////////////////////
140// //////////////////////////////////////////////////////////////////
141// //////////////////////////////////////////////////////////////////
142// class ValueIterator
143// //////////////////////////////////////////////////////////////////
144// //////////////////////////////////////////////////////////////////
145// //////////////////////////////////////////////////////////////////
146
147ValueIterator::ValueIterator(Value::ObjectValues::iterator const& current) : ValueIteratorBase(current)
148{
149}
150
154
158
161{
162 copy(other);
163 return *this;
164}
165
166} // namespace Json
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition json_value.h:45
const iterator for object and array value.
Definition json_value.h:558
SelfType & operator=(ValueIteratorBase const &other)
base class for Value iterators.
Definition json_value.h:493
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:549
Iterator for object and array value.
Definition json_value.h:619
SelfType & operator=(SelfType const &other)
ValueIterator()=default
bool isStaticString() const
char const * c_str() const
Represents a JSON value.
Definition json_value.h:131
Json::UInt UInt
Definition json_value.h:138
JSON (JavaScript Object Notation).
Definition json_errors.h:6
unsigned int UInt