Changeset 9608 for code/branches/core6
- Timestamp:
- Mar 30, 2013, 10:40:48 PM (12 years ago)
- Location:
- code/branches/core6
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core6/src/libraries/core/object/Context.cc
r9606 r9608 47 47 /*static*/ Context* Context::getRootContext() 48 48 { 49 static Context rootContext (NULL);49 static Context rootContext; 50 50 return &rootContext; 51 51 } -
code/branches/core6/src/libraries/core/object/Context.h
r9606 r9608 39 39 #include <vector> 40 40 41 #include "ObjectListBase.h"42 43 41 namespace orxonox 44 42 { … … 48 46 static Context* getRootContext(); 49 47 50 Context(Context* context );48 Context(Context* context = NULL); 51 49 virtual ~Context(); 52 50 -
code/branches/core6/src/libraries/core/object/Listable.cc
r9606 r9608 64 64 this->elements_.clear(); 65 65 } 66 67 /** 68 * @brief Changes the context. 69 * The object is removed from the current context and added to the new one. This also applies to all object lists the object is registered in. 70 */ 71 void Listable::setContext(Context* context) 72 { 73 this->context_ = context; 74 for (size_t i = 0; i < this->elements_.size(); ++i) 75 this->elements_[i]->changeContext(context); 76 } 77 66 78 } -
code/branches/core6/src/libraries/core/object/Listable.h
r9606 r9608 57 57 void unregisterObject(); 58 58 59 inline void setContext(Context* context) 60 { this->context_ = context; } 59 void setContext(Context* context); 61 60 inline Context* getContext() const 62 61 { return this->context_; } -
code/branches/core6/src/libraries/core/object/ObjectListBase.cc
r9606 r9608 41 41 namespace orxonox 42 42 { 43 ObjectListBaseElement::~ObjectListBaseElement() 43 // ############################### 44 // ### ObjectListBaseElement ### 45 // ############################### 46 void ObjectListBaseElement::removeFromList() 44 47 { 45 48 if (this->list_) … … 47 50 } 48 51 52 // ############################### 53 // ### ObjectListBase ### 54 // ############################### 49 55 /** 50 56 @brief Constructor: Sets default values. -
code/branches/core6/src/libraries/core/object/ObjectListBase.h
r9606 r9608 42 42 #include "core/CorePrereqs.h" 43 43 #include <vector> 44 #include "Context.h" 44 45 45 46 namespace orxonox … … 57 58 */ 58 59 ObjectListBaseElement(Listable* object) : next_(0), prev_(0), objectBase_(object), list_(0) {} 59 ~ObjectListBaseElement(); 60 virtual ~ObjectListBaseElement() { this->removeFromList(); } 61 62 virtual void changeContext(Context* context) = 0; 60 63 61 64 ObjectListBaseElement* next_; //!< The next element in the list … … 63 66 Listable* objectBase_; //!< The object 64 67 ObjectListBase* list_; //!< The list 68 69 protected: 70 void removeFromList(); 65 71 }; 66 72 … … 75 81 public: 76 82 ObjectListElement(T* object) : ObjectListBaseElement(static_cast<Listable*>(object)), object_(object) {} 83 84 virtual void changeContext(Context* context) 85 { 86 this->removeFromList(); 87 context->getObjectList<T>()->addElement(this); 88 } 89 77 90 T* object_; //!< The object 78 91 }; -
code/branches/core6/test/core/object/ListableTest.cc
r9606 r9608 20 20 21 21 template <class T> 22 bool objectListContains(T* element )22 bool objectListContains(T* element, Context* context = Context::getRootContext()) 23 23 { 24 for (typename ObjectList<T>::iterator it = ObjectList<T>::begin(); it != ObjectList<T>::end(); ++it) 24 for (typename ObjectList<T>::iterator it = static_cast<ObjectListElement<T>*>(context->getObjectList<T>()->begin()); 25 it != static_cast<ObjectListElement<T>*>(context->getObjectList<T>()->end()); ++it) 25 26 if (*it == element) 26 27 return true; … … 77 78 EXPECT_EQ(0u, ObjectList<ListableSubclassTest>::size()); 78 79 } 80 81 TEST(ListableTest, CanChangeContext) 82 { 83 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 92 EXPECT_EQ(0u, newContext.getObjectList<ListableTest>()->size()); 93 EXPECT_FALSE(objectListContains<ListableTest>(&test, &newContext)); 94 EXPECT_EQ(0u, newContext.getObjectList<ListableSubclassTest>()->size()); 95 EXPECT_FALSE(objectListContains<ListableSubclassTest>(&test, &newContext)); 96 97 test.setContext(&newContext); 98 99 EXPECT_EQ(0u, rootContext->getObjectList<ListableTest>()->size()); 100 EXPECT_FALSE(objectListContains<ListableTest>(&test, rootContext)); 101 EXPECT_EQ(0u, rootContext->getObjectList<ListableSubclassTest>()->size()); 102 EXPECT_FALSE(objectListContains<ListableSubclassTest>(&test, rootContext)); 103 104 EXPECT_EQ(1u, newContext.getObjectList<ListableTest>()->size()); 105 EXPECT_TRUE(objectListContains<ListableTest>(&test, &newContext)); 106 EXPECT_EQ(1u, newContext.getObjectList<ListableSubclassTest>()->size()); 107 EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &newContext)); 108 } 79 109 } -
code/branches/core6/test/core/object/ObjectListBaseTest.cc
r9605 r9608 2 2 #include <gmock/gmock.h> 3 3 #include "core/object/ObjectListBase.h" 4 #include "core/class/Identifier.h" 4 5 5 6 namespace orxonox … … 23 24 { 24 25 ObjectListBase list; 25 ObjectListBaseElement* element = new ObjectList BaseElement(NULL);26 ObjectListBaseElement* element = new ObjectListElement<Listable>(NULL); 26 27 list.addElement(element); 27 28 EXPECT_EQ(1u, list.size()); … … 31 32 { 32 33 ObjectListBase list; 33 ObjectListBaseElement* element = new ObjectList BaseElement(NULL);34 ObjectListBaseElement* element = new ObjectListElement<Listable>(NULL); 34 35 list.addElement(element); 35 36 EXPECT_EQ(1u, list.size()); … … 51 52 EXPECT_EQ(0u, list.size()); 52 53 { 53 ObjectList BaseElementelement1(NULL);54 ObjectListElement<Listable> element1(NULL); 54 55 list.addElement(&element1); 55 56 EXPECT_EQ(1u, list.size()); 56 57 { 57 ObjectList BaseElementelement1(NULL);58 ObjectListElement<Listable> element1(NULL); 58 59 list.addElement(&element1); 59 60 EXPECT_EQ(2u, list.size()); 60 61 { 61 ObjectList BaseElementelement1(NULL);62 ObjectListElement<Listable> element1(NULL); 62 63 list.addElement(&element1); 63 64 EXPECT_EQ(3u, list.size()); … … 75 76 ObjectListElementRemovalListenerMock listener; 76 77 list.registerRemovalListener(&listener); 77 ObjectListBaseElement* element = new ObjectList BaseElement(NULL);78 ObjectListBaseElement* element = new ObjectListElement<Listable>(NULL); 78 79 list.addElement(element); 79 80 EXPECT_CALL(listener, removedElement(element));
Note: See TracChangeset
for help on using the changeset viewer.