json_valueiterator.inl 5.1 KB

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