[Orxonox-commit 1103] r5824 - code/branches/core5/src/libraries/core

landauf at orxonox.net landauf at orxonox.net
Mon Sep 28 17:50:31 CEST 2009


Author: landauf
Date: 2009-09-28 17:50:31 +0200 (Mon, 28 Sep 2009)
New Revision: 5824

Modified:
   code/branches/core5/src/libraries/core/OrxonoxClass.cc
   code/branches/core5/src/libraries/core/WeakPtr.h
Log:
added callback functionality to WeakPtr

Modified: code/branches/core5/src/libraries/core/OrxonoxClass.cc
===================================================================
--- code/branches/core5/src/libraries/core/OrxonoxClass.cc	2009-09-28 15:16:36 UTC (rev 5823)
+++ code/branches/core5/src/libraries/core/OrxonoxClass.cc	2009-09-28 15:50:31 UTC (rev 5824)
@@ -65,7 +65,7 @@
             
         // reset all weak pointers pointing to this object
         for (std::set<WeakPtr<OrxonoxClass>*>::iterator it = this->weakPointers_.begin(); it != this->weakPointers_.end(); )
-            (*(it++))->reset();
+            (*(it++))->objectDeleted();
     }
 
     /** @brief Deletes the object if no smart pointers point to this object. Otherwise schedules the object to be deleted as soon as possible. */

Modified: code/branches/core5/src/libraries/core/WeakPtr.h
===================================================================
--- code/branches/core5/src/libraries/core/WeakPtr.h	2009-09-28 15:16:36 UTC (rev 5823)
+++ code/branches/core5/src/libraries/core/WeakPtr.h	2009-09-28 15:50:31 UTC (rev 5824)
@@ -35,35 +35,38 @@
 
 #include <cassert>
 #include "OrxonoxClass.h"
+#include "Functor.h"
 
 namespace orxonox
 {
     template <class T>
     class WeakPtr
     {
+        friend class OrxonoxClass;
+        
         public:
-            inline WeakPtr() : pointer_(0), base_(0)
+            inline WeakPtr() : pointer_(0), base_(0), callback_(0)
             {
             }
 
-            inline WeakPtr(int) : pointer_(0), base_(0)
+            inline WeakPtr(int) : pointer_(0), base_(0), callback_(0)
             {
             }
 
-            inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer)
+            inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer), callback_(0)
             {
                 if (this->base_)
                     this->base_->registerWeakPtr(this);
             }
 
-            inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_)
+            inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_), callback_(0)
             {
                 if (this->base_)
                     this->base_->registerWeakPtr(this);
             }
 
             template <class O>
-            inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.base_)
+            inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.base_), callback_(0)
             {
                 if (this->base_)
                     this->base_->registerWeakPtr(this);
@@ -73,6 +76,9 @@
             {
                 if (this->base_)
                     this->base_->unregisterWeakPtr(this);
+                if (this->callback_)
+                    delete this->callback_;
+                    
             }
             
             inline const WeakPtr& operator=(int)
@@ -150,10 +156,28 @@
             {
                 WeakPtr().swap(*this);
             }
+            
+            inline void addCallback(Functor* callback)
+            {
+                this->callback_ = callback;
+            }
+            
+            inline Functor* getFunctor() const
+            {
+                return this->callback_;
+            }
 
         private:
+            inline void objectDeleted()
+            {
+                this->reset();
+                if (this->callback_)
+                    (*this->callback_)();
+            }
+        
             T* pointer_;
             OrxonoxClass* base_;
+            Functor* callback_;
     };
 
     template <class T>




More information about the Orxonox-commit mailing list