Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 8, 2015, 10:02:46 PM (9 years ago)
Author:
landauf
Message:

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

Location:
code/branches/core7/src/libraries/core/object
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core7/src/libraries/core/object/Iterator.h

    r9667 r10568  
    7676                @brief Constructor: Sets the element, whereon the iterator points, to zero.
    7777            */
    78             inline Iterator() : IteratorBase<T, Iterator<T> >(NULL) {}
     78            inline Iterator() : IteratorBase<T, Iterator<T> >() {}
    7979
    8080            /**
     
    8888                @param other The other Iterator
    8989            */
    90             inline Iterator(const Iterator<T>& other) : IteratorBase<T, Iterator<T> >(other) {}
     90            inline Iterator(const IteratorBase<T, Iterator<T> >& other) : IteratorBase<T, Iterator<T> >(other) {}
    9191
    9292            /**
  • code/branches/core7/src/libraries/core/object/IteratorBase.h

    r10467 r10568  
    5454        BOOST_STATIC_ASSERT((boost::is_base_of<Listable, T>::value));
    5555
     56        protected:
     57            /**
     58                @brief Constructor: Sets the element, whereon the iterator points, to the given element.
     59                This constructor is protected and only for internal usage (don't mess with the BaseElements directly).
     60            */
     61            inline IteratorBase(ObjectListBaseElement* element = NULL)
     62            {
     63                this->element_ = element;
     64                this->registerIterator();
     65            }
     66
     67
    5668        public:
    5769            /**
    58                 @brief Constructor: Sets the element, whereon the iterator points, to zero.
    59             */
    60             inline IteratorBase(ObjectListBaseElement* element)
    61             {
     70                @brief Constructor: Sets the element, whereon the iterator points, to the given element.
     71            */
     72            inline IteratorBase(ObjectListElement<T>* element)
     73            {
     74                this->element_ = element;
     75                this->registerIterator();
     76            }
     77
     78            /**
     79                @brief Constructor: Sets the element, whereon the iterator points, to the given element of another type.
     80                The element's type O must be a derivative of the Iterator's type T.
     81            */
     82            template <class O>
     83            inline IteratorBase(ObjectListElement<O>* element)
     84            {
     85                (void)static_cast<T*>((O*)NULL); // Check type: The element's type O must be a derivative of the Iterator's type T.
    6286                this->element_ = element;
    6387                this->registerIterator();
  • code/branches/core7/src/libraries/core/object/ObjectListIterator.h

    r9667 r10568  
    7474                @brief Constructor: Sets the element, whereon the ObjectListIterator points, to zero.
    7575            */
    76             inline ObjectListIterator() : IteratorBase<T, ObjectListIterator<T> >(NULL) {}
     76            inline ObjectListIterator() : IteratorBase<T, ObjectListIterator<T> >() {}
    7777
    7878            /**
Note: See TracChangeset for help on using the changeset viewer.