Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9627 for code/branches/core6


Ignore:
Timestamp:
Aug 9, 2013, 12:27:05 AM (11 years ago)
Author:
landauf
Message:

fixed bug: Listable::setContext didn't work properly with sub-contexts (contexts whose root-context is != NULL)

Location:
code/branches/core6
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core6/src/libraries/core/object/Listable.cc

    r9610 r9627  
    8080    void Listable::setContext(Context* context)
    8181    {
     82        std::vector<ObjectListBaseElement*> copy = this->elements_;
     83        this->elements_.clear();
     84
     85        for (size_t i = 0; i < copy.size(); ++i)
     86        {
     87            copy[i]->changeContext(this->context_, context);
     88            delete copy[i];
     89        }
     90
    8291        this->context_ = context;
    83         for (size_t i = 0; i < this->elements_.size(); ++i)
    84             this->elements_[i]->changeContext(context);
    8592    }
    8693
  • code/branches/core6/src/libraries/core/object/ObjectListBase.h

    r9608 r9627  
    6060            virtual ~ObjectListBaseElement() { this->removeFromList(); }
    6161
    62             virtual void changeContext(Context* context) = 0;
     62            virtual void changeContext(Context* oldContext, Context* newContext) = 0;
    6363
    6464            ObjectListBaseElement* next_;       //!< The next element in the list
     
    8282            ObjectListElement(T* object) : ObjectListBaseElement(static_cast<Listable*>(object)), object_(object) {}
    8383
    84             virtual void changeContext(Context* context)
     84            virtual void changeContext(Context* oldContext, Context* newContext)
    8585            {
     86                // add object to new context, but only if this element belongs exactly to the old context (and not to a sub-context to avoid re-adding objects
     87                // multiple times if they are in multiple contexts)
     88                if (oldContext->getObjectList<T>() == this->list_)
     89                    newContext->addObject(this->object_);
     90
     91                // remove from old list
    8692                this->removeFromList();
    87                 context->getObjectList<T>()->addElement(this);
    8893            }
    8994
  • code/branches/core6/test/core/object/ListableTest.cc

    r9608 r9627  
    7979    }
    8080
     81    TEST(ListableTest, CanAddObjectToContext)
     82    {
     83        Context context(NULL);
     84        ListableSubclassTest test;
     85
     86        test.setContext(&context);
     87
     88        // object is in new context
     89        EXPECT_EQ(1u, context.getObjectList<ListableTest>()->size());
     90        EXPECT_TRUE(objectListContains<ListableTest>(&test, &context));
     91        EXPECT_EQ(1u, context.getObjectList<ListableSubclassTest>()->size());
     92        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &context));
     93    }
     94
     95    TEST(ListableTest, CanAddObjectToSubContext)
     96    {
     97        Context baseContext(NULL);
     98        Context subContext(&baseContext);
     99        ListableSubclassTest test;
     100
     101        test.setContext(&subContext);
     102
     103        // object is in both contexts
     104        EXPECT_EQ(1u, baseContext.getObjectList<ListableTest>()->size());
     105        EXPECT_TRUE(objectListContains<ListableTest>(&test, &baseContext));
     106        EXPECT_EQ(1u, baseContext.getObjectList<ListableSubclassTest>()->size());
     107        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &baseContext));
     108
     109        EXPECT_EQ(1u, subContext.getObjectList<ListableTest>()->size());
     110        EXPECT_TRUE(objectListContains<ListableTest>(&test, &subContext));
     111        EXPECT_EQ(1u, subContext.getObjectList<ListableSubclassTest>()->size());
     112        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &subContext));
     113    }
     114
    81115    TEST(ListableTest, CanChangeContext)
    82116    {
    83117        Context* rootContext = Context::getRootContext();
    84         Context newContext;
    85         ListableSubclassTest test;
    86 
    87         EXPECT_EQ(1u, rootContext->getObjectList<ListableTest>()->size());
    88         EXPECT_TRUE(objectListContains<ListableTest>(&test, rootContext));
    89         EXPECT_EQ(1u, rootContext->getObjectList<ListableSubclassTest>()->size());
    90         EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, rootContext));
    91 
     118        Context newContext(NULL);
     119        ListableSubclassTest test;
     120
     121        // object is in root context
     122        EXPECT_EQ(1u, rootContext->getObjectList<ListableTest>()->size());
     123        EXPECT_TRUE(objectListContains<ListableTest>(&test, rootContext));
     124        EXPECT_EQ(1u, rootContext->getObjectList<ListableSubclassTest>()->size());
     125        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, rootContext));
     126
     127        // object is NOT in new context
    92128        EXPECT_EQ(0u, newContext.getObjectList<ListableTest>()->size());
    93129        EXPECT_FALSE(objectListContains<ListableTest>(&test, &newContext));
     
    97133        test.setContext(&newContext);
    98134
     135        // object was removed from root context
    99136        EXPECT_EQ(0u, rootContext->getObjectList<ListableTest>()->size());
    100137        EXPECT_FALSE(objectListContains<ListableTest>(&test, rootContext));
     
    102139        EXPECT_FALSE(objectListContains<ListableSubclassTest>(&test, rootContext));
    103140
     141        // object is now in new context
    104142        EXPECT_EQ(1u, newContext.getObjectList<ListableTest>()->size());
    105143        EXPECT_TRUE(objectListContains<ListableTest>(&test, &newContext));
     
    107145        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &newContext));
    108146    }
     147
     148    TEST(ListableTest, CanChangeToSubContext)
     149    {
     150        Context* rootContext = Context::getRootContext();
     151        Context subContext(rootContext);
     152        ListableSubclassTest test;
     153
     154        // object is in root context
     155        EXPECT_EQ(1u, rootContext->getObjectList<ListableTest>()->size());
     156        EXPECT_TRUE(objectListContains<ListableTest>(&test, rootContext));
     157        EXPECT_EQ(1u, rootContext->getObjectList<ListableSubclassTest>()->size());
     158        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, rootContext));
     159
     160        // object is NOT in sub context
     161        EXPECT_EQ(0u, subContext.getObjectList<ListableTest>()->size());
     162        EXPECT_FALSE(objectListContains<ListableTest>(&test, &subContext));
     163        EXPECT_EQ(0u, subContext.getObjectList<ListableSubclassTest>()->size());
     164        EXPECT_FALSE(objectListContains<ListableSubclassTest>(&test, &subContext));
     165
     166        test.setContext(&subContext);
     167
     168        // object is still in root context
     169        EXPECT_EQ(1u, rootContext->getObjectList<ListableTest>()->size());
     170        EXPECT_TRUE(objectListContains<ListableTest>(&test, rootContext));
     171        EXPECT_EQ(1u, rootContext->getObjectList<ListableSubclassTest>()->size());
     172        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, rootContext));
     173
     174        // object is now also in sub context
     175        EXPECT_EQ(1u, subContext.getObjectList<ListableTest>()->size());
     176        EXPECT_TRUE(objectListContains<ListableTest>(&test, &subContext));
     177        EXPECT_EQ(1u, subContext.getObjectList<ListableSubclassTest>()->size());
     178        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &subContext));
     179    }
     180
     181    TEST(ListableTest, CanChangeFromSubcontextToSubContext)
     182    {
     183        Context* rootContext = Context::getRootContext();
     184        Context subContext1(rootContext);
     185        Context subContext2(rootContext);
     186        ListableSubclassTest test;
     187
     188        test.setContext(&subContext1);
     189
     190        // object is in root context
     191        EXPECT_EQ(1u, rootContext->getObjectList<ListableTest>()->size());
     192        EXPECT_TRUE(objectListContains<ListableTest>(&test, rootContext));
     193        EXPECT_EQ(1u, rootContext->getObjectList<ListableSubclassTest>()->size());
     194        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, rootContext));
     195
     196        // object is also in sub context 1
     197        EXPECT_EQ(1u, subContext1.getObjectList<ListableTest>()->size());
     198        EXPECT_TRUE(objectListContains<ListableTest>(&test, &subContext1));
     199        EXPECT_EQ(1u, subContext1.getObjectList<ListableSubclassTest>()->size());
     200        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &subContext1));
     201
     202        // object is NOT in sub context 2
     203        EXPECT_EQ(0u, subContext2.getObjectList<ListableTest>()->size());
     204        EXPECT_FALSE(objectListContains<ListableTest>(&test, &subContext2));
     205        EXPECT_EQ(0u, subContext2.getObjectList<ListableSubclassTest>()->size());
     206        EXPECT_FALSE(objectListContains<ListableSubclassTest>(&test, &subContext2));
     207
     208        test.setContext(&subContext2);
     209
     210        // object is still in root context
     211        EXPECT_EQ(1u, rootContext->getObjectList<ListableTest>()->size());
     212        EXPECT_TRUE(objectListContains<ListableTest>(&test, rootContext));
     213        EXPECT_EQ(1u, rootContext->getObjectList<ListableSubclassTest>()->size());
     214        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, rootContext));
     215
     216        // object was removed from sub context 1
     217        EXPECT_EQ(0u, subContext1.getObjectList<ListableTest>()->size());
     218        EXPECT_FALSE(objectListContains<ListableTest>(&test, &subContext1));
     219        EXPECT_EQ(0u, subContext1.getObjectList<ListableSubclassTest>()->size());
     220        EXPECT_FALSE(objectListContains<ListableSubclassTest>(&test, &subContext1));
     221
     222        // object is now in sub context 2
     223        EXPECT_EQ(1u, subContext2.getObjectList<ListableTest>()->size());
     224        EXPECT_TRUE(objectListContains<ListableTest>(&test, &subContext2));
     225        EXPECT_EQ(1u, subContext2.getObjectList<ListableSubclassTest>()->size());
     226        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &subContext2));
     227    }
    109228}
Note: See TracChangeset for help on using the changeset viewer.