[Orxonox-commit 4937] r9606 - in code/branches/core6: src/libraries/core src/libraries/core/class src/libraries/core/object test/core/object

landauf at orxonox.net landauf at orxonox.net
Sat Mar 30 19:26:54 CET 2013


Author: landauf
Date: 2013-03-30 19:26:54 +0100 (Sat, 30 Mar 2013)
New Revision: 9606

Modified:
   code/branches/core6/src/libraries/core/ClassTreeMask.cc
   code/branches/core6/src/libraries/core/class/Identifier.cc
   code/branches/core6/src/libraries/core/class/Identifier.h
   code/branches/core6/src/libraries/core/object/Context.cc
   code/branches/core6/src/libraries/core/object/Context.h
   code/branches/core6/src/libraries/core/object/Listable.cc
   code/branches/core6/src/libraries/core/object/Listable.h
   code/branches/core6/src/libraries/core/object/ObjectList.h
   code/branches/core6/src/libraries/core/object/ObjectListBase.cc
   code/branches/core6/src/libraries/core/object/ObjectListBase.h
   code/branches/core6/test/core/object/ListableTest.cc
Log:
object lists are now stored in a Context object instead of Identifiers

Modified: code/branches/core6/src/libraries/core/ClassTreeMask.cc
===================================================================
--- code/branches/core6/src/libraries/core/ClassTreeMask.cc	2013-03-30 14:38:56 UTC (rev 9605)
+++ code/branches/core6/src/libraries/core/ClassTreeMask.cc	2013-03-30 18:26:54 UTC (rev 9606)
@@ -848,7 +848,7 @@
 
         // If there is a first subclass, move the object-iterator to the first object of this class. Else go to the end
         if (this->subclassIterator_ != this->subclasses_.end())
-            this->objectIterator_ = this->subclassIterator_->first->getObjects()->begin();
+            this->objectIterator_ = Context::getRootContext()->getObjectList(this->subclassIterator_->first)->begin();
         else
             this->objectIterator_ = ObjectList<BaseObject>::end();
 
@@ -880,7 +880,7 @@
 
                     // Check if there really is a next class. If yes, move the object-iterator to the first object
                     if (this->subclassIterator_ != this->subclasses_.end())
-                        this->objectIterator_ = this->subclassIterator_->first->getObjects()->begin();
+                        this->objectIterator_ = Context::getRootContext()->getObjectList(this->subclassIterator_->first)->begin();
                     else
                         return (*this);
                 }

