Changeset 9627
- Timestamp:
- Aug 9, 2013, 12:27:05 AM (11 years ago)
- Location:
- code/branches/core6
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core6/src/libraries/core/object/Listable.cc
r9610 r9627 80 80 void Listable::setContext(Context* context) 81 81 { 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 82 91 this->context_ = context; 83 for (size_t i = 0; i < this->elements_.size(); ++i)84 this->elements_[i]->changeContext(context);85 92 } 86 93 -
code/branches/core6/src/libraries/core/object/ObjectListBase.h
r9608 r9627 60 60 virtual ~ObjectListBaseElement() { this->removeFromList(); } 61 61 62 virtual void changeContext(Context* context) = 0;62 virtual void changeContext(Context* oldContext, Context* newContext) = 0; 63 63 64 64 ObjectListBaseElement* next_; //!< The next element in the list … … 82 82 ObjectListElement(T* object) : ObjectListBaseElement(static_cast<Listable*>(object)), object_(object) {} 83 83 84 virtual void changeContext(Context* context)84 virtual void changeContext(Context* oldContext, Context* newContext) 85 85 { 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 86 92 this->removeFromList(); 87 context->getObjectList<T>()->addElement(this);88 93 } 89 94 -
code/branches/core6/test/core/object/ListableTest.cc
r9608 r9627 79 79 } 80 80 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 81 115 TEST(ListableTest, CanChangeContext) 82 116 { 83 117 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 92 128 EXPECT_EQ(0u, newContext.getObjectList<ListableTest>()->size()); 93 129 EXPECT_FALSE(objectListContains<ListableTest>(&test, &newContext)); … … 97 133 test.setContext(&newContext); 98 134 135 // object was removed from root context 99 136 EXPECT_EQ(0u, rootContext->getObjectList<ListableTest>()->size()); 100 137 EXPECT_FALSE(objectListContains<ListableTest>(&test, rootContext)); … … 102 139 EXPECT_FALSE(objectListContains<ListableSubclassTest>(&test, rootContext)); 103 140 141 // object is now in new context 104 142 EXPECT_EQ(1u, newContext.getObjectList<ListableTest>()->size()); 105 143 EXPECT_TRUE(objectListContains<ListableTest>(&test, &newContext)); … … 107 145 EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &newContext)); 108 146 } 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 } 109 228 }
Note: See TracChangeset
for help on using the changeset viewer.