VarIterator.h 3.6 KB

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