[Orxonox-commit 3156] r7849 - code/trunk/src/libraries/core

landauf at orxonox.net landauf at orxonox.net
Thu Feb 10 21:52:23 CET 2011


Author: landauf
Date: 2011-02-10 21:52:23 +0100 (Thu, 10 Feb 2011)
New Revision: 7849

Modified:
   code/trunk/src/libraries/core/CorePrereqs.h
   code/trunk/src/libraries/core/OrxonoxClass.cc
   code/trunk/src/libraries/core/OrxonoxClass.h
   code/trunk/src/libraries/core/WeakPtr.h
Log:
Added DestructionListener, a class which gets notified if a certain object (of type OrxonoxClass) is destroyed. So far only WeakPtr was able to do this, now every class that inherits from DestructionListener can do so.

Modified: code/trunk/src/libraries/core/CorePrereqs.h
===================================================================
--- code/trunk/src/libraries/core/CorePrereqs.h	2011-02-10 17:05:08 UTC (rev 7848)
+++ code/trunk/src/libraries/core/CorePrereqs.h	2011-02-10 20:52:23 UTC (rev 7849)
@@ -137,6 +137,7 @@
     class ConfigFileSection;
     class ConfigValueContainer;
     class Core;
+    class DestructionListener;
     class DynLib;
     class DynLibManager;
     struct Event;

Modified: code/trunk/src/libraries/core/OrxonoxClass.cc
===================================================================
--- code/trunk/src/libraries/core/OrxonoxClass.cc	2011-02-10 17:05:08 UTC (rev 7848)
+++ code/trunk/src/libraries/core/OrxonoxClass.cc	2011-02-10 20:52:23 UTC (rev 7849)
@@ -36,7 +36,6 @@
 #include <cassert>
 #include "MetaObjectList.h"
 #include "Identifier.h"
-#include "WeakPtr.h"
 
 namespace orxonox
 {
@@ -55,7 +54,7 @@
     }
 
     /**
-        @brief Destructor: Removes the object from the object-lists, notifies all @ref WeakPtr "weak pointers" that this object is being deleted.
+        @brief Destructor: Removes the object from the object-lists, notifies all DestructionListener (for example @ref WeakPtr "weak pointers") that this object is being deleted.
     */
     OrxonoxClass::~OrxonoxClass()
     {
@@ -70,8 +69,8 @@
         if (this->parents_)
             delete this->parents_;
 
-        // reset all weak pointers pointing to this object
-        for (std::set<WeakPtr<OrxonoxClass>*>::iterator it = this->weakPointers_.begin(); it != this->weakPointers_.end(); )
+        // notify all destruction listeners
+        for (std::set<DestructionListener*>::iterator it = this->destructionListeners_.begin(); it != this->destructionListeners_.end(); )
             (*(it++))->objectDeleted();
     }
 

Modified: code/trunk/src/libraries/core/OrxonoxClass.h
===================================================================
--- code/trunk/src/libraries/core/OrxonoxClass.h	2011-02-10 17:05:08 UTC (rev 7848)
+++ code/trunk/src/libraries/core/OrxonoxClass.h	2011-02-10 20:52:23 UTC (rev 7849)
@@ -73,8 +73,7 @@
         template <class T>
         friend class SmartPtr;
 
-        template <class T>
-        friend class WeakPtr;
+        friend class DestructionListener;
 
         public:
             OrxonoxClass();
@@ -168,28 +167,41 @@
                     this->destroy();
             }
 
-            /// Register a weak pointer which points to this object.
-            template <class T>
-            inline void registerWeakPtr(WeakPtr<T>* pointer)
-                { this->weakPointers_.insert(reinterpret_cast<WeakPtr<OrxonoxClass>*>(pointer)); }
-            /// Unegister a weak pointer which pointed to this object before.
-            template <class T>
-            inline void unregisterWeakPtr(WeakPtr<T>* pointer)
-                { this->weakPointers_.erase(reinterpret_cast<WeakPtr<OrxonoxClass>*>(pointer)); }
+            /// Register a destruction listener (for example a weak pointer which points to this object).
+            inline void registerDestructionListener(DestructionListener* pointer)
+                { this->destructionListeners_.insert(pointer); }
+            /// Unegister a destruction listener (for example a weak pointer which pointed to this object before).
+            inline void unregisterDestructionListener(DestructionListener* pointer)
+                { this->destructionListeners_.erase(pointer); }
 
