Skip to content
This repository has been archived by the owner on Nov 10, 2020. It is now read-only.

Commit

Permalink
Added const to begin and end of Vector
Browse files Browse the repository at this point in the history
Change-Id: If62bf53aee68dadcd51491e0950126ace109a636
  • Loading branch information
Sven von Beuningen committed Jul 30, 2013
1 parent 012ddab commit 602a8aa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
22 changes: 11 additions & 11 deletions modules/capu/include/capu/container/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -253,22 +253,22 @@ namespace capu
template<typename T>
inline
typename Vector<T>::Iterator
Vector<T>::begin()
Vector<T>::begin() const
{
return Iterator(m_data.getRawData());
}

template<typename T>
inline
typename Vector<T>::Iterator
Vector<T>::end()
Vector<T>::end() const
{
return Iterator(m_data.getRawData() + m_size);
}

template<typename T>
inline
Vector<T>::Iterator::Iterator(T* start)
Vector<T>::Iterator::Iterator(const T* start)
: m_current(start)
{

Expand All @@ -292,15 +292,15 @@ namespace capu

template<typename T>
inline
T&
T const&
Vector<T>::Iterator::operator*()
{
return *m_current;
}

template<typename T>
inline
T*
T const*
Vector<T>::Iterator::operator->()
{
return &** this;
Expand Down
34 changes: 34 additions & 0 deletions modules/capu/test/container/VectorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,40 @@ namespace capu
EXPECT_EQ(47u, vector2[1]);
}

TEST_F(VectorTest, IteratorOnConstVector)
{
Vector<uint32_t> vector;

const Vector<uint32_t>& constVector = vector;

vector.push_back(42u);
vector.push_back(47u);

Vector<uint32_t> vector2;

for (Vector<uint32_t>::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<uint32_t> 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;
Expand Down

0 comments on commit 602a8aa

Please sign in to comment.