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_value.h>
23
24namespace Json {
25
26// //////////////////////////////////////////////////////////////////
27// //////////////////////////////////////////////////////////////////
28// //////////////////////////////////////////////////////////////////
29// class ValueIteratorBase
30// //////////////////////////////////////////////////////////////////
31// //////////////////////////////////////////////////////////////////
32// //////////////////////////////////////////////////////////////////
33
34ValueIteratorBase::ValueIteratorBase() : current_(), isNull_(true)
35{
36}
37
39 const Value::ObjectValues::iterator& current)
40 : current_(current), isNull_(false)
41{
42}
43
44Value&
46{
47 return current_->second;
48}
49
50void
52{
53 ++current_;
54}
55
56void
58{
59 --current_;
60}
61
64{
65 // Iterator for null value are initialized using the default
66 // constructor, which initialize current_ to the default
67 // std::map::iterator. As begin() and end() are two instance
68 // of the default std::map::iterator, they can not be compared.
69 // To allow this, we handle this comparison specifically.
70 if (isNull_ && other.isNull_)
71 {
72 return 0;
73 }
74
75 // Usage of std::distance is not portable (does not compile with Sun Studio
76 // 12 RogueWave STL, which is the one used by default). Using a portable
77 // hand-made version for non random iterator instead:
78 // return difference_type( std::distance( current_, other.current_ ) );
79 difference_type myDistance = 0;
80
81 for (Value::ObjectValues::iterator it = current_; it != other.current_;
82 ++it)
83 {
84 ++myDistance;
85 }
86
87 return myDistance;
88}
89
90bool
92{
93 if (isNull_)
94 {
95 return other.isNull_;
96 }
97
98 return current_ == other.current_;
99}
100
101void
103{
104 current_ = other.current_;
105}
106
107Value
109{
110 const Value::CZString czstring = (*current_).first;
111
112 if (czstring.c_str())
113 {
114 if (czstring.isStaticString())
115 return Value(StaticString(czstring.c_str()));
116
117 return Value(czstring.c_str());
118 }
119
120 return Value(czstring.index());
121}
122
123UInt
125{
126 const Value::CZString czstring = (*current_).first;
127
128 if (!czstring.c_str())
129 return czstring.index();
130
131 return Value::UInt(-1);
132}
133
134const char*
136{
137 const char* name = (*current_).first.c_str();
138 return name ? name : "";
139}
140
141// //////////////////////////////////////////////////////////////////
142// //////////////////////////////////////////////////////////////////
143// //////////////////////////////////////////////////////////////////
144// class ValueConstIterator
145// //////////////////////////////////////////////////////////////////
146// //////////////////////////////////////////////////////////////////
147// //////////////////////////////////////////////////////////////////
148
150 const Value::ObjectValues::iterator& current)
151 : ValueIteratorBase(current)
152{
153}
154
157{
158 copy(other);
159 return *this;
160}
161
162// //////////////////////////////////////////////////////////////////
163// //////////////////////////////////////////////////////////////////
164// //////////////////////////////////////////////////////////////////
165// class ValueIterator
166// //////////////////////////////////////////////////////////////////
167// //////////////////////////////////////////////////////////////////
168// //////////////////////////////////////////////////////////////////
169
170ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
171 : ValueIteratorBase(current)
172{
173}
174
176 : ValueIteratorBase(other)
177{
178}
179
181 : ValueIteratorBase(other)
182{
183}
184
187{
188 copy(other);
189 return *this;
190}
191
192} // namespace Json
T c_str(T... args)
Lightweight wrapper to tag static string.
Definition: json_value.h:61
const iterator for object and array value.
Definition: json_value.h:562
SelfType & operator=(const ValueIteratorBase &other)
base class for Value iterators.
Definition: json_value.h:497
bool isEqual(const SelfType &other) const
Value key() const
Return either the index or the member name of the referenced value as a Value.
void copy(const SelfType &other)
UInt index() const
Return the index of the referenced Value. -1 if it is not an arrayValue.
const char * memberName() const
Return the member name of the referenced Value.
Value::ObjectValues::iterator current_
Definition: json_value.h:553
difference_type computeDistance(const SelfType &other) const
Iterator for object and array value.
Definition: json_value.h:623
ValueIterator()=default
SelfType & operator=(const SelfType &other)
bool isStaticString() const
Definition: json_value.cpp:163
const char * c_str() const
Definition: json_value.cpp:157
Represents a JSON value.
Definition: json_value.h:147
Json::UInt UInt
Definition: json_value.h:154
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
unsigned int UInt
Definition: json_forwards.h:27