You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.1 KiB

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