[Orxonox-commit 5909] r10568 - code/branches/core7/src/libraries/core/object

landauf at orxonox.net landauf at orxonox.net
Tue Sep 8 22:02:47 CEST 2015


Author: landauf
Date: 2015-09-08 22:02:46 +0200 (Tue, 08 Sep 2015)
New Revision: 10568

Modified:
   code/branches/core7/src/libraries/core/object/Iterator.h
   code/branches/core7/src/libraries/core/object/IteratorBase.h
   code/branches/core7/src/libraries/core/object/ObjectListIterator.h
Log:
Enforce type-safety of ObjectListIterator.
Previously it was possible to convert any ObjectListBaseElement* to an ObjectListIterator<T> with any T. This was because ObjectListIterator can be created from IteratorBase which in turn had a public constructor for ObjectListBaseElement*.
Now this constructor in IteratorBase is protected. As a replacement, there are two new public constructors for ObjectListElement<T>* either for the same class T or another class O that must be a derivative of T.
In short, this means that this is ok:
  ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin();  // ok, same class
  ObjectList<BaseObject>::iterator it = ObjectList<WorldEntity>::begin(); // ok, WorldEntity is derivative of BaseObject
But this will now fail to compile:
  ObjectList<BaseObject>::iterator it = ObjectList<Listable>::begin();    // not ok, Listable is not a derivative ob BaseObject

Modified: code/branches/core7/src/libraries/core/object/Iterator.h
===================================================================
--- code/branches/core7/src/libraries/core/object/Iterator.h	2015-09-08 14:16:28 UTC (rev 10567)
+++ code/branches/core7/src/libraries/core/object/Iterator.h	2015-09-08 20:02:46 UTC (rev 10568)
@@ -75,7 +75,7 @@
             /**
                 @brief Constructor: Sets the element, whereon the iterator points, to zero.
             */
-            inline Iterator() : IteratorBase<T, Iterator<T> >(NULL) {}
+            inline Iterator() : IteratorBase<T, Iterator<T> >() {}
 
             /**
                 @brief Constructor: Sets this element to a given element
@@ -87,7 +87,7 @@
                 @brief Constructor: Sets this element to the element of another Iterator.
                 @param other The other Iterator
             */
-            inline Iterator(const Iterator<T>& other) : IteratorBase<T, Iterator<T> >(other) {}
+            inline Iterator(const IteratorBase<T, Iterator<T> >& other) : IteratorBase<T, Iterator<T> >(other) {}
 
             /**
                 @brief Assigns a given element.

Modified: code/branches/core7/src/libraries/core/object/IteratorBase.h
===================================================================
--- code/branches/core7/src/libraries/core/object/IteratorBase.h	2015-09-08 14:16:28 UTC (rev 10567)
+++ code/branches/core7/src/libraries/core/object/IteratorBase.h	2015-09-08 20:02:46 UTC (rev 10568)
@@ -53,17 +53,41 @@
     {
         BOOST_STATIC_ASSERT((boost::is_base_of<Listable, T>::value));
 
+        protected:
+            /**
+                @brief Constructor: Sets the element, whereon the iterator points, to the given element.
+                This constructor is protected and only for internal usage (don't mess with the BaseElements directly).
+            */
+            inline IteratorBase(ObjectListBaseElement* element = NULL)
+            {
+                this->element_ = element;
+                this->registerIterator();
+            }
+
+
         public:
             /**
-                @brief Constructor: Sets the element, whereon the iterator points, to zero.
+                @brief Constructor: Sets the element, whereon the iterator points, to the given element.
             */
-            inline IteratorBase(ObjectListBaseElement* element)
+            inline IteratorBase(ObjectListElement<T>* element)
             {
                 this->element_ = element;
                 this->registerIterator();
             }
 
             /**
+                @brief Constructor: Sets the element, whereon the iterator points, to the given element of another type.
+                The element's type O must be a derivative of the Iterator's type T.
+            */
+            template <class O>
+            inline IteratorBase(ObjectListElement<O>* element)
+            {
+                (void)static_cast<T*>((O*)NULL); // Check type: The element's type O must be a derivative of the Iterator's type T.
+                this->element_ = element;
+                this->registerIterator();
+            }
+
+            /**
                 @brief Constructor: Sets this element to the element of another Iterator.
                 @param other The other Iterator
             */

Modified: code/branches/core7/src/libraries/core/object/ObjectListIterator.h
===================================================================
--- code/branches/core7/src/libraries/core/object/ObjectListIterator.h	2015-09-08 14:16:28 UTC (rev 10567)
+++ code/branches/core7/src/libraries/core/object/ObjectListIterator.h	2015-09-08 20:02:46 UTC (rev 10568)
@@ -73,7 +73,7 @@
             /**
                 @brief Constructor: Sets the element, whereon the ObjectListIterator points, to zero.
             */
-            inline ObjectListIterator() : IteratorBase<T, ObjectListIterator<T> >(NULL) {}
+            inline ObjectListIterator() : IteratorBase<T, ObjectListIterator<T> >() {}
 
             /**
                 @brief Constructor: Sets this element to a given element.




More information about the Orxonox-commit mailing list