JsonCpp project page JsonCpp home page

json_valueiterator.inl
Go to the documentation of this file.
1 // Copyright 2007-2010 Baptiste Lepilleur
2 // Distributed under MIT license, or public domain if desired and
3 // recognized in your jurisdiction.
4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5 
6 // included by json_value.cpp
7 
8 namespace Json {
9 
10 // //////////////////////////////////////////////////////////////////
11 // //////////////////////////////////////////////////////////////////
12 // //////////////////////////////////////////////////////////////////
13 // class ValueIteratorBase
14 // //////////////////////////////////////////////////////////////////
15 // //////////////////////////////////////////////////////////////////
16 // //////////////////////////////////////////////////////////////////
17 
19 #ifndef JSON_VALUE_USE_INTERNAL_MAP
20  : current_(), isNull_(true) {
21 }
22 #else
23  : isArray_(true), isNull_(true) {
24  iterator_.array_ = ValueInternalArray::IteratorState();
25 }
26 #endif
27 
28 #ifndef JSON_VALUE_USE_INTERNAL_MAP
30  const Value::ObjectValues::iterator& current)
31  : current_(current), isNull_(false) {}
32 #else
34  const ValueInternalArray::IteratorState& state)
35  : isArray_(true) {
36  iterator_.array_ = state;
37 }
38 
40  const ValueInternalMap::IteratorState& state)
41  : isArray_(false) {
42  iterator_.map_ = state;
43 }
44 #endif
45 
47 #ifndef JSON_VALUE_USE_INTERNAL_MAP
48  return current_->second;
49 #else
50  if (isArray_)
51  return ValueInternalArray::dereference(iterator_.array_);
52  return ValueInternalMap::value(iterator_.map_);
53 #endif
54 }
55 
57 #ifndef JSON_VALUE_USE_INTERNAL_MAP
58  ++current_;
59 #else
60  if (isArray_)
61  ValueInternalArray::increment(iterator_.array_);
62  ValueInternalMap::increment(iterator_.map_);
63 #endif
64 }
65 
67 #ifndef JSON_VALUE_USE_INTERNAL_MAP
68  --current_;
69 #else
70  if (isArray_)
71  ValueInternalArray::decrement(iterator_.array_);
72  ValueInternalMap::decrement(iterator_.map_);
73 #endif
74 }
75 
78 #ifndef JSON_VALUE_USE_INTERNAL_MAP
79 #ifdef JSON_USE_CPPTL_SMALLMAP
80  return current_ - other.current_;
81 #else
82  // Iterator for null value are initialized using the default
83  // constructor, which initialize current_ to the default
84  // std::map::iterator. As begin() and end() are two instance
85  // of the default std::map::iterator, they can not be compared.
86  // To allow this, we handle this comparison specifically.
87  if (isNull_ && other.isNull_) {
88  return 0;
89  }
90 
91  // Usage of std::distance is not portable (does not compile with Sun Studio 12
92  // RogueWave STL,
93  // which is the one used by default).
94  // Using a portable hand-made version for non random iterator instead:
95  // return difference_type( std::distance( current_, other.current_ ) );
96  difference_type myDistance = 0;
97  for (Value::ObjectValues::iterator it = current_; it != other.current_;
98  ++it) {
99  ++myDistance;
100  }
101  return myDistance;
102 #endif
103 #else
104  if (isArray_)
105  return ValueInternalArray::distance(iterator_.array_,
106  other.iterator_.array_);
107  return ValueInternalMap::distance(iterator_.map_, other.iterator_.map_);
108 #endif
109 }
110 
111 bool ValueIteratorBase::isEqual(const SelfType& other) const {
112 #ifndef JSON_VALUE_USE_INTERNAL_MAP
113  if (isNull_) {
114  return other.isNull_;
115  }
116  return current_ == other.current_;
117 #else
118  if (isArray_)
119  return ValueInternalArray::equals(iterator_.array_, other.iterator_.array_);
120  return ValueInternalMap::equals(iterator_.map_, other.iterator_.map_);
121 #endif
122 }
123 
124 void ValueIteratorBase::copy(const SelfType& other) {
125 #ifndef JSON_VALUE_USE_INTERNAL_MAP
126  current_ = other.current_;
127  isNull_ = other.isNull_;
128 #else
129  if (isArray_)
130  iterator_.array_ = other.iterator_.array_;
131  iterator_.map_ = other.iterator_.map_;
132 #endif
133 }
134 
136 #ifndef JSON_VALUE_USE_INTERNAL_MAP
137  const Value::CZString czstring = (*current_).first;
138  if (czstring.c_str()) {
139  if (czstring.isStaticString())
140  return Value(StaticString(czstring.c_str()));
141  return Value(czstring.c_str());
142  }
143  return Value(czstring.index());
144 #else
145  if (isArray_)
146  return Value(ValueInternalArray::indexOf(iterator_.array_));
147  bool isStatic;
148  const char* memberName = ValueInternalMap::key(iterator_.map_, isStatic);
149  if (isStatic)
150  return Value(StaticString(memberName));
151  return Value(memberName);
152 #endif
153 }
154 
156 #ifndef JSON_VALUE_USE_INTERNAL_MAP
157  const Value::CZString czstring = (*current_).first;
158  if (!czstring.c_str())
159  return czstring.index();
160  return Value::UInt(-1);
161 #else
162  if (isArray_)
163  return Value::UInt(ValueInternalArray::indexOf(iterator_.array_));
164  return Value::UInt(-1);
165 #endif
166 }
167 
168 const char* ValueIteratorBase::memberName() const {
169 #ifndef JSON_VALUE_USE_INTERNAL_MAP
170  const char* name = (*current_).first.c_str();
171  return name ? name : "";
172 #else
173  if (!isArray_)
174  return ValueInternalMap::key(iterator_.map_);
175  return "";
176 #endif
177 }
178 
179 // //////////////////////////////////////////////////////////////////
180 // //////////////////////////////////////////////////////////////////
181 // //////////////////////////////////////////////////////////////////
182 // class ValueConstIterator
183 // //////////////////////////////////////////////////////////////////
184 // //////////////////////////////////////////////////////////////////
185 // //////////////////////////////////////////////////////////////////
186 
188 
189 #ifndef JSON_VALUE_USE_INTERNAL_MAP
191  const Value::ObjectValues::iterator& current)
192  : ValueIteratorBase(current) {}
193 #else
195  const ValueInternalArray::IteratorState& state)
196  : ValueIteratorBase(state) {}
197 
199  const ValueInternalMap::IteratorState& state)
200  : ValueIteratorBase(state) {}
201 #endif
202 
203 ValueConstIterator& ValueConstIterator::
205  copy(other);
206  return *this;
207 }
208 
209 // //////////////////////////////////////////////////////////////////
210 // //////////////////////////////////////////////////////////////////
211 // //////////////////////////////////////////////////////////////////
212 // class ValueIterator
213 // //////////////////////////////////////////////////////////////////
214 // //////////////////////////////////////////////////////////////////
215 // //////////////////////////////////////////////////////////////////
216 
218 
219 #ifndef JSON_VALUE_USE_INTERNAL_MAP
220 ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
221  : ValueIteratorBase(current) {}
222 #else
223 ValueIterator::ValueIterator(const ValueInternalArray::IteratorState& state)
224  : ValueIteratorBase(state) {}
225 
226 ValueIterator::ValueIterator(const ValueInternalMap::IteratorState& state)
227  : ValueIteratorBase(state) {}
228 #endif
229 
231  : ValueIteratorBase(other) {}
232 
234  : ValueIteratorBase(other) {}
235 
237  copy(other);
238  return *this;
239 }
240 
241 } // namespace Json
base class for Value iterators.
Definition: value.h:908
Lightweight wrapper to tag static string.
Definition: value.h:75
ValueInternalMap::IteratorState map_
Definition: value.h:963
bool isEqual(const SelfType &other) const
difference_type computeDistance(const SelfType &other) const
void copy(const SelfType &other)
const iterator for object and array value.
Definition: value.h:972
SelfType & operator=(const SelfType &other)
Json::UInt UInt
Definition: value.h:126
SelfType & operator=(const ValueIteratorBase &other)
Represents a JSON value.
Definition: value.h:116
UInt index() const
Return the index of the referenced Value. -1 if it is not an arrayValue.
unsigned int UInt
Definition: config.h:92
Value key() const
Return either the index or the member name of the referenced value as a Value.
Iterator for object and array value.
Definition: value.h:1026
ValueInternalArray::IteratorState array_
Definition: value.h:962
const char * memberName() const
Return the member name of the referenced Value.