-            Identifier* identifier_;                            //!< The Identifier of the object
-            std::set<const Identifier*>* parents_;              //!< List of all parents of the object
-            MetaObjectList* metaList_;                          //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in
-            int referenceCount_;                                //!< Counts the references from smart pointers to this object
-            bool requestedDestruction_;                         //!< Becomes true after someone called delete on this object
-            std::set<WeakPtr<OrxonoxClass>*> weakPointers_;     //!< All weak pointers which point to this object (and like to get notified if it dies)
+            Identifier* identifier_;                                //!< The Identifier of the object
+            std::set<const Identifier*>* parents_;                  //!< List of all parents of the object
+            MetaObjectList* metaList_;                              //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in
+            int referenceCount_;                                    //!< Counts the references from smart pointers to this object
+            bool requestedDestruction_;                             //!< Becomes true after someone called delete on this object
+            std::set<DestructionListener*> destructionListeners_;   //!< All destruction listeners (for example weak pointers which point to this object and like to get notified if it dies)
 
             /// 'Fast map' that holds this-pointers of all derived types
             std::vector<std::pair<unsigned int, void*> > objectPointers_;
     };
 
+    /**
+        @brief This listener is used to inform weak pointers if an object of type OrxonoxClass gets destroyed.
+    */
+    class _CoreExport DestructionListener
+    {
+        friend class OrxonoxClass;
+
+        protected:
+            inline void registerAsDestructionListener(OrxonoxClass* object)
+                { object->registerDestructionListener(this); }
+            inline void unregisterAsDestructionListener(OrxonoxClass* object)
+                { object->unregisterDestructionListener(this); }
+
+            virtual void objectDeleted() = 0;
+    };
+
     SUPER_FUNCTION(11, OrxonoxClass, clone, false);
-
 }
 
 #endif /* _OrxonoxClass_H__ */

Modified: code/trunk/src/libraries/core/WeakPtr.h
===================================================================
--- code/trunk/src/libraries/core/WeakPtr.h	2011-02-10 17:05:08 UTC (rev 7848)
+++ code/trunk/src/libraries/core/WeakPtr.h	2011-02-10 20:52:23 UTC (rev 7849)
@@ -95,10 +95,8 @@
         @see See @ref WeakPtrExample "this description" for more information and an example.
     */
     template <class T>
-    class WeakPtr
+    class WeakPtr : public DestructionListener
     {
-        friend class OrxonoxClass;
-
         public:
             /// Constructor: Initializes the weak pointer with a null pointer.
             inline WeakPtr() : pointer_(0), base_(0), callback_(0)
@@ -114,14 +112,14 @@
             inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer), callback_(0)
             {
                 if (this->base_)
-                    this->base_->registerWeakPtr(this);
+                    this->registerAsDestructionListener(this->base_);
             }
 
             /// Copy-constructor
             inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_), callback_(0)
             {
                 if (this->base_)
-                    this->base_->registerWeakPtr(this);
+                    this->registerAsDestructionListener(this->base_);
             }
 
             /// Copy-constructor for weak pointers to objects of another class.
@@ -129,14 +127,14 @@
             inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.base_), callback_(0)
             {
                 if (this->base_)
-                    this->base_->registerWeakPtr(this);
+                    this->registerAsDestructionListener(this->base_);
             }
 
             /// Destructor
             inline ~WeakPtr()
             {
                 if (this->base_)
-                    this->base_->unregisterWeakPtr(this);
+                    this->unregisterAsDestructionListener(this->base_);
 
             }
 
@@ -211,9 +209,9 @@
             inline void swap(WeakPtr& other)
             {
                 if (this->base_)
-                    this->base_->unregisterWeakPtr(this);
+                    this->unregisterAsDestructionListener(this->base_);
                 if (other.base_)
-                    other.base_->unregisterWeakPtr(&other);
+                    other.unregisterAsDestructionListener(other.base_);
 
                 {
                     T* temp = this->pointer_;
@@ -227,9 +225,9 @@
                 }
 
                 if (this->base_)
-                    this->base_->registerWeakPtr(this);
+                    this->registerAsDestructionListener(this->base_);
                 if (other.base_)
-                    other.base_->registerWeakPtr(&other);
+                    other.registerAsDestructionListener(other.base_);
             }
 
             /// Resets the weak pointer (equivalent to assigning a NULL pointer).




More information about the Orxonox-commit mailing list