From 64c751f3a31b978fc656f5e01b821d3d08a49cce Mon Sep 17 00:00:00 2001 From: vfjpl Date: Thu, 3 Oct 2024 10:52:45 +0200 Subject: [PATCH] Add missing relational operators to VarIterator (#4714) * Add missing relational operators to VarIterator * Add tests for relational operators of VarIterator --- Foundation/include/Poco/Dynamic/VarIterator.h | 36 +++++++++++++++++++ Foundation/src/VarIterator.cpp | 3 +- Foundation/testsuite/src/VarTest.cpp | 6 ++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Foundation/include/Poco/Dynamic/VarIterator.h b/Foundation/include/Poco/Dynamic/VarIterator.h index eb9129fbc..ebf6c0853 100644 --- a/Foundation/include/Poco/Dynamic/VarIterator.h +++ b/Foundation/include/Poco/Dynamic/VarIterator.h @@ -69,6 +69,18 @@ public: bool operator != (const VarIterator& other) const; /// Inequality operator. + bool operator < (const VarIterator& other) const; + /// Less than operator. + + bool operator > (const VarIterator& other) const; + /// Greater than operator. + + bool operator <= (const VarIterator& other) const; + /// Less than or equal to operator. + + bool operator >= (const VarIterator& other) const; + /// Greater than or equal to operator. + Var& operator * () const; /// Returns value at the current position. @@ -138,6 +150,30 @@ inline bool VarIterator::operator != (const VarIterator& other) const } +inline bool VarIterator::operator < (const VarIterator& other) const +{ + return _position < other._position; +} + + +inline bool VarIterator::operator > (const VarIterator& other) const +{ + return _position > other._position; +} + + +inline bool VarIterator::operator <= (const VarIterator& other) const +{ + return _position <= other._position; +} + + +inline bool VarIterator::operator >= (const VarIterator& other) const +{ + return _position >= other._position; +} + + } } // namespace Poco::Dynamic diff --git a/Foundation/src/VarIterator.cpp b/Foundation/src/VarIterator.cpp index f4f66e395..cd4161aac 100644 --- a/Foundation/src/VarIterator.cpp +++ b/Foundation/src/VarIterator.cpp @@ -83,8 +83,7 @@ void VarIterator::increment() const { if (POSITION_END == _position) throw RangeException("End of iterator reached."); - - if (_position < _pVar->size() - 1) + else if (_position < _pVar->size() - 1) ++_position; else _position = POSITION_END; diff --git a/Foundation/testsuite/src/VarTest.cpp b/Foundation/testsuite/src/VarTest.cpp index 7b56545c9..c05ce734e 100644 --- a/Foundation/testsuite/src/VarTest.cpp +++ b/Foundation/testsuite/src/VarTest.cpp @@ -3161,6 +3161,8 @@ void VarTest::testIterator() da = Poco::Dynamic::Array(); assertTrue(da.begin() == da.end()); + assertTrue(da.begin() <= da.end()); + assertTrue(da.begin() >= da.end()); da = 1; assertTrue (!da.isEmpty()); @@ -3172,6 +3174,10 @@ void VarTest::testIterator() } catch (RangeException&) {} assertTrue (da.begin() != da.end()); + assertTrue (da.begin() <= da.end()); + assertTrue (da.begin() < da.end()); + assertTrue (da.end() >= da.begin()); + assertTrue (da.end() > da.begin()); Var::Iterator it = da.begin(); Var::Iterator end = da.end();