JsonCpp project page JsonCpp home page

json_valueiterator.inl

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

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