VarIterator.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // VarIterator.h
  3. //
  4. // Library: Foundation
  5. // Package: Dynamic
  6. // Module: VarIterator
  7. //
  8. // Definition of the VarIterator class.
  9. //
  10. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_VarIterator_INCLUDED
  16. #define Foundation_VarIterator_INCLUDED
  17. #include "Poco/Exception.h"
  18. #include <iterator>
  19. #include <algorithm>
  20. namespace Poco {
  21. namespace Dynamic {
  22. class Var;
  23. class Foundation_API VarIterator
  24. /// VarIterator class.
  25. {
  26. public:
  27. typedef std::bidirectional_iterator_tag iterator_category;
  28. typedef Var value_type;
  29. typedef std::ptrdiff_t difference_type;
  30. typedef Var* pointer;
  31. typedef Var& reference;
  32. static const std::size_t POSITION_END;
  33. /// End position indicator.
  34. VarIterator(Var* pVar, bool positionEnd);
  35. /// Creates the VarIterator and positions it at the end of
  36. /// the recordset if positionEnd is true. Otherwise, it is
  37. /// positioned at the beginning.
  38. VarIterator(const VarIterator& other);
  39. /// Creates a copy of other VarIterator.
  40. ~VarIterator();
  41. /// Destroys the VarIterator.
  42. VarIterator& operator = (const VarIterator& other);
  43. /// Assigns the other VarIterator.
  44. bool operator == (const VarIterator& other) const;
  45. /// Equality operator.
  46. bool operator != (const VarIterator& other) const;
  47. /// Inequality operator.
  48. Var& operator * () const;
  49. /// Returns value at the current position.
  50. Var* operator -> () const;
  51. /// Returns pointer to the value at current position.
  52. const VarIterator& operator ++ () const;
  53. /// Advances by one position and returns current position.
  54. VarIterator operator ++ (int) const;
  55. /// Advances by one position and returns copy of the iterator with
  56. /// previous current position.
  57. const VarIterator& operator -- () const;
  58. /// Goes back by one position and returns copy of the iterator with
  59. /// previous current position.
  60. VarIterator operator -- (int) const;
  61. /// Goes back by one position and returns previous current position.
  62. VarIterator operator + (std::size_t diff) const;
  63. /// Returns a copy the VarIterator advanced by diff positions.
  64. VarIterator operator - (std::size_t diff) const;
  65. /// Returns a copy the VarIterator backed by diff positions.
  66. /// Throws RangeException if diff is larger than current position.
  67. void swap(VarIterator& other);
  68. /// Swaps the VarIterator with another one.
  69. private:
  70. VarIterator();
  71. void increment() const;
  72. /// Increments the iterator position by one.
  73. /// Throws RangeException if position is out of range.
  74. void decrement() const;
  75. /// Decrements the iterator position by one.
  76. /// Throws RangeException if position is out of range.
  77. void setPosition(std::size_t pos) const;
  78. /// Sets the iterator position.
  79. /// Throws RangeException if position is out of range.
  80. Var* _pVar;
  81. mutable std::size_t _position;
  82. friend class Var;
  83. };
  84. ///
  85. /// inlines
  86. ///
  87. inline bool VarIterator::operator == (const VarIterator& other) const
  88. {
  89. return _pVar == other._pVar && _position == other._position;
  90. }
  91. inline bool VarIterator::operator != (const VarIterator& other) const
  92. {
  93. return _pVar != other._pVar || _position != other._position;
  94. }
  95. } } // namespace Poco::Dynamic
  96. namespace std
  97. {
  98. template<>
  99. inline void swap<Poco::Dynamic::VarIterator>(Poco::Dynamic::VarIterator& s1,
  100. Poco::Dynamic::VarIterator& s2)
  101. /// Full template specialization of std:::swap for VarIterator
  102. {
  103. s1.swap(s2);
  104. }
  105. }
  106. #endif // Foundation_VarIterator_INCLUDED