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_()
21  , isNull_( true )
22 {
23 }
24 #else
25  : isArray_( true )
26  , isNull_( true )
27 {
28  iterator_.array_ = ValueInternalArray::IteratorState();
29 }
30 #endif
31 
32 
33 #ifndef JSON_VALUE_USE_INTERNAL_MAP
34 ValueIteratorBase::ValueIteratorBase( const Value::ObjectValues::iterator &current )
35  : current_( current )
36  , isNull_( false )
37 {
38 }
39 #else
40 ValueIteratorBase::ValueIteratorBase( const ValueInternalArray::IteratorState &state )
41  : isArray_( true )
42 {
43  iterator_.array_ = state;
44 }
45 
46 
47 ValueIteratorBase::ValueIteratorBase( const ValueInternalMap::IteratorState &state )
48  : isArray_( false )
49 {
50  iterator_.map_ = state;
51 }
52 #endif
53 
54 Value &
56 {
57 #ifndef JSON_VALUE_USE_INTERNAL_MAP
58  return current_->second;
59 #else
60  if ( isArray_ )
61  return ValueInternalArray::dereference( iterator_.array_ );
62  return ValueInternalMap::value( iterator_.map_ );
63 #endif
64 }
65 
66 
67 void
69 {
70 #ifndef JSON_VALUE_USE_INTERNAL_MAP
71  ++current_;
72 #else
73  if ( isArray_ )
74  ValueInternalArray::increment( iterator_.array_ );
75  ValueInternalMap::increment( iterator_.map_ );
76 #endif
77 }
78 
79 
80 void
82 {
83 #ifndef JSON_VALUE_USE_INTERNAL_MAP
84  --current_;
85 #else
86  if ( isArray_ )
87  ValueInternalArray::decrement( iterator_.array_ );
88  ValueInternalMap::decrement( iterator_.map_ );
89 #endif
90 }
91 
92 
95 {
96 #ifndef JSON_VALUE_USE_INTERNAL_MAP
97 # ifdef JSON_USE_CPPTL_SMALLMAP
98  return current_ - other.current_;
99 # else
100  // Iterator for null value are initialized using the default
101  // constructor, which initialize current_ to the default
102  // std::map::iterator. As begin() and end() are two instance
103  // of the default std::map::iterator, they can not be compared.
104  // To allow this, we handle this comparison specifically.
105  if ( isNull_ && other.isNull_ )
106  {
107  return 0;
108  }
109 
110 
111  // Usage of std::distance is not portable (does not compile with Sun Studio 12 RogueWave STL,
112  // which is the one used by default).
113  // Using a portable hand-made version for non random iterator instead:
114  // return difference_type( std::distance( current_, other.current_ ) );
115  difference_type myDistance = 0;
116  for ( Value::ObjectValues::iterator it = current_; it != other.current_; ++it )
117  {
118  ++myDistance;
119  }
120  return myDistance;
121 # endif
122 #else
123  if ( isArray_ )
124  return ValueInternalArray::distance( iterator_.array_, other.iterator_.array_ );
125  return ValueInternalMap::distance( iterator_.map_, other.iterator_.map_ );
126 #endif
127 }
128 
129 
130 bool
132 {
133 #ifndef JSON_VALUE_USE_INTERNAL_MAP
134  if ( isNull_ )
135  {
136  return other.isNull_;
137  }
138  return current_ == other.current_;
139 #else
140  if ( isArray_ )
141  return ValueInternalArray::equals( iterator_.array_, other.iterator_.array_ );
142  return ValueInternalMap::equals( iterator_.map_, other.iterator_.map_ );
143 #endif
144 }
145 
146 
147 void
149 {
150 #ifndef JSON_VALUE_USE_INTERNAL_MAP
151  current_ = other.current_;
152  isNull_ = other.isNull_;
153 #else
154  if ( isArray_ )
155  iterator_.array_ = other.iterator_.array_;
156  iterator_.map_ = other.iterator_.map_;
157 #endif
158 }
159 
160 
161 Value
163 {
164 #ifndef JSON_VALUE_USE_INTERNAL_MAP
165  const Value::CZString czstring = (*current_).first;
166  if ( czstring.c_str() )
167  {
168  if ( czstring.isStaticString() )
169  return Value( StaticString( czstring.c_str() ) );
170  return Value( czstring.c_str() );
171  }
172  return Value( czstring.index() );
173 #else
174  if ( isArray_ )
175  return Value( ValueInternalArray::indexOf( iterator_.array_ ) );
176  bool isStatic;
177  const char *memberName = ValueInternalMap::key( iterator_.map_, isStatic );
178  if ( isStatic )
179  return Value( StaticString( memberName ) );
180  return Value( memberName );
181 #endif
182 }
183 
184 
185 UInt
187 {
188 #ifndef JSON_VALUE_USE_INTERNAL_MAP
189  const Value::CZString czstring = (*current_).first;
190  if ( !czstring.c_str() )
191  return czstring.index();
192  return Value::UInt( -1 );
193 #else
194  if ( isArray_ )
195  return Value::UInt( ValueInternalArray::indexOf( iterator_.array_ ) );
196  return Value::UInt( -1 );
197 #endif
198 }
199 
200 
201 const char *
203 {
204 #ifndef JSON_VALUE_USE_INTERNAL_MAP
205  const char *name = (*current_).first.c_str();
206  return name ? name : "";
207 #else
208  if ( !isArray_ )
209  return ValueInternalMap::key( iterator_.map_ );
210  return "";
211 #endif
212 }
213 
214 
215 // //////////////////////////////////////////////////////////////////
216 // //////////////////////////////////////////////////////////////////
217 // //////////////////////////////////////////////////////////////////
218 // class ValueConstIterator
219 // //////////////////////////////////////////////////////////////////
220 // //////////////////////////////////////////////////////////////////
221 // //////////////////////////////////////////////////////////////////
222 
224 {
225 }
226 
227 
228 #ifndef JSON_VALUE_USE_INTERNAL_MAP
229 ValueConstIterator::ValueConstIterator( const Value::ObjectValues::iterator &current )
230  : ValueIteratorBase( current )
231 {
232 }
233 #else
234 ValueConstIterator::ValueConstIterator( const ValueInternalArray::IteratorState &state )
235  : ValueIteratorBase( state )
236 {
237 }
238 
239 ValueConstIterator::ValueConstIterator( const ValueInternalMap::IteratorState &state )
240  : ValueIteratorBase( state )
241 {
242 }
243 #endif
244 
245 ValueConstIterator &
247 {
248  copy( other );
249  return *this;
250 }
251 
252 
253 // //////////////////////////////////////////////////////////////////
254 // //////////////////////////////////////////////////////////////////
255 // //////////////////////////////////////////////////////////////////
256 // class ValueIterator
257 // //////////////////////////////////////////////////////////////////
258 // //////////////////////////////////////////////////////////////////
259 // //////////////////////////////////////////////////////////////////
260 
262 {
263 }
264 
265 
266 #ifndef JSON_VALUE_USE_INTERNAL_MAP
267 ValueIterator::ValueIterator( const Value::ObjectValues::iterator &current )
268  : ValueIteratorBase( current )
269 {
270 }
271 #else
272 ValueIterator::ValueIterator( const ValueInternalArray::IteratorState &state )
273  : ValueIteratorBase( state )
274 {
275 }
276 
277 ValueIterator::ValueIterator( const ValueInternalMap::IteratorState &state )
278  : ValueIteratorBase( state )
279 {
280 }
281 #endif
282 
284  : ValueIteratorBase( other )
285 {
286 }
287 
289  : ValueIteratorBase( other )
290 {
291 }
292 
295 {
296  copy( other );
297  return *this;
298 }
299 
300 } // namespace Json
301 // vim: et ts=3 sts=3 sw=3 tw=0
Json::ValueConstIterator::operator=
SelfType & operator=(const ValueIteratorBase &other)
Definition: json_valueiterator.inl:246
Json::ValueConstIterator
const iterator for object and array value.
Definition: value.h:972
Json::ValueIterator
Iterator for object and array value.
Definition: value.h:1024
Json::ValueIteratorBase::difference_type
int difference_type
Definition: value.h:912
Json::ValueIteratorBase::computeDistance
difference_type computeDistance(const SelfType &other) const
Definition: json_valueiterator.inl:94
Json::ValueIteratorBase::deref
Value & deref() const
Definition: json_valueiterator.inl:55
Json::UInt
unsigned int UInt
Definition: config.h:92
Json::ValueIteratorBase::copy
void copy(const SelfType &other)
Definition: json_valueiterator.inl:148
Json::ValueIteratorBase::key
Value key() const
Return either the index or the member name of the referenced value as a Value.
Definition: json_valueiterator.inl:162
Json::ValueConstIterator::ValueConstIterator
ValueConstIterator()
Definition: json_valueiterator.inl:223
Json::ValueIteratorBase::map_
ValueInternalMap::IteratorState map_
Definition: value.h:963
Json::ValueIteratorBase::memberName
const char * memberName() const
Return the member name of the referenced Value.
Definition: json_valueiterator.inl:202
Json::ValueIteratorBase::index
UInt index() const
Return the index of the referenced Value. -1 if it is not an arrayValue.
Definition: json_valueiterator.inl:186
Json::Value
Represents a JSON value.
Definition: value.h:116
Json::ValueIterator::operator=
SelfType & operator=(const SelfType &other)
Definition: json_valueiterator.inl:294
Json::ValueIterator::ValueIterator
ValueIterator()
Definition: json_valueiterator.inl:261
Json
JSON (JavaScript Object Notation).
Definition: config.h:90
Json::Value::UInt
Json::UInt UInt
Definition: value.h:126
Json::ValueIteratorBase
base class for Value iterators.
Definition: value.h:908
Json::ValueIteratorBase::ValueIteratorBase
ValueIteratorBase()
Definition: json_valueiterator.inl:18
Json::ValueIteratorBase::array_
ValueInternalArray::IteratorState array_
Definition: value.h:962
Json::ValueIteratorBase::isEqual
bool isEqual(const SelfType &other) const
Definition: json_valueiterator.inl:131
Json::ValueIteratorBase::increment
void increment()
Definition: json_valueiterator.inl:68
Json::StaticString
Lightweight wrapper to tag static string.
Definition: value.h:75
Json::ValueIteratorBase::decrement
void decrement()
Definition: json_valueiterator.inl:81