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 8namespace Json { 9 10// ////////////////////////////////////////////////////////////////// 11// ////////////////////////////////////////////////////////////////// 12// ////////////////////////////////////////////////////////////////// 13// class ValueIteratorBase 14// ////////////////////////////////////////////////////////////////// 15// ////////////////////////////////////////////////////////////////// 16// ////////////////////////////////////////////////////////////////// 17 18ValueIteratorBase::ValueIteratorBase() 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 29ValueIteratorBase::ValueIteratorBase( 30 const Value::ObjectValues::iterator& current) 31 : current_(current), isNull_(false) {} 32#else 33ValueIteratorBase::ValueIteratorBase( 34 const ValueInternalArray::IteratorState& state) 35 : isArray_(true) { 36 iterator_.array_ = state; 37} 38 39ValueIteratorBase::ValueIteratorBase( 40 const ValueInternalMap::IteratorState& state) 41 : isArray_(false) { 42 iterator_.map_ = state; 43} 44#endif 45 46Value& ValueIteratorBase::deref() const { 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 56void ValueIteratorBase::increment() { 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 66void ValueIteratorBase::decrement() { 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 76ValueIteratorBase::difference_type 77ValueIteratorBase::computeDistance(const SelfType& other) const { 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 111bool 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 124void 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 135Value ValueIteratorBase::key() const { 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 155UInt ValueIteratorBase::index() const { 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 168const 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 187ValueConstIterator::ValueConstIterator() {} 188 189#ifndef JSON_VALUE_USE_INTERNAL_MAP 190ValueConstIterator::ValueConstIterator( 191 const Value::ObjectValues::iterator& current) 192 : ValueIteratorBase(current) {} 193#else 194ValueConstIterator::ValueConstIterator( 195 const ValueInternalArray::IteratorState& state) 196 : ValueIteratorBase(state) {} 197 198ValueConstIterator::ValueConstIterator( 199 const ValueInternalMap::IteratorState& state) 200 : ValueIteratorBase(state) {} 201#endif 202 203ValueConstIterator& ValueConstIterator:: 204operator=(const ValueIteratorBase& other) { 205 copy(other); 206 return *this; 207} 208 209// ////////////////////////////////////////////////////////////////// 210// ////////////////////////////////////////////////////////////////// 211// ////////////////////////////////////////////////////////////////// 212// class ValueIterator 213// ////////////////////////////////////////////////////////////////// 214// ////////////////////////////////////////////////////////////////// 215// ////////////////////////////////////////////////////////////////// 216 217ValueIterator::ValueIterator() {} 218 219#ifndef JSON_VALUE_USE_INTERNAL_MAP 220ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current) 221 : ValueIteratorBase(current) {} 222#else 223ValueIterator::ValueIterator(const ValueInternalArray::IteratorState& state) 224 : ValueIteratorBase(state) {} 225 226ValueIterator::ValueIterator(const ValueInternalMap::IteratorState& state) 227 : ValueIteratorBase(state) {} 228#endif 229 230ValueIterator::ValueIterator(const ValueConstIterator& other) 231 : ValueIteratorBase(other) {} 232 233ValueIterator::ValueIterator(const ValueIterator& other) 234 : ValueIteratorBase(other) {} 235 236ValueIterator& ValueIterator::operator=(const SelfType& other) { 237 copy(other); 238 return *this; 239} 240 241} // namespace Json 242