Drizzled Public API Documentation

json_valueiterator.inl
00001 /*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00002  * 
00003  *  JSON Library, originally from http://jsoncpp.sourceforge.net/
00004  *
00005  *  Copyright (C) 2011 Stewart Smith
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions are
00010  *  met:
00011  *
00012  *      * Redistributions of source code must retain the above copyright
00013  *  notice, this list of conditions and the following disclaimer.
00014  *
00015  *      * Redistributions in binary form must reproduce the above
00016  *  copyright notice, this list of conditions and the following disclaimer
00017  *  in the documentation and/or other materials provided with the
00018  *  distribution.
00019  *
00020  *      * The names of its contributors may not be used to endorse or
00021  *  promote products derived from this software without specific prior
00022  *  written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00027  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00028  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00029  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00030  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00031  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00032  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  */
00037 
00038 #pragma once
00039 // included by json_value.cpp
00040 // everything is within Json namespace
00041 
00042 
00043 // //////////////////////////////////////////////////////////////////
00044 // //////////////////////////////////////////////////////////////////
00045 // //////////////////////////////////////////////////////////////////
00046 // class ValueIteratorBase
00047 // //////////////////////////////////////////////////////////////////
00048 // //////////////////////////////////////////////////////////////////
00049 // //////////////////////////////////////////////////////////////////
00050 
00051 ValueIteratorBase::ValueIteratorBase()
00052 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00053    : current_()
00054    , isNull_( true )
00055 {
00056 }
00057 #else
00058    : isArray_( true )
00059    , isNull_( true )
00060 {
00061    iterator_.array_ = ValueInternalArray::IteratorState();
00062 }
00063 #endif
00064 
00065 
00066 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00067 ValueIteratorBase::ValueIteratorBase( const Value::ObjectValues::iterator &current )
00068    : current_( current )
00069    , isNull_( false )
00070 {
00071 }
00072 #else
00073 ValueIteratorBase::ValueIteratorBase( const ValueInternalArray::IteratorState &state )
00074    : isArray_( true )
00075 {
00076    iterator_.array_ = state;
00077 }
00078 
00079 
00080 ValueIteratorBase::ValueIteratorBase( const ValueInternalMap::IteratorState &state )
00081    : isArray_( false )
00082 {
00083    iterator_.map_ = state;
00084 }
00085 #endif
00086 
00087 Value &
00088 ValueIteratorBase::deref() const
00089 {
00090 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00091    return current_->second;
00092 #else
00093    if ( isArray_ )
00094       return ValueInternalArray::dereference( iterator_.array_ );
00095    return ValueInternalMap::value( iterator_.map_ );
00096 #endif
00097 }
00098 
00099 
00100 void 
00101 ValueIteratorBase::increment()
00102 {
00103 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00104    ++current_;
00105 #else
00106    if ( isArray_ )
00107       ValueInternalArray::increment( iterator_.array_ );
00108    ValueInternalMap::increment( iterator_.map_ );
00109 #endif
00110 }
00111 
00112 
00113 void 
00114 ValueIteratorBase::decrement()
00115 {
00116 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00117    --current_;
00118 #else
00119    if ( isArray_ )
00120       ValueInternalArray::decrement( iterator_.array_ );
00121    ValueInternalMap::decrement( iterator_.map_ );
00122 #endif
00123 }
00124 
00125 
00126 ValueIteratorBase::difference_type 
00127 ValueIteratorBase::computeDistance( const SelfType &other ) const
00128 {
00129 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00130 # ifdef JSON_USE_CPPTL_SMALLMAP
00131    return current_ - other.current_;
00132 # else
00133    // Iterator for null value are initialized using the default
00134    // constructor, which initialize current_ to the default
00135    // std::map::iterator. As begin() and end() are two instance 
00136    // of the default std::map::iterator, they can not be compared.
00137    // To allow this, we handle this comparison specifically.
00138    if ( isNull_  &&  other.isNull_ )
00139    {
00140       return 0;
00141    }
00142 
00143 
00144    // Usage of std::distance is not portable (does not compile with Sun Studio 12 RogueWave STL,
00145    // which is the one used by default).
00146    // Using a portable hand-made version for non random iterator instead:
00147    //   return difference_type( std::distance( current_, other.current_ ) );
00148    difference_type myDistance = 0;
00149    for ( Value::ObjectValues::iterator it = current_; it != other.current_; ++it )
00150    {
00151       ++myDistance;
00152    }
00153    return myDistance;
00154 # endif
00155 #else
00156    if ( isArray_ )
00157       return ValueInternalArray::distance( iterator_.array_, other.iterator_.array_ );
00158    return ValueInternalMap::distance( iterator_.map_, other.iterator_.map_ );
00159 #endif
00160 }
00161 
00162 
00163 bool 
00164 ValueIteratorBase::isEqual( const SelfType &other ) const
00165 {
00166 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00167    if ( isNull_ )
00168    {
00169       return other.isNull_;
00170    }
00171    return current_ == other.current_;
00172 #else
00173    if ( isArray_ )
00174       return ValueInternalArray::equals( iterator_.array_, other.iterator_.array_ );
00175    return ValueInternalMap::equals( iterator_.map_, other.iterator_.map_ );
00176 #endif
00177 }
00178 
00179 
00180 void 
00181 ValueIteratorBase::copy( const SelfType &other )
00182 {
00183 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00184    current_ = other.current_;
00185 #else
00186    if ( isArray_ )
00187       iterator_.array_ = other.iterator_.array_;
00188    iterator_.map_ = other.iterator_.map_;
00189 #endif
00190 }
00191 
00192 
00193 Value 
00194 ValueIteratorBase::key() const
00195 {
00196 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00197    const Value::CZString czstring = (*current_).first;
00198    if ( czstring.c_str() )
00199    {
00200       if ( czstring.isStaticString() )
00201          return Value( StaticString( czstring.c_str() ) );
00202       return Value( czstring.c_str() );
00203    }
00204    return Value( czstring.index() );
00205 #else
00206    if ( isArray_ )
00207       return Value( ValueInternalArray::indexOf( iterator_.array_ ) );
00208    bool isStatic;
00209    const char *memberName = ValueInternalMap::key( iterator_.map_, isStatic );
00210    if ( isStatic )
00211       return Value( StaticString( memberName ) );
00212    return Value( memberName );
00213 #endif
00214 }
00215 
00216 
00217 UInt 
00218 ValueIteratorBase::index() const
00219 {
00220 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00221    const Value::CZString czstring = (*current_).first;
00222    if ( !czstring.c_str() )
00223       return czstring.index();
00224    return Value::UInt( -1 );
00225 #else
00226    if ( isArray_ )
00227       return Value::UInt( ValueInternalArray::indexOf( iterator_.array_ ) );
00228    return Value::UInt( -1 );
00229 #endif
00230 }
00231 
00232 
00233 const char *
00234 ValueIteratorBase::memberName() const
00235 {
00236 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00237    const char *name = (*current_).first.c_str();
00238    return name ? name : "";
00239 #else
00240    if ( !isArray_ )
00241       return ValueInternalMap::key( iterator_.map_ );
00242    return "";
00243 #endif
00244 }
00245 
00246 
00247 // //////////////////////////////////////////////////////////////////
00248 // //////////////////////////////////////////////////////////////////
00249 // //////////////////////////////////////////////////////////////////
00250 // class ValueConstIterator
00251 // //////////////////////////////////////////////////////////////////
00252 // //////////////////////////////////////////////////////////////////
00253 // //////////////////////////////////////////////////////////////////
00254 
00255 ValueConstIterator::ValueConstIterator()
00256 {
00257 }
00258 
00259 
00260 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00261 ValueConstIterator::ValueConstIterator( const Value::ObjectValues::iterator &current )
00262    : ValueIteratorBase( current )
00263 {
00264 }
00265 #else
00266 ValueConstIterator::ValueConstIterator( const ValueInternalArray::IteratorState &state )
00267    : ValueIteratorBase( state )
00268 {
00269 }
00270 
00271 ValueConstIterator::ValueConstIterator( const ValueInternalMap::IteratorState &state )
00272    : ValueIteratorBase( state )
00273 {
00274 }
00275 #endif
00276 
00277 ValueConstIterator &
00278 ValueConstIterator::operator =( const ValueIteratorBase &other )
00279 {
00280    copy( other );
00281    return *this;
00282 }
00283 
00284 
00285 // //////////////////////////////////////////////////////////////////
00286 // //////////////////////////////////////////////////////////////////
00287 // //////////////////////////////////////////////////////////////////
00288 // class ValueIterator
00289 // //////////////////////////////////////////////////////////////////
00290 // //////////////////////////////////////////////////////////////////
00291 // //////////////////////////////////////////////////////////////////
00292 
00293 ValueIterator::ValueIterator()
00294 {
00295 }
00296 
00297 
00298 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00299 ValueIterator::ValueIterator( const Value::ObjectValues::iterator &current )
00300    : ValueIteratorBase( current )
00301 {
00302 }
00303 #else
00304 ValueIterator::ValueIterator( const ValueInternalArray::IteratorState &state )
00305    : ValueIteratorBase( state )
00306 {
00307 }
00308 
00309 ValueIterator::ValueIterator( const ValueInternalMap::IteratorState &state )
00310    : ValueIteratorBase( state )
00311 {
00312 }
00313 #endif
00314 
00315 ValueIterator::ValueIterator( const ValueConstIterator &other )
00316    : ValueIteratorBase( other )
00317 {
00318 }
00319 
00320 ValueIterator::ValueIterator( const ValueIterator &other )
00321    : ValueIteratorBase( other )
00322 {
00323 }
00324 
00325 ValueIterator &
00326 ValueIterator::operator =( const SelfType &other )
00327 {
00328    copy( other );
00329    return *this;
00330 }