Modified: code/branches/core6/src/libraries/core/class/Identifier.cc
===================================================================
--- code/branches/core6/src/libraries/core/class/Identifier.cc	2013-03-30 14:38:56 UTC (rev 9605)
+++ code/branches/core6/src/libraries/core/class/Identifier.cc	2013-03-30 18:26:54 UTC (rev 9606)
@@ -51,8 +51,6 @@
     Identifier::Identifier()
         : classID_(IdentifierManager::classIDCounter_s++)
     {
-        this->objects_ = new ObjectListBase();
-
         this->bCreatedOneObject_ = false;
         this->bSetName_ = false;
         this->factory_ = 0;
@@ -69,8 +67,6 @@
     */
     Identifier::~Identifier()
     {
-        delete this->objects_;
-
         if (this->factory_)
             delete this->factory_;
 

Modified: code/branches/core6/src/libraries/core/class/Identifier.h
===================================================================
--- code/branches/core6/src/libraries/core/class/Identifier.h	2013-03-30 14:38:56 UTC (rev 9605)
+++ code/branches/core6/src/libraries/core/class/Identifier.h	2013-03-30 18:26:54 UTC (rev 9606)
@@ -58,14 +58,6 @@
     OrxonoxClass* other = object->getIdentifier()->fabricate(0);                // fabricates a new instance of MyClass
 
 
-    // iterate through all objects of type MyClass:
-    ObjectListBase* objects = object->getIdentifier()->getObjects();            // get a pointer to the object-list
-    int count;
-    for (Iterator<MyClass> it = objects.begin(); it != objects.end(); ++it)     // iterate through the objects
-        ++count;
-    orxout() << count << endl;                                                  // prints "2" because we created 2 instances of MyClass so far
-
-
     // test the class hierarchy
     object->getIdentifier()->isA(Class(MyClass));                               // returns true
     object->isA(Class(MyClass));                                                // returns true (short version)
@@ -90,7 +82,8 @@
 
 #include "util/Output.h"
 #include "core/object/ObjectList.h"
-#include "core/object/ObjectListBase.h"
+#include "core/object/Listable.h"
+#include "core/object/Context.h"
 #include "IdentifierManager.h"
 #include "Super.h"
 
@@ -126,9 +119,6 @@
             /// Returns the unique ID of the class.
             ORX_FORCEINLINE unsigned int getClassID() const { return this->classID_; }
 
-            /// Returns the list of all existing objects of this class.
-            inline ObjectListBase* getObjects() const { return this->objects_; }
-
             /// Sets the Factory.
             void setFactory(Factory* factory);
             /// Returns true if the Identifier has a Factory.
@@ -231,8 +221,6 @@
             /// Returns the direct children of the class the Identifier belongs to.
             inline std::set<const Identifier*>& getDirectChildrenIntern() const { return this->directChildren_; }
 
-            ObjectListBase* objects_;                                      //!< The list of all objects of this class
-
         private:
             void initialize(std::set<const Identifier*>* parents);
 
@@ -423,10 +411,9 @@
      * @brief Only adds the object to the object list if is a @ref Listable
      */
     template <class T>
-    void ClassIdentifier<T>::addObjectToList(T* object, Listable*)
+    void ClassIdentifier<T>::addObjectToList(T* object, Listable* listable)
     {
-        orxout(verbose, context::object_list) << "Added object to " << this->getName() << "-list." << endl;
-        object->elements_.push_back(this->objects_->add(object));
+        listable->getContext()->addObject(object);
     }
 
     template <class T>

Modified: code/branches/core6/src/libraries/core/object/Context.cc
===================================================================
--- code/branches/core6/src/libraries/core/object/Context.cc	2013-03-30 14:38:56 UTC (rev 9605)
+++ code/branches/core6/src/libraries/core/object/Context.cc	2013-03-30 18:26:54 UTC (rev 9606)
@@ -32,6 +32,7 @@
 */
 
 #include "Context.h"
+#include "core/class/Identifier.h"
 
 namespace orxonox
 {
@@ -48,4 +49,14 @@
         static Context rootContext(NULL);
         return &rootContext;
     }
+
+    ObjectListBase* Context::getObjectList(const Identifier* identifier)
+    {
+        unsigned int classID = identifier->getClassID();
+        if (classID >= this->objectLists_.size())
+            this->objectLists_.resize(classID + 1);
+        if (!this->objectLists_[classID])
+            this->objectLists_[classID] = new ObjectListBase();
+        return this->objectLists_[classID];
+    }
 }

Modified: code/branches/core6/src/libraries/core/object/Context.h
===================================================================
--- code/branches/core6/src/libraries/core/object/Context.h	2013-03-30 14:38:56 UTC (rev 9605)
+++ code/branches/core6/src/libraries/core/object/Context.h	2013-03-30 18:26:54 UTC (rev 9606)
@@ -36,21 +36,41 @@
 
 #include "core/CorePrereqs.h"
 
+#include <vector>
+
+#include "ObjectListBase.h"
+
 namespace orxonox
 {
     class _CoreExport Context
     {
         public:
+            static Context* getRootContext();
+
             Context(Context* context);
             virtual ~Context();
 
             inline Context* getParentContext() const
                 { return this->parentContext_; }
 
-            static Context* getRootContext();
+            ObjectListBase* getObjectList(const Identifier* identifier);
 
+            template <class T>
+            inline ObjectListBase* getObjectList()
+                { return this->getObjectList(ClassIdentifier<T>::getIdentifier()); }
+
+            template <class T>
+            inline void addObject(T* object)
+            {
+                ObjectListBaseElement* element = this->getObjectList<T>()->add(object);
+                object->elements_.push_back(element);
+                if (this->getParentContext())
+                    this->getParentContext()->addObject(object);
+            }
+
         private:
             Context* parentContext_;
+            std::vector<ObjectListBase*> objectLists_;
     };
 }
 

Modified: code/branches/core6/src/libraries/core/object/Listable.cc
===================================================================
--- code/branches/core6/src/libraries/core/object/Listable.cc	2013-03-30 14:38:56 UTC (rev 9605)
+++ code/branches/core6/src/libraries/core/object/Listable.cc	2013-03-30 18:26:54 UTC (rev 9606)
@@ -33,6 +33,7 @@
 
 #include "Listable.h"
 #include "ObjectListBase.h"
+#include "Context.h"
 
 namespace orxonox
 {
@@ -41,6 +42,7 @@
     */
     Listable::Listable()
     {
+        this->context_ = Context::getRootContext();
         this->elements_.reserve(6);
     }
 

Modified: code/branches/core6/src/libraries/core/object/Listable.h
===================================================================
--- code/branches/core6/src/libraries/core/object/Listable.h	2013-03-30 14:38:56 UTC (rev 9605)
+++ code/branches/core6/src/libraries/core/object/Listable.h	2013-03-30 18:26:54 UTC (rev 9606)
@@ -48,8 +48,7 @@
     */
     class _CoreExport Listable : virtual public Identifiable
     {
-        template <class T>
-        friend class ClassIdentifier;
+        friend class Context;
 
         public:
             Listable();
@@ -57,7 +56,13 @@
 
             void unregisterObject();
 
+            inline void setContext(Context* context)
+                { this->context_ = context; }
+            inline Context* getContext() const
+                { return this->context_; }
+
         private:
+            Context* context_;                             //!< The object will register itself in the object lists of this context
             std::vector<ObjectListBaseElement*> elements_; //!< The corresponding ObjectListElements in all object lists the object is registered in
     };
 }

Modified: code/branches/core6/src/libraries/core/object/ObjectList.h
===================================================================
--- code/branches/core6/src/libraries/core/object/ObjectList.h	2013-03-30 14:38:56 UTC (rev 9605)
+++ code/branches/core6/src/libraries/core/object/ObjectList.h	2013-03-30 18:26:54 UTC (rev 9606)
@@ -48,6 +48,7 @@
 
 #include "ObjectListBase.h"
 #include "ObjectListIterator.h"
+#include "Context.h"
 
 namespace orxonox
 {
@@ -70,34 +71,34 @@
             /// Returns the size of the list
             inline static size_t size()
             {
-                return ClassIdentifier<T>::getIdentifier()->getObjects()->size();
+                return Context::getRootContext()->getObjectList<T>()->size();
             }
 
             /// Returns an Iterator to the first element in the list.
             inline static ObjectListElement<T>* begin()
             {
-                ObjectListBase* list = ClassIdentifier<T>::getIdentifier()->getObjects();
+                ObjectListBase* list = Context::getRootContext()->getObjectList<T>();
                 return static_cast<ObjectListElement<T>*>(list->begin());
             }
 
             /// Returns an Iterator to the element after the last element in the list.
             inline static ObjectListElement<T>* end()
             {
-                ObjectListBase* list = ClassIdentifier<T>::getIdentifier()->getObjects();
+                ObjectListBase* list = Context::getRootContext()->getObjectList<T>();
                 return static_cast<ObjectListElement<T>*>(list->end());
             }
 
             /// Returns an Iterator to the last element in the list.
             inline static ObjectListElement<T>* rbegin()
             {
-                ObjectListBase* list = ClassIdentifier<T>::getIdentifier()->getObjects();
+                ObjectListBase* list = Context::getRootContext()->getObjectList<T>();
                 return static_cast<ObjectListElement<T>*>(list->rbegin());
             }
 
             /// Returns an Iterator to the element before the first element in the list.
             inline static ObjectListElement<T>* rend()
             {
-                ObjectListBase* list = ClassIdentifier<T>::getIdentifier()->getObjects();
+                ObjectListBase* list = Context::getRootContext()->getObjectList<T>();
                 return static_cast<ObjectListElement<T>*>(list->rend());
             }
     };

Modified: code/branches/core6/src/libraries/core/object/ObjectListBase.cc
===================================================================
--- code/branches/core6/src/libraries/core/object/ObjectListBase.cc	2013-03-30 14:38:56 UTC (rev 9605)
+++ code/branches/core6/src/libraries/core/object/ObjectListBase.cc	2013-03-30 18:26:54 UTC (rev 9606)
@@ -88,6 +88,9 @@
             return;
         }
 
