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
6 // included by json_value.cpp
7 // everything is within Json namespace
10 // //////////////////////////////////////////////////////////////////
11 // //////////////////////////////////////////////////////////////////
12 // //////////////////////////////////////////////////////////////////
13 // class ValueIteratorBase
14 // //////////////////////////////////////////////////////////////////
15 // //////////////////////////////////////////////////////////////////
16 // //////////////////////////////////////////////////////////////////
18 ValueIteratorBase::ValueIteratorBase()
19 #ifndef JSON_VALUE_USE_INTERNAL_MAP
28 iterator_.array_ = ValueInternalArray::IteratorState();
33 #ifndef JSON_VALUE_USE_INTERNAL_MAP
34 ValueIteratorBase::ValueIteratorBase( const Value::ObjectValues::iterator ¤t )
40 ValueIteratorBase::ValueIteratorBase( const ValueInternalArray::IteratorState &state )
43 iterator_.array_ = state;
47 ValueIteratorBase::ValueIteratorBase( const ValueInternalMap::IteratorState &state )
50 iterator_.map_ = state;
55 ValueIteratorBase::deref() const
57 #ifndef JSON_VALUE_USE_INTERNAL_MAP
58 return current_->second;
61 return ValueInternalArray::dereference( iterator_.array_ );
62 return ValueInternalMap::value( iterator_.map_ );
68 ValueIteratorBase::increment()
70 #ifndef JSON_VALUE_USE_INTERNAL_MAP
74 ValueInternalArray::increment( iterator_.array_ );
75 ValueInternalMap::increment( iterator_.map_ );
81 ValueIteratorBase::decrement()
83 #ifndef JSON_VALUE_USE_INTERNAL_MAP
87 ValueInternalArray::decrement( iterator_.array_ );
88 ValueInternalMap::decrement( iterator_.map_ );
93 ValueIteratorBase::difference_type
94 ValueIteratorBase::computeDistance( const SelfType &other ) const
96 #ifndef JSON_VALUE_USE_INTERNAL_MAP
97 # ifdef JSON_USE_CPPTL_SMALLMAP
98 return current_ - other.current_;
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_ )
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 )
124 return ValueInternalArray::distance( iterator_.array_, other.iterator_.array_ );
125 return ValueInternalMap::distance( iterator_.map_, other.iterator_.map_ );
131 ValueIteratorBase::isEqual( const SelfType &other ) const
133 #ifndef JSON_VALUE_USE_INTERNAL_MAP
136 return other.isNull_;
138 return current_ == other.current_;
141 return ValueInternalArray::equals( iterator_.array_, other.iterator_.array_ );
142 return ValueInternalMap::equals( iterator_.map_, other.iterator_.map_ );
148 ValueIteratorBase::copy( const SelfType &other )
150 #ifndef JSON_VALUE_USE_INTERNAL_MAP
151 current_ = other.current_;
154 iterator_.array_ = other.iterator_.array_;
155 iterator_.map_ = other.iterator_.map_;
161 ValueIteratorBase::key() const
163 #ifndef JSON_VALUE_USE_INTERNAL_MAP
164 const Value::CZString czstring = (*current_).first;
165 if ( czstring.c_str() )
167 if ( czstring.isStaticString() )
168 return Value( StaticString( czstring.c_str() ) );
169 return Value( czstring.c_str() );
171 return Value( czstring.index() );
174 return Value( ValueInternalArray::indexOf( iterator_.array_ ) );
176 const char *memberName = ValueInternalMap::key( iterator_.map_, isStatic );
178 return Value( StaticString( memberName ) );
179 return Value( memberName );
185 ValueIteratorBase::index() const
187 #ifndef JSON_VALUE_USE_INTERNAL_MAP
188 const Value::CZString czstring = (*current_).first;
189 if ( !czstring.c_str() )
190 return czstring.index();
191 return Value::UInt( -1 );
194 return Value::UInt( ValueInternalArray::indexOf( iterator_.array_ ) );
195 return Value::UInt( -1 );
201 ValueIteratorBase::memberName() const
203 #ifndef JSON_VALUE_USE_INTERNAL_MAP
204 const char *name = (*current_).first.c_str();
205 return name ? name : "";
208 return ValueInternalMap::key( iterator_.map_ );
214 // //////////////////////////////////////////////////////////////////
215 // //////////////////////////////////////////////////////////////////
216 // //////////////////////////////////////////////////////////////////
217 // class ValueConstIterator
218 // //////////////////////////////////////////////////////////////////
219 // //////////////////////////////////////////////////////////////////
220 // //////////////////////////////////////////////////////////////////
222 ValueConstIterator::ValueConstIterator()
227 #ifndef JSON_VALUE_USE_INTERNAL_MAP
228 ValueConstIterator::ValueConstIterator( const Value::ObjectValues::iterator ¤t )
229 : ValueIteratorBase( current )
233 ValueConstIterator::ValueConstIterator( const ValueInternalArray::IteratorState &state )
234 : ValueIteratorBase( state )
238 ValueConstIterator::ValueConstIterator( const ValueInternalMap::IteratorState &state )
239 : ValueIteratorBase( state )
245 ValueConstIterator::operator =( const ValueIteratorBase &other )
252 // //////////////////////////////////////////////////////////////////
253 // //////////////////////////////////////////////////////////////////
254 // //////////////////////////////////////////////////////////////////
255 // class ValueIterator
256 // //////////////////////////////////////////////////////////////////
257 // //////////////////////////////////////////////////////////////////
258 // //////////////////////////////////////////////////////////////////
260 ValueIterator::ValueIterator()
265 #ifndef JSON_VALUE_USE_INTERNAL_MAP
266 ValueIterator::ValueIterator( const Value::ObjectValues::iterator ¤t )
267 : ValueIteratorBase( current )
271 ValueIterator::ValueIterator( const ValueInternalArray::IteratorState &state )
272 : ValueIteratorBase( state )
276 ValueIterator::ValueIterator( const ValueInternalMap::IteratorState &state )
277 : ValueIteratorBase( state )
282 ValueIterator::ValueIterator( const ValueConstIterator &other )
283 : ValueIteratorBase( other )
287 ValueIterator::ValueIterator( const ValueIterator &other )
288 : ValueIteratorBase( other )
293 ValueIterator::operator =( const SelfType &other )