From 602a8aa4a4503c112694a6c879a93dec4df562d0 Mon Sep 17 00:00:00 2001 From: Sven von Beuningen Date: Tue, 30 Jul 2013 11:24:11 +0200 Subject: [PATCH] Added const to begin and end of Vector Change-Id: If62bf53aee68dadcd51491e0950126ace109a636 --- modules/capu/include/capu/container/Vector.h | 22 ++++++------- modules/capu/test/container/VectorTest.cpp | 34 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/modules/capu/include/capu/container/Vector.h b/modules/capu/include/capu/container/Vector.h index 8cadbd18..b58309d0 100644 --- a/modules/capu/include/capu/container/Vector.h +++ b/modules/capu/include/capu/container/Vector.h @@ -53,24 +53,24 @@ namespace capu /** * Dereferences the iterator to access the internal data */ - T& operator*(); + T const& operator*(); /** * Allows access to methods and member of the internal data */ - T* operator->(); + T const* operator->(); protected: private: /** * Creates a Iterator for the Vector * @param start pointer for the iterator */ - Iterator(T* start); + Iterator(const T* start); /** * Pointer to the current data */ - T* m_current; + T const* m_current; }; /** @@ -130,13 +130,13 @@ namespace capu * Returns a new Iterator to the start of the Vector * @return a new Iterator to the start of the Vector */ - Iterator begin(); + Iterator begin() const; /** * Returns a new Iterator to the end of the Vector * @return a new Iterator to the end of the Vector */ - Iterator end(); + Iterator end() const; protected: private: @@ -253,7 +253,7 @@ namespace capu template inline typename Vector::Iterator - Vector::begin() + Vector::begin() const { return Iterator(m_data.getRawData()); } @@ -261,14 +261,14 @@ namespace capu template inline typename Vector::Iterator - Vector::end() + Vector::end() const { return Iterator(m_data.getRawData() + m_size); } template inline - Vector::Iterator::Iterator(T* start) + Vector::Iterator::Iterator(const T* start) : m_current(start) { @@ -292,7 +292,7 @@ namespace capu template inline - T& + T const& Vector::Iterator::operator*() { return *m_current; @@ -300,7 +300,7 @@ namespace capu template inline - T* + T const* Vector::Iterator::operator->() { return &** this; diff --git a/modules/capu/test/container/VectorTest.cpp b/modules/capu/test/container/VectorTest.cpp index af279e83..bf404801 100644 --- a/modules/capu/test/container/VectorTest.cpp +++ b/modules/capu/test/container/VectorTest.cpp @@ -99,6 +99,40 @@ namespace capu EXPECT_EQ(47u, vector2[1]); } + TEST_F(VectorTest, IteratorOnConstVector) + { + Vector vector; + + const Vector& constVector = vector; + + vector.push_back(42u); + vector.push_back(47u); + + Vector vector2; + + for (Vector::Iterator iter = constVector.begin(); iter != constVector.end(); ++iter) + { + vector2.push_back(*iter); + } + + EXPECT_EQ(42u, vector2[0]); + EXPECT_EQ(47u, vector2[1]); + } + + TEST_F(VectorTest, AccessOperator) + { + Vector vector; + + vector.push_back(42u); + vector.push_back(47u); + + vector[0] = 47u; + vector[1] = 42u; + + EXPECT_EQ(47u, vector[0]); + EXPECT_EQ(42u, vector[1]); + } + struct TestStruct { uint32_t value1;