VarIterator.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // VarIterator.cpp
  3. //
  4. // $Id: //poco/Main/Data/src/VarIterator.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Dynamic
  8. // Module: VarIterator
  9. //
  10. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Dynamic/VarIterator.h"
  16. #include "Poco/Dynamic/Var.h"
  17. #include "Poco/Dynamic/Struct.h"
  18. #undef min
  19. #undef max
  20. #include <limits>
  21. namespace Poco {
  22. namespace Dynamic {
  23. const std::size_t VarIterator::POSITION_END = std::numeric_limits<std::size_t>::max();
  24. VarIterator::VarIterator(Var* pVar, bool positionEnd):
  25. _pVar(pVar),
  26. _position(positionEnd ? POSITION_END : 0)
  27. {
  28. }
  29. VarIterator::VarIterator(const VarIterator& other):
  30. _pVar(other._pVar),
  31. _position(other._position)
  32. {
  33. }
  34. VarIterator::~VarIterator()
  35. {
  36. }
  37. VarIterator& VarIterator::operator = (const VarIterator& other)
  38. {
  39. VarIterator tmp(other);
  40. swap(tmp);
  41. return *this;
  42. }
  43. void VarIterator::swap(VarIterator& other)
  44. {
  45. using std::swap;
  46. swap(_pVar, other._pVar);
  47. swap(_position, other._position);
  48. }
  49. void VarIterator::increment() const
  50. {
  51. if (POSITION_END == _position)
  52. throw RangeException("End of iterator reached.");
  53. if (_position < _pVar->size() - 1)
  54. ++_position;
  55. else
  56. _position = POSITION_END;
  57. }
  58. void VarIterator::decrement() const
  59. {
  60. if (0 == _position)
  61. throw RangeException("Beginning of iterator reached.");
  62. else if (POSITION_END == _position)
  63. _position = _pVar->size() - 1;
  64. else
  65. --_position;
  66. }
  67. void VarIterator::setPosition(std::size_t pos) const
  68. {
  69. if (_position == pos) return;
  70. if (pos < _pVar->size())
  71. _position = pos;
  72. else if (pos == _pVar->size())
  73. _position = POSITION_END;
  74. else
  75. throw RangeException("Invalid position argument.");
  76. }
  77. Var& VarIterator::operator * () const
  78. {
  79. if (POSITION_END == _position)
  80. throw InvalidAccessException("End of iterator reached.");
  81. return _pVar->operator[](_position);
  82. }
  83. Var* VarIterator::operator -> () const
  84. {
  85. if (POSITION_END == _position)
  86. throw InvalidAccessException("End of iterator reached.");
  87. return &_pVar->operator[](_position);
  88. }
  89. const VarIterator& VarIterator::operator ++ () const
  90. {
  91. increment();
  92. return *this;
  93. }
  94. VarIterator VarIterator::operator ++ (int) const
  95. {
  96. VarIterator old(*this);
  97. increment();
  98. return old;
  99. }
  100. const VarIterator& VarIterator::operator -- () const
  101. {
  102. decrement();
  103. return *this;
  104. }
  105. VarIterator VarIterator::operator -- (int) const
  106. {
  107. VarIterator old(*this);
  108. decrement();
  109. return old;
  110. }
  111. VarIterator VarIterator::operator + (std::size_t diff) const
  112. {
  113. VarIterator ri(*this);
  114. ri.setPosition(_position + diff);
  115. return ri;
  116. }
  117. VarIterator VarIterator::operator - (std::size_t diff) const
  118. {
  119. if (diff > _position) throw RangeException("Invalid position argument.");
  120. VarIterator ri(*this);
  121. ri.setPosition(_position - diff);
  122. return ri;
  123. }
  124. } } // namespace Poco::Dynamic