JsonCpp project page JsonCpp home page

json_valueiterator.inl

Go to the documentation of this file.
00001 // Copyright 2007-2010 Baptiste Lepilleur
00002 // Distributed under MIT license, or public domain if desired and
00003 // recognized in your jurisdiction.
00004 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
00005 
00006 // included by json_value.cpp
00007 
00008 namespace Json {
00009 
00010 // //////////////////////////////////////////////////////////////////
00011 // //////////////////////////////////////////////////////////////////
00012 // //////////////////////////////////////////////////////////////////
00013 // class ValueIteratorBase
00014 // //////////////////////////////////////////////////////////////////
00015 // //////////////////////////////////////////////////////////////////
00016 // //////////////////////////////////////////////////////////////////
00017 
00018 ValueIteratorBase::ValueIteratorBase()
00019 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00020    : current_()
00021    , isNull_( true )
00022 {
00023 }
00024 #else
00025    : isArray_( true )
00026    , isNull_( true )
00027 {
00028    iterator_.array_ = ValueInternalArray::IteratorState();
00029 }
00030 #endif
00031 
00032 
00033 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00034 ValueIteratorBase::ValueIteratorBase( const Value::ObjectValues::iterator &current )
00035    : current_( current )
00036    , isNull_( false )
00037 {
00038 }
00039 #else
00040 ValueIteratorBase::ValueIteratorBase( const ValueInternalArray::IteratorState &state )
00041    : isArray_( true )
00042 {
00043    iterator_.array_ = state;
00044 }
00045 
00046 
00047 ValueIteratorBase::ValueIteratorBase( const ValueInternalMap::IteratorState &state )
00048    : isArray_( false )
00049 {
00050    iterator_.map_ = state;
00051 }
00052 #endif
00053 
00054 Value &
00055 ValueIteratorBase::deref() const
00056 {
00057 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00058    return current_->second;
00059 #else
00060    if ( isArray_ )
00061       return ValueInternalArray::dereference( iterator_.array_ );
00062    return ValueInternalMap::value( iterator_.map_ );
00063 #endif
00064 }
00065 
00066 
00067 void 
00068 ValueIteratorBase::increment()
00069 {
00070 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00071    ++current_;
00072 #else
00073    if ( isArray_ )
00074       ValueInternalArray::increment( iterator_.array_ );
00075    ValueInternalMap::increment( iterator_.map_ );
00076 #endif
00077 }
00078 
00079 
00080 void 
00081 ValueIteratorBase::decrement()
00082 {
00083 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00084    --current_;
00085 #else
00086    if ( isArray_ )
00087       ValueInternalArray::decrement( iterator_.array_ );
00088    ValueInternalMap::decrement( iterator_.map_ );
00089 #endif
00090 }
00091 
00092 
00093 ValueIteratorBase::difference_type 
00094 ValueIteratorBase::computeDistance( const SelfType &other ) const
00095 {
00096 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00097 # ifdef JSON_USE_CPPTL_SMALLMAP
00098    return current_ - other.current_;
00099 # else
00100    // Iterator for null value are initialized using the default
00101    // constructor, which initialize current_ to the default
00102    // std::map::iterator. As begin() and end() are two instance 
00103    // of the default std::map::iterator, they can not be compared.
00104    // To allow this, we handle this comparison specifically.
00105    if ( isNull_  &&  other.isNull_ )
00106    {
00107       return 0;
00108    }
00109 
00110 
00111    // Usage of std::distance is not portable (does not compile with Sun Studio 12 RogueWave STL,
00112    // which is the one used by default).
00113    // Using a portable hand-made version for non random iterator instead:
00114    //   return difference_type( std::distance( current_, other.current_ ) );
00115    difference_type myDistance = 0;
00116    for ( Value::ObjectValues::iterator it = current_; it != other.current_; ++it )
00117    {
00118       ++myDistance;
00119    }
00120    return myDistance;
00121 # endif
00122 #else
00123    if ( isArray_ )
00124       return ValueInternalArray::distance( iterator_.array_, other.iterator_.array_ );
00125    return ValueInternalMap::distance( iterator_.map_, other.iterator_.map_ );
00126 #endif
00127 }
00128 
00129 
00130 bool 
00131 ValueIteratorBase::isEqual( const SelfType &other ) const
00132 {
00133 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00134    if ( isNull_ )
00135    {
00136       return other.isNull_;
00137    }
00138    return current_ == other.current_;
00139 #else
00140    if ( isArray_ )
00141       return ValueInternalArray::equals( iterator_.array_, other.iterator_.array_ );
00142    return ValueInternalMap::equals( iterator_.map_, other.iterator_.map_ );
00143 #endif
00144 }
00145 
00146 
00147 void 
00148 ValueIteratorBase::copy( const SelfType &other )
00149 {
00150 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00151    current_ = other.current_;
00152 #else
00153    if ( isArray_ )
00154       iterator_.array_ = other.iterator_.array_;
00155    iterator_.map_ = other.iterator_.map_;
00156 #endif
00157 }
00158 
00159 
00160 Value 
00161 ValueIteratorBase::key() const
00162 {
00163 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00164    const Value::CZString czstring = (*current_).first;
00165    if ( czstring.c_str() )
00166    {
00167       if ( czstring.isStaticString() )
00168          return Value( StaticString( czstring.c_str() ) );
00169       return Value( czstring.c_str() );
00170    }
00171    return Value( czstring.index() );
00172 #else
00173    if ( isArray_ )
00174       return Value( ValueInternalArray::indexOf( iterator_.array_ ) );
00175    bool isStatic;
00176    const char *memberName = ValueInternalMap::key( iterator_.map_, isStatic );
00177    if ( isStatic )
00178       return Value( StaticString( memberName ) );
00179    return Value( memberName );
00180 #endif
00181 }
00182 
00183 
00184 UInt 
00185 ValueIteratorBase::index() const
00186 {
00187 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00188    const Value::CZString czstring = (*current_).first;
00189    if ( !czstring.c_str() )
00190       return czstring.index();
00191    return Value::UInt( -1 );
00192 #else
00193    if ( isArray_ )
00194       return Value::UInt( ValueInternalArray::indexOf( iterator_.array_ ) );
00195    return Value::UInt( -1 );
00196 #endif
00197 }
00198 
00199 
00200 const char *
00201 ValueIteratorBase::memberName() const
00202 {
00203 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00204    const char *name = (*current_).first.c_str();
00205    return name ? name : "";
00206 #else
00207    if ( !isArray_ )
00208       return ValueInternalMap::key( iterator_.map_ );
00209    return "";
00210 #endif
00211 }
00212 
00213 
00214 // //////////////////////////////////////////////////////////////////
00215 // //////////////////////////////////////////////////////////////////
00216 // //////////////////////////////////////////////////////////////////
00217 // class ValueConstIterator
00218 // //////////////////////////////////////////////////////////////////
00219 // //////////////////////////////////////////////////////////////////
00220 // //////////////////////////////////////////////////////////////////
00221 
00222 ValueConstIterator::ValueConstIterator()
00223 {
00224 }
00225 
00226 
00227 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00228 ValueConstIterator::ValueConstIterator( const Value::ObjectValues::iterator &current )
00229    : ValueIteratorBase( current )
00230 {
00231 }
00232 #else
00233 ValueConstIterator::ValueConstIterator( const ValueInternalArray::IteratorState &state )
00234    : ValueIteratorBase( state )
00235 {
00236 }
00237 
00238 ValueConstIterator::ValueConstIterator( const ValueInternalMap::IteratorState &state )
00239    : ValueIteratorBase( state )
00240 {
00241 }
00242 #endif
00243 
00244 ValueConstIterator &
00245 ValueConstIterator::operator =( const ValueIteratorBase &other )
00246 {
00247    copy( other );
00248    return *this;
00249 }
00250 
00251 
00252 // //////////////////////////////////////////////////////////////////
00253 // //////////////////////////////////////////////////////////////////
00254 // //////////////////////////////////////////////////////////////////
00255 // class ValueIterator
00256 // //////////////////////////////////////////////////////////////////
00257 // //////////////////////////////////////////////////////////////////
00258 // //////////////////////////////////////////////////////////////////
00259 
00260 ValueIterator::ValueIterator()
00261 {
00262 }
00263 
00264 
00265 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00266 ValueIterator::ValueIterator( const Value::ObjectValues::iterator &current )
00267    : ValueIteratorBase( current )
00268 {
00269 }
00270 #else
00271 ValueIterator::ValueIterator( const ValueInternalArray::IteratorState &state )
00272    : ValueIteratorBase( state )
00273 {
00274 }
00275 
00276 ValueIterator::ValueIterator( const ValueInternalMap::IteratorState &state )
00277    : ValueIteratorBase( state )
00278 {
00279 }
00280 #endif
00281 
00282 ValueIterator::ValueIterator( const ValueConstIterator &other )
00283    : ValueIteratorBase( other )
00284 {
00285 }
00286 
00287 ValueIterator::ValueIterator( const ValueIterator &other )
00288    : ValueIteratorBase( other )
00289 {
00290 }
00291 
00292 ValueIterator &
00293 ValueIterator::operator =( const SelfType &other )
00294 {
00295    copy( other );
00296    return *this;
00297 }
00298 
00299 } // namespace Json

SourceForge Logo hosts this site. Send comments to:
Json-cpp Developers