+        if (element->objectBase_)
+            orxout(verbose, context::object_list) << "Added object to " << element->objectBase_->getIdentifier()->getName() << "-list." << endl;
+
         if (!this->last_)
         {
             // If the list is empty

Modified: code/branches/core6/src/libraries/core/object/ObjectListBase.h
===================================================================
--- code/branches/core6/src/libraries/core/object/ObjectListBase.h	2013-03-30 14:38:56 UTC (rev 9605)
+++ code/branches/core6/src/libraries/core/object/ObjectListBase.h	2013-03-30 18:26:54 UTC (rev 9606)
@@ -123,13 +123,13 @@
             size_t size() const { return this->size_; }
 
             /// Returns a pointer to the first element in the list. Works only with Iterator.
-            inline ObjectListBaseElement* begin() { return this->first_; }
+            inline ObjectListBaseElement* begin() const { return this->first_; }
             /// Returns a pointer to the element after the last element in the list. Works only with Iterator.
-            inline ObjectListBaseElement* end() { return 0; }
+            inline ObjectListBaseElement* end() const { return 0; }
             /// Returns a pointer to the last element in the list. Works only with Iterator.
-            inline ObjectListBaseElement* rbegin() { return this->last_; }
+            inline ObjectListBaseElement* rbegin() const { return this->last_; }
             /// Returns a pointer to the element in front of the first element in the list. Works only with Iterator.
-            inline ObjectListBaseElement* rend() { return 0; }
+            inline ObjectListBaseElement* rend() const { return 0; }
 
             inline void registerRemovalListener(ObjectListElementRemovalListener* listener) { this->listeners_.push_back(listener); }
             inline void unregisterRemovalListener(ObjectListElementRemovalListener* listener)

Modified: code/branches/core6/test/core/object/ListableTest.cc
===================================================================
--- code/branches/core6/test/core/object/ListableTest.cc	2013-03-30 14:38:56 UTC (rev 9605)
+++ code/branches/core6/test/core/object/ListableTest.cc	2013-03-30 18:26:54 UTC (rev 9606)
@@ -53,6 +53,7 @@
 
     TEST(ListableTest, RemovesFromObjectList)
     {
+        EXPECT_EQ(0u, ObjectList<ListableTest>::size());
         {
             ListableTest test;
             EXPECT_EQ(1u, ObjectList<ListableTest>::size());
@@ -63,6 +64,8 @@
 
     TEST(ListableTest, RemovesFromAllObjectLists)
     {
+        EXPECT_EQ(0u, ObjectList<ListableTest>::size());
+        EXPECT_EQ(0u, ObjectList<ListableSubclassTest>::size());
         {
             ListableSubclassTest test;
             EXPECT_EQ(1u, ObjectList<ListableTest>::size());




More information about the Orxonox-commit mailing list