rippled
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 <ripple/json/json_value.h>
23 
24 namespace Json {
25 
26 // //////////////////////////////////////////////////////////////////
27 // //////////////////////////////////////////////////////////////////
28 // //////////////////////////////////////////////////////////////////
29 // class ValueIteratorBase
30 // //////////////////////////////////////////////////////////////////
31 // //////////////////////////////////////////////////////////////////
32 // //////////////////////////////////////////////////////////////////
33 
34 ValueIteratorBase::ValueIteratorBase() : current_(), isNull_(true)
35 {
36 }
37 
39  const Value::ObjectValues::iterator& current)
40  : current_(current), isNull_(false)
41 {
42 }
43 
44 Value&
46 {
47  return current_->second;
48 }
49 
50 void
52 {
53  ++current_;
54 }
55 
56 void
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 
90 bool
92 {
93  if (isNull_)
94  {
95  return other.isNull_;
96  }
97 
98  return current_ == other.current_;
99 }
100 
101 void
103 {
104  current_ = other.current_;
105 }
106 
107 Value
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 
123 UInt
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 
134 const 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 
170 ValueIterator::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
Json::ValueIterator
Iterator for object and array value.
Definition: json_value.h:620
Json::ValueIteratorBase::copy
void copy(const SelfType &other)
Definition: json_valueiterator.cpp:102
Json::Value::CZString::index
int index() const
Definition: json_value.cpp:151
Json::ValueConstIterator::operator=
SelfType & operator=(const ValueIteratorBase &other)
Definition: json_valueiterator.cpp:156
Json::UInt
unsigned int UInt
Definition: json_forwards.h:27
Json::ValueIteratorBase::ValueIteratorBase
ValueIteratorBase()
Definition: json_valueiterator.cpp:34
Json::Value::CZString::c_str
const char * c_str() const
Definition: json_value.cpp:157
Json::ValueIteratorBase::index
UInt index() const
Return the index of the referenced Value. -1 if it is not an arrayValue.
Definition: json_valueiterator.cpp:124
Json::ValueIteratorBase::decrement
void decrement()
Definition: json_valueiterator.cpp:57
Json::ValueIteratorBase::isEqual
bool isEqual(const SelfType &other) const
Definition: json_valueiterator.cpp:91
Json::ValueIteratorBase::key
Value key() const
Return either the index or the member name of the referenced value as a Value.
Definition: json_valueiterator.cpp:108
Json::ValueIteratorBase::current_
Value::ObjectValues::iterator current_
Definition: json_value.h:551
Json::ValueIteratorBase::isNull_
bool isNull_
Definition: json_value.h:553
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:27
Json::ValueConstIterator
const iterator for object and array value.
Definition: json_value.h:559
std::string::c_str
T c_str(T... args)
Json::ValueIteratorBase
base class for Value iterators.
Definition: json_value.h:494
Json::ValueIterator::ValueIterator
ValueIterator()=default
Json::Value::UInt
Json::UInt UInt
Definition: json_value.h:153
Json::ValueIteratorBase::computeDistance
difference_type computeDistance(const SelfType &other) const
Definition: json_valueiterator.cpp:63
Json::ValueIterator::operator=
SelfType & operator=(const SelfType &other)
Definition: json_valueiterator.cpp:186
Json::Value::CZString
Definition: json_value.h:163
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:60
Json::ValueIteratorBase::increment
void increment()
Definition: json_valueiterator.cpp:51
Json::Value::CZString::isStaticString
bool isStaticString() const
Definition: json_value.cpp:163
Json::ValueIteratorBase::difference_type
int difference_type
Definition: json_value.h:498
Json::Value
Represents a JSON value.
Definition: json_value.h:145
Json::ValueIteratorBase::deref
Value & deref() const
Definition: json_valueiterator.cpp:45
Json::ValueConstIterator::ValueConstIterator
ValueConstIterator()=default
Json::ValueIteratorBase::memberName
const char * memberName() const
Return the member name of the referenced Value.
Definition: json_valueiterator.cpp:135