Commit 9311e4b6 authored by Joshua's avatar Joshua

Make LifetimeList iterator C++20 ready

parent 7bc4829a
......@@ -65,25 +65,41 @@ public:
* @brief Iterates over a LifetimeList.
*/
class Iterator :
public std::
iterator<std::forward_iterator_tag, T, std::ptrdiff_t, std::remove_reference_t<T>>
public std::iterator<std::forward_iterator_tag,
std::remove_cv_t<T>,
std::ptrdiff_t,
std::add_pointer_t<T>,
std::add_lvalue_reference_t<T>>
{
public:
typedef std::forward_iterator_tag iterator_category;
/// The type "pointed to" by the iterator.
typedef std::remove_cv_t<T> value_type;
/// Distance between iterators is represented as this type.
typedef std::ptrdiff_t difference_type;
/// This type represents a pointer-to-value_type.
typedef std::add_pointer_t<T> pointer;
/// This type represents a reference-to-value_type.
typedef std::add_lvalue_reference_t<T> reference;
constexpr Iterator();
Iterator(const LifetimeList& lst);
Iterator(const Iterator& other) = default;
Iterator& operator=(const Iterator& other) = default;
bool operator==(const Iterator& other) const;
bool operator!=(const Iterator& other) const;
T& operator*() const;
constexpr bool operator==(const Iterator& other) const;
reference operator*() const;
pointer operator->() const;
// Prefix increment
Iterator& operator++();
// Postfix increment
Iterator operator++(int);
private:
/** Needed for private constructor. */
friend LifetimeList;
Iterator();
Iterator(LifetimeList& lst);
/** Node at the current position. */
Node* current;
};
......@@ -103,9 +119,9 @@ public:
template<class... ConstParamTypes>
std::unique_ptr<Node> appendDynamic(ConstParamTypes&&... value);
Iterator begin() const;
Iterator end() const;
size_t size() const;
Iterator begin() const;
constexpr Iterator end() const;
size_t size() const;
private:
Node* root; /**< Root(first) node of the collection. */
......
......@@ -9,12 +9,14 @@
#include "patterns/lifetime_list/LifetimeList.hpp"
/**
* @brief creates an empty iterator
* @brief creates an iterator over lst
*
* @tparam T datatype of objects in LifetimeList
* @tparam T
* @param lst
* @param atEnd false: start in front, true: start at end
*/
template<class T>
Patterns::LifetimeList<T>::Iterator::Iterator() : current(nullptr)
constexpr Patterns::LifetimeList<T>::Iterator::Iterator() : current(nullptr)
{}
/**
......@@ -25,47 +27,40 @@ Patterns::LifetimeList<T>::Iterator::Iterator() : current(nullptr)
* @param atEnd false: start in front, true: start at end
*/
template<class T>
Patterns::LifetimeList<T>::Iterator::Iterator(LifetimeList<T>& lst) : current(lst.root)
Patterns::LifetimeList<T>::Iterator::Iterator(const LifetimeList<T>& lst) : current(lst.root)
{}
/**
* @brief comperator to fulfill the iterator interface
*
* @tparam T
* @param other
* @return true
* @return false
*/
template<class T>
bool Patterns::LifetimeList<T>::Iterator::operator==(const Iterator& other) const
constexpr bool Patterns::LifetimeList<T>::Iterator::operator==(
const Patterns::LifetimeList<T>::Iterator& other) const
{
return other.current == current;
return current == other.current;
}
/**
* @brief comperator to fulfill the iterator interface
* @brief returns the encapsuled value of current node
*
* @tparam T
* @param other
* @return true
* @return false
* @return T
*/
template<class T>
bool Patterns::LifetimeList<T>::Iterator::operator!=(const Iterator& other) const
Patterns::LifetimeList<T>::Iterator::reference Patterns::LifetimeList<T>::Iterator::operator*()
const
{
return other.current != current;
return current->value;
}
/**
* @brief returns the encapsuled value of current node
* @brief returns the encapsuled value
* of current node
*
* @tparam T
* @return T
*/
template<class T>
T& Patterns::LifetimeList<T>::Iterator::operator*() const
Patterns::LifetimeList<T>::Iterator::pointer Patterns::LifetimeList<T>::Iterator::operator->() const
{
return current->value;
return &current->value;
}
/**
......@@ -83,3 +78,17 @@ typename Patterns::LifetimeList<T>::Iterator& Patterns::LifetimeList<T>::Iterato
}
return *this;
}
/**
* @brief iterate over the linked list
*
* @tparam T
* @return Patterns::LifetimeList<T>::Iterator&
*/
template<class T>
typename Patterns::LifetimeList<T>::Iterator Patterns::LifetimeList<T>::Iterator::operator++(int)
{
Iterator tmp = *this;
++(*this);
return tmp;
}
\ No newline at end of file
......@@ -119,7 +119,7 @@ std::unique_ptr<typename Patterns::LifetimeList<T>::Node> Patterns::LifetimeList
template<class T>
typename Patterns::LifetimeList<T>::Iterator Patterns::LifetimeList<T>::begin() const
{
return Iterator(static_cast<LifetimeList&>(*this));
return Iterator(*this);
}
/**
......@@ -128,9 +128,9 @@ typename Patterns::LifetimeList<T>::Iterator Patterns::LifetimeList<T>::begin()
* @return Iterator
*/
template<class T>
typename Patterns::LifetimeList<T>::Iterator Patterns::LifetimeList<T>::end() const
constexpr typename Patterns::LifetimeList<T>::Iterator Patterns::LifetimeList<T>::end() const
{
return Iterator();
return Iterator {};
}
/**
......@@ -142,9 +142,8 @@ typename Patterns::LifetimeList<T>::Iterator Patterns::LifetimeList<T>::end() co
template<class T>
size_t Patterns::LifetimeList<T>::size() const
{
size_t n = 0;
auto node = begin();
for (; node != end(); ++node)
size_t n = 0;
for (auto node = begin(); node != end(); ++node)
{
++n;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment