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