Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10736


Ignore:
Timestamp:
Oct 31, 2015, 11:50:17 PM (8 years ago)
Author:
landauf
Message:

fixed range based for-loop: ObjectList now returns an iterator
added tests and refactored construction of Iterator/IteratorBase/ObjectListIterator

Location:
code/branches/cpp11_v2
Files:
1 added
8 edited

Legend:

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

    r10624 r10736  
    7979
    8080            /**
    81                 @brief Constructor: Sets this element to a given element
    82                 @param element The element
    83             */
    84             inline Iterator(ObjectListBaseElement* element) : IteratorBase<T, Iterator<T> >(element) {}
    85 
    86             /**
    8781                @brief Constructor: Sets this element to the element of another Iterator.
    8882                @param other The other Iterator
    8983            */
    90             inline Iterator(const IteratorBase<T, Iterator<T> >& other) : IteratorBase<T, Iterator<T> >(other) {}
     84            template <class OT, class OI>
     85            inline Iterator(const IteratorBase<OT, OI>& other) : IteratorBase<T, Iterator<T> >(other) {}
    9186
    9287            /**
  • code/branches/cpp11_v2/src/libraries/core/object/IteratorBase.h

    r10624 r10736  
    5454        BOOST_STATIC_ASSERT((boost::is_base_of<Listable, T>::value));
    5555
    56         protected:
     56        public:
    5757            /**
    5858                @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)
     59            */
     60            inline IteratorBase(ObjectListElement<T>* element = NULL)
    6261            {
    6362                this->element_ = element;
     
    6564            }
    6665
    67 
    68         public:
    69             /**
    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 
    7866            /**
    7967                @brief Constructor: Sets the element, whereon the iterator points, to the given element of another type.
    8068                The element's type O must be a derivative of the Iterator's type T.
    8169            */
    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.
    86                 this->element_ = element;
    87                 this->registerIterator();
    88             }
    89 
    90             /**
    91                 @brief Constructor: Sets this element to the element of another Iterator.
    92                 @param other The other Iterator
    93             */
    94             inline IteratorBase(const IteratorBase& other)
    95             {
    96                 this->element_ = other.element_;
     70            template <class OT, class OI>
     71            inline IteratorBase(const IteratorBase<OT, OI>& other)
     72            {
     73                this->element_ = other.getElement();
    9774                this->registerIterator();
    9875            }
     
    205182                if (this->element_ == element)
    206183                    this->operator++();
     184            }
     185
     186            inline ObjectListBaseElement* getElement() const
     187            {
     188                return this->element_;
    207189            }
    208190
  • code/branches/cpp11_v2/src/libraries/core/object/ObjectList.h

    r10624 r10736  
    8484
    8585            /// Returns an Iterator to the first element in the list (for the root context).
    86             inline static ObjectListElement<T>* begin()
     86            inline static ObjectListIterator<T> begin()
    8787            {   return begin(Context::getRootContext());   }
    8888            /// Returns an Iterator to the first element in the list.
    89             inline static ObjectListElement<T>* begin(Context* context)
     89            inline static ObjectListIterator<T> begin(Context* context)
    9090            {
    9191                ObjectListBase* list = context->getObjectList<T>();
     
    9494
    9595            /// Returns an Iterator to the element after the last element in the list (for the root context).
    96             inline static ObjectListElement<T>* end()
     96            inline static ObjectListIterator<T> end()
    9797            {   return end(Context::getRootContext());   }
    9898            /// Returns an Iterator to the element after the last element in the list.
    99             inline static ObjectListElement<T>* end(Context* context)
     99            inline static ObjectListIterator<T> end(Context* context)
    100100            {
    101101                ObjectListBase* list = context->getObjectList<T>();
     
    104104
    105105            /// Returns an Iterator to the last element in the list (for the root context).
    106             inline static ObjectListElement<T>* rbegin()
     106            inline static ObjectListIterator<T> rbegin()
    107107            {   return rbegin(Context::getRootContext());   }
    108108            /// Returns an Iterator to the last element in the list.
    109             inline static ObjectListElement<T>* rbegin(Context* context)
     109            inline static ObjectListIterator<T> rbegin(Context* context)
    110110            {
    111111                ObjectListBase* list = context->getObjectList<T>();
     
    114114
    115115            /// Returns an Iterator to the element before the first element in the list (for the root context).
    116             inline static ObjectListElement<T>* rend()
     116            inline static ObjectListIterator<T> rend()
    117117            {   return rend(Context::getRootContext());   }
    118118            /// Returns an Iterator to the element before the first element in the list.
    119             inline static ObjectListElement<T>* rend(Context* context)
     119            inline static ObjectListIterator<T> rend(Context* context)
    120120            {
    121121                ObjectListBase* list = context->getObjectList<T>();
  • code/branches/cpp11_v2/src/libraries/core/object/ObjectListBase.h

    r10733 r10736  
    9393            }
    9494
    95             operator T*()
    96             {
    97                 return object_;
    98             }
    99 
    10095            T* object_;              //!< The object
    10196    };
  • code/branches/cpp11_v2/src/libraries/core/object/ObjectListIterator.h

    r10624 r10736  
    8686                @param other The other ObjectListIterator
    8787            */
    88             inline ObjectListIterator(const IteratorBase<T, ObjectListIterator<T> >& other) : IteratorBase<T, ObjectListIterator<T> >(other) {}
     88            template <class OI>
     89            inline ObjectListIterator(const IteratorBase<T, OI>& other) : IteratorBase<T, ObjectListIterator<T> >(other) {}
    8990
    9091            /**
  • code/branches/cpp11_v2/test/core/CMakeLists.txt

    r10624 r10736  
    2525    object/ListableTest.cc
    2626    object/ObjectListBaseTest.cc
     27    object/ObjectListTest.cc
    2728    object/ObjectListIteratorTest.cc
    2829    object/StrongPtrTest.cc
  • code/branches/cpp11_v2/test/core/object/IteratorTest.cc

    r10624 r10736  
    8080            it->test();
    8181    }
     82
     83    TEST_F(IteratorTest, CanIterateOverInterfaceListWithInterfaceIterator)
     84    {
     85        TestClass testClass;
     86        TestInterface testInterface;
     87
     88        size_t i = 0;
     89        for (Iterator<TestInterface> it = ObjectList<TestInterface>::begin(); it != ObjectList<TestInterface>::end(); ++it)
     90        {
     91            ++i;
     92            if (i == 1u) EXPECT_EQ(&testClass, *it);
     93            if (i == 2u) EXPECT_EQ(&testInterface, *it);
     94        }
     95        EXPECT_EQ(2u, i);
     96    }
     97
     98    TEST_F(IteratorTest, CanIterateOverClassListWithClassIterator)
     99    {
     100        TestClass testClass;
     101        TestInterface testInterface;
     102
     103        size_t i = 0;
     104        for (Iterator<TestClass> it = ObjectList<TestClass>::begin(); it != ObjectList<TestClass>::end(); ++it)
     105        {
     106            ++i;
     107            if (i == 1u) EXPECT_EQ(&testClass, *it);
     108        }
     109        EXPECT_EQ(1u, i);
     110    }
     111
     112    TEST_F(IteratorTest, CanIterateOverInterfaceListWithClassIterator)
     113    {
     114        TestClass testClass;
     115        TestInterface testInterface;
     116
     117        size_t i = 0;
     118        for (Iterator<TestClass> it = ObjectList<TestInterface>::begin(); it != ObjectList<TestInterface>::end(); ++it)
     119        {
     120            ++i;
     121            if (i == 1u) EXPECT_EQ(&testClass, *it);
     122            if (i == 2u) EXPECT_EQ(NULL, *it);
     123        }
     124        EXPECT_EQ(2u, i);
     125    }
     126
     127    TEST_F(IteratorTest, CanIterateOverClassListWithInterfaceIterator)
     128    {
     129        TestClass testClass;
     130        TestInterface testInterface;
     131
     132        size_t i = 0;
     133        for (Iterator<TestInterface> it = ObjectList<TestClass>::begin(); it != ObjectList<TestClass>::end(); ++it)
     134        {
     135            ++i;
     136            if (i == 1u) EXPECT_EQ(&testClass, *it);
     137        }
     138        EXPECT_EQ(1u, i);
     139    }
    82140}
  • code/branches/cpp11_v2/test/core/object/ObjectListIteratorTest.cc

    r10624 r10736  
    33
    44#include "core/object/ObjectListIterator.h"
    5 #include "core/object/Listable.h"
     5#include "core/class/OrxonoxClass.h"
     6#include "core/class/OrxonoxInterface.h"
    67#include "core/CoreIncludes.h"
    78#include "core/module/ModuleInstance.h"
     
    1112    namespace
    1213    {
    13         class ListableTest : public Listable
     14        class TestInterface : virtual public OrxonoxInterface
    1415        {
    1516            public:
    16                 ListableTest() { RegisterObject(ListableTest); }
     17            TestInterface() { RegisterObject(TestInterface); }
     18        };
     19
     20        class TestClass : public OrxonoxClass, public TestInterface
     21        {
     22            public:
     23                TestClass() { RegisterObject(TestClass); }
    1724                MOCK_METHOD0(test, void());
    1825        };
    1926
    20         RegisterClassNoArgs(ListableTest);
     27        RegisterClassNoArgs(TestInterface);
     28        RegisterClassNoArgs(TestClass);
    2129
    2230        // Fixture
     
    4250    TEST_F(ObjectListIteratorTest, CanCreateIterator)
    4351    {
    44         ObjectListIterator<ListableTest> it;
     52        ObjectListIterator<TestClass> it;
    4553    }
    4654
    4755    TEST_F(ObjectListIteratorTest, CanAssignIterator)
    4856    {
    49         ObjectListIterator<ListableTest> it = ObjectList<ListableTest>::begin();
     57        ObjectListIterator<TestClass> it = ObjectList<TestClass>::begin();
    5058    }
    5159
     
    5361    {
    5462        size_t i = 0;
    55         for (ObjectListIterator<ListableTest> it = ObjectList<ListableTest>::begin(); it != ObjectList<ListableTest>::end(); ++it)
     63        for (ObjectListIterator<TestClass> it = ObjectList<TestClass>::begin(); it != ObjectList<TestClass>::end(); ++it)
    5664            ++i;
    5765        EXPECT_EQ(0u, i);
     
    6068    TEST_F(ObjectListIteratorTest, CanIterateOverFullList)
    6169    {
    62         ListableTest test1;
    63         ListableTest test2;
    64         ListableTest test3;
     70        TestClass test1;
     71        TestClass test2;
     72        TestClass test3;
     73        TestInterface interface;
    6574
    6675        size_t i = 0;
    67         for (ObjectListIterator<ListableTest> it = ObjectList<ListableTest>::begin(); it != ObjectList<ListableTest>::end(); ++it)
     76        for (ObjectListIterator<TestClass> it = ObjectList<TestClass>::begin(); it != ObjectList<TestClass>::end(); ++it)
    6877        {
    6978            ++i;
     
    7786    TEST_F(ObjectListIteratorTest, CanIterateReverseOverFullList)
    7887    {
    79         ListableTest test1;
    80         ListableTest test2;
    81         ListableTest test3;
     88        TestClass test1;
     89        TestClass test2;
     90        TestClass test3;
     91        TestInterface interface;
    8292
    8393        size_t i = 0;
    84         for (ObjectListIterator<ListableTest> it = ObjectList<ListableTest>::rbegin(); it != ObjectList<ListableTest>::rend(); --it)
     94        for (ObjectListIterator<TestClass> it = ObjectList<TestClass>::rbegin(); it != ObjectList<TestClass>::rend(); --it)
    8595        {
    8696            ++i;
     
    94104    TEST_F(ObjectListIteratorTest, CanCallObjects)
    95105    {
    96         ListableTest test1;
    97         ListableTest test2;
    98         ListableTest test3;
     106        TestClass test1;
     107        TestClass test2;
     108        TestClass test3;
     109        TestInterface interface;
    99110
    100111        EXPECT_CALL(test1, test());
     
    102113        EXPECT_CALL(test3, test());
    103114
    104         for (ObjectListIterator<ListableTest> it = ObjectList<ListableTest>::begin(); it != ObjectList<ListableTest>::end(); ++it)
     115        for (ObjectListIterator<TestClass> it = ObjectList<TestClass>::begin(); it != ObjectList<TestClass>::end(); ++it)
    105116            it->test();
    106117    }
Note: See TracChangeset for help on using the changeset viewer.