json_valueiterator.inl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // included by json_value.cpp
  6. namespace Json {
  7. // //////////////////////////////////////////////////////////////////
  8. // //////////////////////////////////////////////////////////////////
  9. // //////////////////////////////////////////////////////////////////
  10. // class ValueIteratorBase
  11. // //////////////////////////////////////////////////////////////////
  12. // //////////////////////////////////////////////////////////////////
  13. // //////////////////////////////////////////////////////////////////
  14. ValueIteratorBase::ValueIteratorBase()
  15. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  16. : current_()
  17. , isNull_( true )
  18. {
  19. }
  20. #else
  21. : isArray_( true )
  22. , isNull_( true )
  23. {
  24. iterator_.array_ = ValueInternalArray::IteratorState();
  25. }
  26. #endif
  27. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  28. ValueIteratorBase::ValueIteratorBase( const Value::ObjectValues::iterator &current )
  29. : current_( current )
  30. , isNull_( false )
  31. {
  32. }
  33. #else
  34. ValueIteratorBase::ValueIteratorBase( const ValueInternalArray::IteratorState &state )
  35. : isArray_( true )
  36. {
  37. iterator_.array_ = state;
  38. }
  39. ValueIteratorBase::ValueIteratorBase( const ValueInternalMap::IteratorState &state )
  40. : isArray_( false )
  41. {
  42. iterator_.map_ = state;
  43. }
  44. #endif
  45. Value &
  46. ValueIteratorBase::deref() const
  47. {
  48. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  49. return current_->second;
  50. #else
  51. if ( isArray_ )
  52. return ValueInternalArray::dereference( iterator_.array_ );
  53. return ValueInternalMap::value( iterator_.map_ );
  54. #endif
  55. }
  56. void
  57. ValueIteratorBase::increment()
  58. {
  59. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  60. ++current_;
  61. #else
  62. if ( isArray_ )
  63. ValueInternalArray::increment( iterator_.array_ );
  64. ValueInternalMap::increment( iterator_.map_ );
  65. #endif
  66. }
  67. void
  68. ValueIteratorBase::decrement()
  69. {
  70. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  71. --current_;
  72. #else
  73. if ( isArray_ )
  74. ValueInternalArray::decrement( iterator_.array_ );
  75. ValueInternalMap::decrement( iterator_.map_ );
  76. #endif
  77. }
  78. ValueIteratorBase::difference_type
  79. ValueIteratorBase::computeDistance( const SelfType &other ) const
  80. {
  81. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  82. # ifdef JSON_USE_CPPTL_SMALLMAP
  83. return current_ - other.current_;
  84. # else
  85. // Iterator for null value are initialized using the default
  86. // constructor, which initialize current_ to the default
  87. // std::map::iterator. As begin() and end() are two instance
  88. // of the default std::map::iterator, they can not be compared.
  89. // To allow this, we handle this comparison specifically.
  90. if ( isNull_ && other.isNull_ )
  91. {
  92. return 0;
  93. }
  94. // Usage of std::distance is not portable (does not compile with Sun Studio 12 RogueWave STL,
  95. // which is the one used by default).
  96. // Using a portable hand-made version for non random iterator instead:
  97. // return difference_type( std::distance( current_, other.current_ ) );
  98. difference_type myDistance = 0;
  99. for ( Value::ObjectValues::iterator it = current_; it != other.current_; ++it )
  100. {
  101. ++myDistance;
  102. }
  103. return myDistance;
  104. # endif
  105. #else
  106. if ( isArray_ )
  107. return ValueInternalArray::distance( iterator_.array_, other.iterator_.array_ );
  108. return ValueInternalMap::distance( iterator_.map_, other.iterator_.map_ );
  109. #endif
  110. }
  111. bool
  112. ValueIteratorBase::isEqual( const SelfType &other ) const
  113. {
  114. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  115. if ( isNull_ )
  116. {
  117. return other.isNull_;
  118. }
  119. return current_ == other.current_;
  120. #else
  121. if ( isArray_ )
  122. return ValueInternalArray::equals( iterator_.array_, other.iterator_.array_ );
  123. return ValueInternalMap::equals( iterator_.map_, other.iterator_.map_ );
  124. #endif
  125. }
  126. void
  127. ValueIteratorBase::copy( const SelfType &other )
  128. {
  129. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  130. current_ = other.current_;
  131. #else
  132. if ( isArray_ )
  133. iterator_.array_ = other.iterator_.array_;
  134. iterator_.map_ = other.iterator_.map_;
  135. #endif
  136. }
  137. Value
  138. ValueIteratorBase::key() const
  139. {
  140. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  141. const Value::CZString czstring = (*current_).first;
  142. if ( czstring.c_str() )
  143. {
  144. if ( czstring.isStaticString() )
  145. return Value( StaticString( czstring.c_str() ) );
  146. return Value( czstring.c_str() );
  147. }
  148. return Value( czstring.index() );
  149. #else
  150. if ( isArray_ )
  151. return Value( ValueInternalArray::indexOf( iterator_.array_ ) );
  152. bool isStatic;
  153. const char *memberName = ValueInternalMap::key( iterator_.map_, isStatic );
  154. if ( isStatic )
  155. return Value( StaticString( memberName ) );
  156. return Value( memberName );
  157. #endif
  158. }
  159. UInt
  160. ValueIteratorBase::index() const
  161. {
  162. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  163. const Value::CZString czstring = (*current_).first;
  164. if ( !czstring.c_str() )
  165. return czstring.index();
  166. return Value::UInt( -1 );
  167. #else
  168. if ( isArray_ )
  169. return Value::UInt( ValueInternalArray::indexOf( iterator_.array_ ) );
  170. return Value::UInt( -1 );
  171. #endif
  172. }
  173. const char *
  174. ValueIteratorBase::memberName() const
  175. {
  176. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  177. const char *name = (*current_).first.c_str();
  178. return name ? name : "";
  179. #else
  180. if ( !isArray_ )
  181. return ValueInternalMap::key( iterator_.map_ );
  182. return "";
  183. #endif
  184. }
  185. // //////////////////////////////////////////////////////////////////
  186. // //////////////////////////////////////////////////////////////////
  187. // //////////////////////////////////////////////////////////////////
  188. // class ValueConstIterator
  189. // //////////////////////////////////////////////////////////////////
  190. // //////////////////////////////////////////////////////////////////
  191. // //////////////////////////////////////////////////////////////////
  192. ValueConstIterator::ValueConstIterator()
  193. {
  194. }
  195. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  196. ValueConstIterator::ValueConstIterator( const Value::ObjectValues::iterator &current )
  197. : ValueIteratorBase( current )
  198. {
  199. }
  200. #else
  201. ValueConstIterator::ValueConstIterator( const ValueInternalArray::IteratorState &state )
  202. : ValueIteratorBase( state )
  203. {
  204. }
  205. ValueConstIterator::ValueConstIterator( const ValueInternalMap::IteratorState &state )
  206. : ValueIteratorBase( state )
  207. {
  208. }
  209. #endif
  210. ValueConstIterator &
  211. ValueConstIterator::operator =( const ValueIteratorBase &other )
  212. {
  213. copy( other );
  214. return *this;
  215. }
  216. // //////////////////////////////////////////////////////////////////
  217. // //////////////////////////////////////////////////////////////////
  218. // //////////////////////////////////////////////////////////////////
  219. // class ValueIterator
  220. // //////////////////////////////////////////////////////////////////
  221. // //////////////////////////////////////////////////////////////////
  222. // //////////////////////////////////////////////////////////////////
  223. ValueIterator::ValueIterator()
  224. {
  225. }
  226. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  227. ValueIterator::ValueIterator( const Value::ObjectValues::iterator &current )
  228. : ValueIteratorBase( current )
  229. {
  230. }
  231. #else
  232. ValueIterator::ValueIterator( const ValueInternalArray::IteratorState &state )
  233. : ValueIteratorBase( state )
  234. {
  235. }
  236. ValueIterator::ValueIterator( const ValueInternalMap::IteratorState &state )
  237. : ValueIteratorBase( state )
  238. {
  239. }
  240. #endif
  241. ValueIterator::ValueIterator( const ValueConstIterator &other )
  242. : ValueIteratorBase( other )
  243. {
  244. }
  245. ValueIterator::ValueIterator( const ValueIterator &other )
  246. : ValueIteratorBase( other )
  247. {
  248. }
  249. ValueIterator &
  250. ValueIterator::operator =( const SelfType &other )
  251. {
  252. copy( other );
  253. return *this;
  254. }
  255. } // namespace Json