rippled
Loading...
Searching...
No Matches
json_valueiterator.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20// included by json_value.cpp
21
22#include <xrpl/json/json_forwards.h>
23#include <xrpl/json/json_value.h>
24
25namespace Json {
26
27// //////////////////////////////////////////////////////////////////
28// //////////////////////////////////////////////////////////////////
29// //////////////////////////////////////////////////////////////////
30// class ValueIteratorBase
31// //////////////////////////////////////////////////////////////////
32// //////////////////////////////////////////////////////////////////
33// //////////////////////////////////////////////////////////////////
34
35ValueIteratorBase::ValueIteratorBase() : current_(), isNull_(true)
36{
37}
38
40 Value::ObjectValues::iterator const& current)
41 : current_(current), isNull_(false)
42{
43}
44
45Value&
47{
48 return current_->second;
49}
50
51void
56
57void
62
65{
66 // Iterator for null value are initialized using the default
67 // constructor, which initialize current_ to the default
68 // std::map::iterator. As begin() and end() are two instance
69 // of the default std::map::iterator, they can not be compared.
70 // To allow this, we handle this comparison specifically.
71 if (isNull_ && other.isNull_)
72 {
73 return 0;
74 }
75
76 // Usage of std::distance is not portable (does not compile with Sun Studio
77 // 12 RogueWave STL, which is the one used by default). Using a portable
78 // hand-made version for non random iterator instead:
79 // return difference_type( std::distance( current_, other.current_ ) );
80 difference_type myDistance = 0;
81
82 for (Value::ObjectValues::iterator it = current_; it != other.current_;
83 ++it)
84 {
85 ++myDistance;
86 }
87
88 return myDistance;
89}
90
91bool
93{
94 if (isNull_)
95 {
96 return other.isNull_;
97 }
98
99 return current_ == other.current_;
100}
101
102void
104{
105 current_ = other.current_;
106}
107
108Value
110{
111 Value::CZString const czstring = (*current_).first;
112
113 if (czstring.c_str())
114 {
115 if (czstring.isStaticString())
116 return Value(StaticString(czstring.c_str()));
117
118 return Value(czstring.c_str());
119 }
120
121 return Value(czstring.index());
122}
123
124UInt
126{
127 Value::CZString const czstring = (*current_).first;
128
129 if (!czstring.c_str())
130 return czstring.index();
131
132 return Value::UInt(-1);
133}
134
135char const*
137{
138 char const* name = (*current_).first.c_str();
139 return name ? name : "";
140}
141
142// //////////////////////////////////////////////////////////////////
143// //////////////////////////////////////////////////////////////////
144// //////////////////////////////////////////////////////////////////
145// class ValueConstIterator
146// //////////////////////////////////////////////////////////////////
147// //////////////////////////////////////////////////////////////////
148// //////////////////////////////////////////////////////////////////
149
151 Value::ObjectValues::iterator const& current)
152 : ValueIteratorBase(current)
153{
154}
155
158{
159 copy(other);
160 return *this;
161}
162
163// //////////////////////////////////////////////////////////////////
164// //////////////////////////////////////////////////////////////////
165// //////////////////////////////////////////////////////////////////
166// class ValueIterator
167// //////////////////////////////////////////////////////////////////
168// //////////////////////////////////////////////////////////////////
169// //////////////////////////////////////////////////////////////////
170
171ValueIterator::ValueIterator(Value::ObjectValues::iterator const& current)
172 : ValueIteratorBase(current)
173{
174}
175
180
185
188{
189 copy(other);
190 return *this;
191}
192
193} // namespace Json
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition json_value.h:63
const iterator for object and array value.
Definition json_value.h:573
SelfType & operator=(ValueIteratorBase const &other)
base class for Value iterators.
Definition json_value.h:508
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:564
Iterator for object and array value.
Definition json_value.h:634
SelfType & operator=(SelfType const &other)
ValueIterator()=default
bool isStaticString() const
char const * c_str() const
Represents a JSON value.
Definition json_value.h:149
Json::UInt UInt
Definition json_value.h:156
JSON (JavaScript Object Notation).
Definition json_errors.h:25
unsigned int UInt