[Orxonox-commit 4929] r9598 - code/branches/core6/src/libraries/core/object
landauf at orxonox.net
landauf at orxonox.net
Fri Mar 29 14:12:42 CET 2013
Author: landauf
Date: 2013-03-29 14:12:41 +0100 (Fri, 29 Mar 2013)
New Revision: 9598
Added:
code/branches/core6/src/libraries/core/object/IteratorBase.h
Modified:
code/branches/core6/src/libraries/core/object/Iterator.h
code/branches/core6/src/libraries/core/object/ObjectListIterator.h
Log:
added common base template for Iterator and ObjectListIterator in order to avoid code duplication
Modified: code/branches/core6/src/libraries/core/object/Iterator.h
===================================================================
--- code/branches/core6/src/libraries/core/object/Iterator.h 2013-03-29 10:44:22 UTC (rev 9597)
+++ code/branches/core6/src/libraries/core/object/Iterator.h 2013-03-29 13:12:41 UTC (rev 9598)
@@ -56,6 +56,7 @@
#include "core/CorePrereqs.h"
#include "ObjectListBase.h"
+#include "IteratorBase.h"
namespace orxonox
{
@@ -68,140 +69,37 @@
@see See @ref IteratorExample "Iterator.h" for more information an example.
*/
template <class T>
- class Iterator
+ class Iterator : public IteratorBase<T, Iterator<T> >
{
public:
/**
@brief Constructor: Sets the element, whereon the iterator points, to zero.
*/
- inline Iterator()
- {
- this->element_ = NULL;
- this->list_ = NULL;
- }
+ inline Iterator() : IteratorBase<T, Iterator<T> >(NULL) {}
/**
- @brief Constructor: Sets this element to the element of another Iterator.
- @param other The other Iterator
- */
- inline Iterator(const Iterator<T>& other)
- {
- this->element_ = other.element_;
- this->registerIterator();
- }
-
- /**
@brief Constructor: Sets this element to a given element
@param element The element
*/
- inline Iterator(ObjectListBaseElement* element)
- {
- this->element_ = element;
- this->registerIterator();
- }
+ inline Iterator(ObjectListBaseElement* element) : IteratorBase<T, Iterator<T> >(element) {}
/**
- @brief Constructor: Sets this element to the element an ObjectListIterator.
- @param other The ObjectListIterator
- */
- template <class O>
- inline Iterator(const ObjectListIterator<O>& other)
- {
- this->element_ = other.element_;
- this->registerIterator();
- }
-
- /**
- @brief Unregisters the Iterator from the ObjectList.
- */
- inline ~Iterator()
- {
- this->unregisterIterator();
- }
-
- /**
- @brief Assigns the element of another Iterator.
+ @brief Constructor: Sets this element to the element of another Iterator.
@param other The other Iterator
*/
- inline Iterator<T>& operator=(const Iterator<T>& other)
- {
- this->unregisterIterator();
- this->element_ = other.element_;
- this->registerIterator();
+ inline Iterator(const Iterator<T>& other) : IteratorBase<T, Iterator<T> >(other) {}
- return (*this);
- }
-
/**
@brief Assigns a given element.
@param element The element
*/
inline Iterator<T>& operator=(ObjectListBaseElement* element)
{
- this->unregisterIterator();
- this->element_ = element;
- this->registerIterator();
-
+ this->setElement(element);
return (*this);
}
/**
- @brief Assigns the element of an ObjectListIterator.
- @param other The ObjectListIterator
- */
- template <class O>
- inline Iterator<T>& operator=(const ObjectListIterator<O>& other)
- {
- this->unregisterIterator();
- this->element_ = other.element_;
- this->registerIterator();
-
- return (*this);
- }
-
- /**
- @brief Overloading of the ++it operator: Iterator points to the next object in the list.
- @return The Iterator itself
- */
- inline const Iterator<T>& operator++()
- {
- this->element_ = this->element_->next_;
- return *this;
- }
-
- /**
- @brief Overloading of the it++ operator: Iterator points to the next object in the list.
- @return The Iterator itself
- */
- inline Iterator<T> operator++(int)
- {
- Iterator<T> copy = *this;
- this->element_ = this->element_->next_;
- return copy;
- }
-
- /**
- @brief Overloading of the --it operator: Iterator points to the previous object in the list.
- @return The Iterator itself
- */
- inline const Iterator<T>& operator--()
- {
- this->element_ = this->element_->prev_;
- return *this;
- }
-
- /**
- @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
- @return The Iterator itself
- */
- inline Iterator<T> operator--(int i)
- {
- Iterator<T> copy = *this;
- this->element_ = this->element_->prev_;
- return copy;
- }
-
- /**
@brief Overloading of the *it operator: returns the pointer to the object.
@return The object the Iterator points at
*/
@@ -218,72 +116,6 @@
{
return orxonox_cast<T*>(this->element_->objectBase_);
}
-
- /**
- @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
- @return True if the Iterator points to an existing object.
- */
- inline operator bool() const
- {
- return (this->element_ != NULL);
- }
-
- /**
- @brief Overloading of the == operator to compare with another Iterator.
- @param compare The other Iterator
- @return True if the iterators point to the same element
- */
- inline bool operator==(const Iterator<T>& compare) const
- {
- return (this->element_ == compare.element_);
- }
-
- /**
- @brief Overloading of the != operator to compare with another Iterator.
- @param compare The other Iterator
- @return True if the iterators point to different elements
- */
- inline bool operator!=(const Iterator<T>& compare) const
- {
- return (this->element_ != compare.element_);
- }
-
- /**
- @brief Increments the Iterator if it points at the given object.
- @param object The object to compare with
- */
- inline void incrementIfEqual(Listable* object)
- {
- if (this->element_ && this->element_->objectBase_ == object)
- this->operator++();
- }
-
- private:
- /**
- * @brief Registers the Iterator at the list to which it belongs
- */
- inline void registerIterator()
- {
- if (this->element_)
- {
- this->list_ = this->element_->list_;
- this->list_->registerIterator(this);
- }
- else
- this->list_ = NULL;
- }
-
- /**
- * @brief Unregisters the Iterator from the list (if any)
- */
- inline void unregisterIterator()
- {
- if (this->list_)
- this->list_->unregisterIterator(this);
- }
-
- ObjectListBaseElement* element_; //!< The element the Iterator points at
- ObjectListBase* list_; //!< The list in which the Iterator registered itself
};
}
Added: code/branches/core6/src/libraries/core/object/IteratorBase.h
===================================================================
--- code/branches/core6/src/libraries/core/object/IteratorBase.h (rev 0)
+++ code/branches/core6/src/libraries/core/object/IteratorBase.h 2013-03-29 13:12:41 UTC (rev 9598)
@@ -0,0 +1,216 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Fabian 'x3n' Landau
+ * Co-authors:
+ * ...
+ *
+ */
+
+/**
+ @file
+ @ingroup Object ObjectList
+ @brief Definition of the IteratorBase class, used to iterate through object-lists.
+*/
+
+#ifndef _IteratorBase_H__
+#define _IteratorBase_H__
+
+#include "core/CorePrereqs.h"
+
+#include "ObjectListBase.h"
+
+namespace orxonox
+{
+ /**
+ @brief The Iterator allows to iterate through object lists.
+ It serves as base class for @ref ObjectListIterator and @ref Iterator
+ */
+ template <class T, class I>
+ class IteratorBase
+ {
+ public:
+ /**
+ @brief Constructor: Sets the element, whereon the iterator points, to zero.
+ */
+ inline IteratorBase(ObjectListBaseElement* element)
+ {
+ this->element_ = element;
+ this->registerIterator();
+ }
+
+ /**
+ @brief Constructor: Sets this element to the element of another Iterator.
+ @param other The other Iterator
+ */
+ inline IteratorBase(const IteratorBase& other)
+ {
+ this->element_ = other.element_;
+ this->registerIterator();
+ }
+
+ /**
+ @brief Unregisters the Iterator from the ObjectList.
+ */
+ inline ~IteratorBase()
+ {
+ this->unregisterIterator();
+ }
+
+ /**
+ @brief Assigns a given element.
+ @param element The element
+ */
+ inline IteratorBase<T, I>& operator=(ObjectListElement<T>* element)
+ {
+ this->setElement(element);
+ return (*this);
+ }
+
+ /**
+ @brief Assigns the element of another Iterator.
+ @param other The other Iterator
+ */
+ inline IteratorBase<T, I>& operator=(const IteratorBase<T, I>& other)
+ {
+ this->setElement(other.element_);
+ return (*this);
+ }
+
+ /**
+ @brief Overloading of the ++it operator: Iterator points to the next object in the list.
+ @return The Iterator itself
+ */
+ inline const IteratorBase<T, I>& operator++()
+ {
+ this->element_ = this->element_->next_;
+ return *this;
+ }
+
+ /**
+ @brief Overloading of the it++ operator: Iterator points to the next object in the list.
+ @return The Iterator itself
+ */
+ inline I operator++(int)
+ {
+ I copy = *this;
+ this->element_ = this->element_->next_;
+ return copy;
+ }
+
+ /**
+ @brief Overloading of the --it operator: Iterator points to the previous object in the list.
+ @return The Iterator itself
+ */
+ inline const IteratorBase<T, I>& operator--()
+ {
+ this->element_ = this->element_->prev_;
+ return *this;
+ }
+
+ /**
+ @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
+ @return The Iterator itself
+ */
+ inline I operator--(int i)
+ {
+ I copy = *this;
+ this->element_ = this->element_->prev_;
+ return copy;
+ }
+
+ /**
+ @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
+ @return True if the Iterator points to an existing object.
+ */
+ inline operator bool() const
+ {
+ return (this->element_ != NULL);
+ }
+
+ /**
+ @brief Overloading of the == operator to compare with another Iterator.
+ @param compare The other Iterator
+ @return True if the iterators point to the same element
+ */
+ inline bool operator==(const IteratorBase<T, I>& compare) const
+ {
+ return (this->element_ == compare.element_);
+ }
+
+ /**
+ @brief Overloading of the != operator to compare with another Iterator.
+ @param compare The other Iterator
+ @return True if the iterators point to different elements
+ */
+ inline bool operator!=(const IteratorBase<T, I>& compare) const
+ {
+ return (this->element_ != compare.element_);
+ }
+
+ /**
+ @brief Increments the Iterator if it points at the given object.
+ @param object The object to compare with
+ */
+ inline void incrementIfEqual(Listable* object)
+ {
+ if (this->element_ && this->element_->objectBase_ == object)
+ this->operator++();
+ }
+
+ protected:
+ inline void setElement(ObjectListBaseElement* element)
+ {
+ this->unregisterIterator();
+ this->element_ = element;
+ this->registerIterator();
+ }
+
+ /**
+ * @brief Registers the Iterator at the list to which it belongs
+ */
+ inline void registerIterator()
+ {
+ if (this->element_)
+ {
+ this->list_ = this->element_->list_;
+ this->list_->registerIterator(this);
+ }
+ else
+ this->list_ = NULL;
+ }
+
+ /**
+ * @brief Unregisters the Iterator from the list (if any)
+ */
+ inline void unregisterIterator()
+ {
+ if (this->list_)
+ this->list_->unregisterIterator(this);
+ }
+
+ ObjectListBaseElement* element_; //!< The element the Iterator points at
+ ObjectListBase* list_; //!< The list in which the Iterator registered itself
+ };
+}
+
+#endif /* _IteratorBase_H__ */
Property changes on: code/branches/core6/src/libraries/core/object/IteratorBase.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: code/branches/core6/src/libraries/core/object/ObjectListIterator.h
===================================================================
--- code/branches/core6/src/libraries/core/object/ObjectListIterator.h 2013-03-29 10:44:22 UTC (rev 9597)
+++ code/branches/core6/src/libraries/core/object/ObjectListIterator.h 2013-03-29 13:12:41 UTC (rev 9598)
@@ -57,6 +57,7 @@
#include "core/CorePrereqs.h"
#include "core/class/Identifier.h"
#include "ObjectList.h"
+#include "IteratorBase.h"
namespace orxonox
{
@@ -66,7 +67,7 @@
@see See @ref ObjectListIteratorExample "ObjectListIterator.h" for more information an example.
*/
template <class T>
- class ObjectListIterator
+ class ObjectListIterator : public IteratorBase<T, ObjectListIterator<T> >
{
template <class I>
friend class Iterator;
@@ -75,109 +76,27 @@
/**
@brief Constructor: Sets the element, whereon the ObjectListIterator points, to zero.
*/
- inline ObjectListIterator()
- {
- this->element_ = 0;
- ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
- }
+ inline ObjectListIterator() : IteratorBase<T, ObjectListIterator<T> >(NULL) {}
/**
@brief Constructor: Sets this element to a given element.
@param element The element to start with
*/
- inline ObjectListIterator(ObjectListElement<T>* element)
- {
- this->element_ = element;
- ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
- }
+ inline ObjectListIterator(ObjectListElement<T>* element) : IteratorBase<T, ObjectListIterator<T> >(element) {}
/**
@brief Constructor: Sets this element to the element of another ObjectListIterator.
@param other The other ObjectListIterator
*/
- inline ObjectListIterator(const ObjectListIterator<T>& other)
- {
- this->element_ = other.element_;
- ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
- }
+ inline ObjectListIterator(const IteratorBase<T, ObjectListIterator<T> >& other) : IteratorBase<T, ObjectListIterator<T> >(other) {}
/**
- @brief Unregisters the ObjectListIterator from the ObjectList.
- */
- inline ~ObjectListIterator()
- {
- ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterObjectListIterator(this);
- }
-
- /**
- @brief Assigns an ObjectListElement.
- @param element The ObjectListElement
- */
- inline ObjectListIterator<T>& operator=(ObjectListElement<T>* element)
- {
- this->element_ = element;
- return (*this);
- }
-
- /**
- @brief Assigns the element of another ObjectListIterator.
- @param other The other ObjectListIterator
- */
- inline ObjectListIterator<T>& operator=(const ObjectListIterator<T>& other)
- {
- this->element_ = other.element_;
- return (*this);
- }
-
- /**
- @brief Overloading of the ++it operator: ObjectListIterator points to the next object in the list.
- @return The ObjectListIterator itself
- */
- inline const ObjectListIterator<T>& operator++()
- {
- this->element_ = static_cast<ObjectListElement<T>*>(this->element_->next_);
- return *this;
- }
-
- /**
- @brief Overloading of the it++ operator: ObjectListIterator points to the next object in the list.
- @return The ObjectListIterator itself
- */
- inline ObjectListIterator<T> operator++(int)
- {
- ObjectListIterator<T> copy = *this;
- this->element_ = static_cast<ObjectListElement<T>*>(this->element_->next_);
- return copy;
- }
-
- /**
- @brief Overloading of the --it operator: ObjectListIterator points to the previous object in the list.
- @return The ObjectListIterator itself
- */
- inline const ObjectListIterator<T>& operator--()
- {
- this->element_ = static_cast<ObjectListElement<T>*>(this->element_->prev_);
- return *this;
- }
-
- /**
- @brief Overloading of the it-- operator: ObjectListIterator points to the previous object in the list.
- @return The ObjectListIterator itself
- */
- inline ObjectListIterator<T> operator--(int i)
- {
- ObjectListIterator<T> copy = *this;
- this->element_ = static_cast<ObjectListElement<T>*>(this->element_->prev_);
- return copy;
- }
-
- /**
@brief Overloading of the *it operator: returns the pointer to the object.
@return The object the ObjectListIterator points at
*/
inline T* operator*() const
{
- return this->element_->object_;
+ return static_cast<ObjectListElement<T>*>(this->element_)->object_;
}
/**
@@ -186,50 +105,8 @@
*/
inline T* operator->() const
{
- return this->element_->object_;
+ return static_cast<ObjectListElement<T>*>(this->element_)->object_;
}
-
- /**
- @brief Overloading of the typecast-operator to bool: returns true if the ObjectListIterator points to an existing object.
- @return True if the ObjectListIterator points to an existing object.
- */
- inline operator bool() const
- {
- return (this->element_ != 0);
- }
-
- /**
- @brief Overloading of the == operator to compare with another ObjectListIterator.
- @param compare The other ObjectListIterator
- @return True if the ObjectListIterator point to the same element
- */
- inline bool operator==(const ObjectListIterator<T>& compare) const
- {
- return (this->element_ == compare.element_);
- }
-
- /**
- @brief Overloading of the != operator to compare with another ObjectListIterator.
- @param compare The other ObjectListIterator
- @return True if the ObjectListIterator point to different elements
- */
- inline bool operator!=(const ObjectListIterator<T>& compare) const
- {
- return (this->element_ != compare.element_);
- }
-
- /**
- @brief Increments the ObjectListIterator if it points at the given object.
- @param object The object to compare with
- */
- inline void incrementIfEqual(Listable* object)
- {
- if (this->element_ && this->element_->objectBase_ == object)
- this->operator++();
- }
-
- private:
- ObjectListElement<T>* element_; //!< The element the iterator points at
};
}
More information about the Orxonox-commit
mailing list