json_valueiterator.inl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
  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. : current_(), isNull_(true) {
  16. }
  17. ValueIteratorBase::ValueIteratorBase(
  18. const Value::ObjectValues::iterator& current)
  19. : current_(current), isNull_(false) {}
  20. Value& ValueIteratorBase::deref() const {
  21. return current_->second;
  22. }
  23. void ValueIteratorBase::increment() {
  24. ++current_;
  25. }
  26. void ValueIteratorBase::decrement() {
  27. --current_;
  28. }
  29. ValueIteratorBase::difference_type
  30. ValueIteratorBase::computeDistance(const SelfType& other) const {
  31. #ifdef JSON_USE_CPPTL_SMALLMAP
  32. return other.current_ - current_;
  33. #else
  34. // Iterator for null value are initialized using the default
  35. // constructor, which initialize current_ to the default
  36. // std::map::iterator. As begin() and end() are two instance
  37. // of the default std::map::iterator, they can not be compared.
  38. // To allow this, we handle this comparison specifically.
  39. if (isNull_ && other.isNull_) {
  40. return 0;
  41. }
  42. // Usage of std::distance is not portable (does not compile with Sun Studio 12
  43. // RogueWave STL,
  44. // which is the one used by default).
  45. // Using a portable hand-made version for non random iterator instead:
  46. // return difference_type( std::distance( current_, other.current_ ) );
  47. difference_type myDistance = 0;
  48. for (Value::ObjectValues::iterator it = current_; it != other.current_;
  49. ++it) {
  50. ++myDistance;
  51. }
  52. return myDistance;
  53. #endif
  54. }
  55. bool ValueIteratorBase::isEqual(const SelfType& other) const {
  56. if (isNull_) {
  57. return other.isNull_;
  58. }
  59. return current_ == other.current_;
  60. }
  61. void ValueIteratorBase::copy(const SelfType& other) {
  62. current_ = other.current_;
  63. isNull_ = other.isNull_;
  64. }
  65. Value ValueIteratorBase::key() const {
  66. const Value::CZString czstring = (*current_).first;
  67. if (czstring.data()) {
  68. if (czstring.isStaticString())
  69. return Value(StaticString(czstring.data()));
  70. return Value(czstring.data(), czstring.data() + czstring.length());
  71. }
  72. return Value(czstring.index());
  73. }
  74. UInt ValueIteratorBase::index() const {
  75. const Value::CZString czstring = (*current_).first;
  76. if (!czstring.data())
  77. return czstring.index();
  78. return Value::UInt(-1);
  79. }
  80. JSONCPP_STRING ValueIteratorBase::name() const {
  81. char const* keey;
  82. char const* end;
  83. keey = memberName(&end);
  84. if (!keey) return JSONCPP_STRING();
  85. return JSONCPP_STRING(keey, end);
  86. }
  87. char const* ValueIteratorBase::memberName() const {
  88. const char* cname = (*current_).first.data();
  89. return cname ? cname : "";
  90. }
  91. char const* ValueIteratorBase::memberName(char const** end) const {
  92. const char* cname = (*current_).first.data();
  93. if (!cname) {
  94. *end = NULL;
  95. return NULL;
  96. }
  97. *end = cname + (*current_).first.length();
  98. return cname;
  99. }
  100. // //////////////////////////////////////////////////////////////////
  101. // //////////////////////////////////////////////////////////////////
  102. // //////////////////////////////////////////////////////////////////
  103. // class ValueConstIterator
  104. // //////////////////////////////////////////////////////////////////
  105. // //////////////////////////////////////////////////////////////////
  106. // //////////////////////////////////////////////////////////////////
  107. ValueConstIterator::ValueConstIterator() {}
  108. ValueConstIterator::ValueConstIterator(
  109. const Value::ObjectValues::iterator& current)
  110. : ValueIteratorBase(current) {}
  111. ValueConstIterator::ValueConstIterator(ValueIterator const& other)
  112. : ValueIteratorBase(other) {}
  113. ValueConstIterator& ValueConstIterator::
  114. operator=(const ValueIteratorBase& other) {
  115. copy(other);
  116. return *this;
  117. }
  118. // //////////////////////////////////////////////////////////////////
  119. // //////////////////////////////////////////////////////////////////
  120. // //////////////////////////////////////////////////////////////////
  121. // class ValueIterator
  122. // //////////////////////////////////////////////////////////////////
  123. // //////////////////////////////////////////////////////////////////
  124. // //////////////////////////////////////////////////////////////////
  125. ValueIterator::ValueIterator() {}
  126. ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
  127. : ValueIteratorBase(current) {}
  128. ValueIterator::ValueIterator(const ValueConstIterator& other)
  129. : ValueIteratorBase(other) {
  130. throwRuntimeError("ConstIterator to Iterator should never be allowed.");
  131. }
  132. ValueIterator::ValueIterator(const ValueIterator& other)
  133. : ValueIteratorBase(other) {}
  134. ValueIterator& ValueIterator::operator=(const SelfType& other) {
  135. copy(other);
  136. return *this;
  137. }
  138. } // namespace Json