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 18 ValueIteratorBase::ValueIteratorBase() 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 ¤t ) 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 & 55 ValueIteratorBase::deref() const 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 68 ValueIteratorBase::increment() 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 81 ValueIteratorBase::decrement() 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 93 ValueIteratorBase::difference_type 94 ValueIteratorBase::computeDistance( const SelfType &other ) const 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 131 ValueIteratorBase::isEqual( const SelfType &other ) const 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 148 ValueIteratorBase::copy( const SelfType &other ) 149 { 150 #ifndef JSON_VALUE_USE_INTERNAL_MAP 151 current_ = other.current_; 152 #else 153 if ( isArray_ ) 154 iterator_.array_ = other.iterator_.array_; 155 iterator_.map_ = other.iterator_.map_; 156 #endif 157 } 158 159 160 Value 161 ValueIteratorBase::key() const 162 { 163 #ifndef JSON_VALUE_USE_INTERNAL_MAP 164 const Value::CZString czstring = (*current_).first; 165 if ( czstring.c_str() ) 166 { 167 if ( czstring.isStaticString() ) 168 return Value( StaticString( czstring.c_str() ) ); 169 return Value( czstring.c_str() ); 170 } 171 return Value( czstring.index() ); 172 #else 173 if ( isArray_ ) 174 return Value( ValueInternalArray::indexOf( iterator_.array_ ) ); 175 bool isStatic; 176 const char *memberName = ValueInternalMap::key( iterator_.map_, isStatic ); 177 if ( isStatic ) 178 return Value( StaticString( memberName ) ); 179 return Value( memberName ); 180 #endif 181 } 182 183 184 UInt 185 ValueIteratorBase::index() const 186 { 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 ); 192 #else 193 if ( isArray_ ) 194 return Value::UInt( ValueInternalArray::indexOf( iterator_.array_ ) ); 195 return Value::UInt( -1 ); 196 #endif 197 } 198 199 200 const char * 201 ValueIteratorBase::memberName() const 202 { 203 #ifndef JSON_VALUE_USE_INTERNAL_MAP 204 const char *name = (*current_).first.c_str(); 205 return name ? name : ""; 206 #else 207 if ( !isArray_ ) 208 return ValueInternalMap::key( iterator_.map_ ); 209 return ""; 210 #endif 211 } 212 213 214 // ////////////////////////////////////////////////////////////////// 215 // ////////////////////////////////////////////////////////////////// 216 // ////////////////////////////////////////////////////////////////// 217 // class ValueConstIterator 218 // ////////////////////////////////////////////////////////////////// 219 // ////////////////////////////////////////////////////////////////// 220 // ////////////////////////////////////////////////////////////////// 221 222 ValueConstIterator::ValueConstIterator() 223 { 224 } 225 226 227 #ifndef JSON_VALUE_USE_INTERNAL_MAP 228 ValueConstIterator::ValueConstIterator( const Value::ObjectValues::iterator ¤t ) 229 : ValueIteratorBase( current ) 230 { 231 } 232 #else 233 ValueConstIterator::ValueConstIterator( const ValueInternalArray::IteratorState &state ) 234 : ValueIteratorBase( state ) 235 { 236 } 237 238 ValueConstIterator::ValueConstIterator( const ValueInternalMap::IteratorState &state ) 239 : ValueIteratorBase( state ) 240 { 241 } 242 #endif 243 244 ValueConstIterator & 245 ValueConstIterator::operator =( const ValueIteratorBase &other ) 246 { 247 copy( other ); 248 return *this; 249 } 250 251 252 // ////////////////////////////////////////////////////////////////// 253 // ////////////////////////////////////////////////////////////////// 254 // ////////////////////////////////////////////////////////////////// 255 // class ValueIterator 256 // ////////////////////////////////////////////////////////////////// 257 // ////////////////////////////////////////////////////////////////// 258 // ////////////////////////////////////////////////////////////////// 259 260 ValueIterator::ValueIterator() 261 { 262 } 263 264 265 #ifndef JSON_VALUE_USE_INTERNAL_MAP 266 ValueIterator::ValueIterator( const Value::ObjectValues::iterator ¤t ) 267 : ValueIteratorBase( current ) 268 { 269 } 270 #else 271 ValueIterator::ValueIterator( const ValueInternalArray::IteratorState &state ) 272 : ValueIteratorBase( state ) 273 { 274 } 275 276 ValueIterator::ValueIterator( const ValueInternalMap::IteratorState &state ) 277 : ValueIteratorBase( state ) 278 { 279 } 280 #endif 281 282 ValueIterator::ValueIterator( const ValueConstIterator &other ) 283 : ValueIteratorBase( other ) 284 { 285 } 286 287 ValueIterator::ValueIterator( const ValueIterator &other ) 288 : ValueIteratorBase( other ) 289 { 290 } 291 292 ValueIterator & 293 ValueIterator::operator =( const SelfType &other ) 294 { 295 copy( other ); 296 return *this; 297 } 298 299 } // namespace Json 300