[Orxonox-commit 5759] r10419 - in code/branches/core7/src/libraries/core: . object

landauf at orxonox.net landauf at orxonox.net
Sun May 3 14:39:30 CEST 2015


Author: landauf
Date: 2015-05-03 14:39:30 +0200 (Sun, 03 May 2015)
New Revision: 10419

Added:
   code/branches/core7/src/libraries/core/object/DestroyLaterManager.cc
   code/branches/core7/src/libraries/core/object/DestroyLaterManager.h
Modified:
   code/branches/core7/src/libraries/core/CorePrereqs.h
   code/branches/core7/src/libraries/core/object/CMakeLists.txt
   code/branches/core7/src/libraries/core/object/Destroyable.cc
   code/branches/core7/src/libraries/core/object/Destroyable.h
Log:
added destroyLater() to Destroyable. This will destroy the object (like destroy()) but not until the current tick has ended.

Modified: code/branches/core7/src/libraries/core/CorePrereqs.h
===================================================================
--- code/branches/core7/src/libraries/core/CorePrereqs.h	2015-05-03 11:54:43 UTC (rev 10418)
+++ code/branches/core7/src/libraries/core/CorePrereqs.h	2015-05-03 12:39:30 UTC (rev 10419)
@@ -156,6 +156,7 @@
     class Context;
     class Core;
     class Destroyable;
+    class DestroyLaterManager;
     class DestructionListener;
     class DynLib;
     class DynLibManager;

Modified: code/branches/core7/src/libraries/core/object/CMakeLists.txt
===================================================================
--- code/branches/core7/src/libraries/core/object/CMakeLists.txt	2015-05-03 11:54:43 UTC (rev 10418)
+++ code/branches/core7/src/libraries/core/object/CMakeLists.txt	2015-05-03 12:39:30 UTC (rev 10419)
@@ -1,6 +1,7 @@
 ADD_SOURCE_FILES(CORE_SRC_FILES
   Context.cc
   Destroyable.cc
+  DestroyLaterManager.cc
   Listable.cc
   ObjectListBase.cc
 )

Added: code/branches/core7/src/libraries/core/object/DestroyLaterManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/object/DestroyLaterManager.cc	                        (rev 0)
+++ code/branches/core7/src/libraries/core/object/DestroyLaterManager.cc	2015-05-03 12:39:30 UTC (rev 10419)
@@ -0,0 +1,56 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "DestroyLaterManager.h"
+
+#include "core/CoreIncludes.h"
+#include "core/singleton/ScopedSingletonManager.h"
+
+namespace orxonox
+{
+    ManageScopedSingleton(DestroyLaterManager, ScopeID::Root, false);
+
+    RegisterAbstractClass(DestroyLaterManager).inheritsFrom<UpdateListener>();
+
+    DestroyLaterManager::DestroyLaterManager()
+    {
+        RegisterObject(DestroyLaterManager);
+    }
+
+    DestroyLaterManager::~DestroyLaterManager()
+    {
+        // Be sure to release all instances
+        this->retainedInstances_.clear();
+    }
+
+    void DestroyLaterManager::postUpdate(const Clock& time)
+    {
+        // clearing the list will destroy all smartpointers and thus all retained instances (as long as there are no other smart pointers pointing to them).
+        this->retainedInstances_.clear();
+    }
+}


Property changes on: code/branches/core7/src/libraries/core/object/DestroyLaterManager.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/branches/core7/src/libraries/core/object/DestroyLaterManager.h
===================================================================
--- code/branches/core7/src/libraries/core/object/DestroyLaterManager.h	                        (rev 0)
+++ code/branches/core7/src/libraries/core/object/DestroyLaterManager.h	2015-05-03 12:39:30 UTC (rev 10419)
@@ -0,0 +1,60 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef _DestroyLaterManager_H__
+#define _DestroyLaterManager_H__
+
+#include "core/CorePrereqs.h"
+
+#include "util/Singleton.h"
+#include "core/UpdateListener.h"
+#include "SmartPtr.h"
+
+namespace orxonox
+{
+    class _CoreExport DestroyLaterManager : public Singleton<DestroyLaterManager>, public UpdateListener
+    {
+            friend class Singleton<DestroyLaterManager>;
+        public:
+            DestroyLaterManager();
+            virtual ~DestroyLaterManager();
+
+            virtual void preUpdate(const Clock& time) { /*no action*/ }
+            virtual void postUpdate(const Clock& time);
+
+            void retain(Destroyable* instance)
+                { this->retainedInstances_.push_back(instance); }
+
+        private:
+            std::vector<SmartPtr<Destroyable> > retainedInstances_;
+
+            static DestroyLaterManager* singletonPtr_s;
+    };
+}
+
+#endif /* _DestroyLaterManager_H__ */


Property changes on: code/branches/core7/src/libraries/core/object/DestroyLaterManager.h
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: code/branches/core7/src/libraries/core/object/Destroyable.cc
===================================================================
--- code/branches/core7/src/libraries/core/object/Destroyable.cc	2015-05-03 11:54:43 UTC (rev 10418)
+++ code/branches/core7/src/libraries/core/object/Destroyable.cc	2015-05-03 12:39:30 UTC (rev 10419)
@@ -32,6 +32,7 @@
 */
 
 #include "Destroyable.h"
+#include "DestroyLaterManager.h"
 
 #include <cassert>
 
@@ -73,4 +74,16 @@
                 delete this;
         }
     }
+
+    /**
+     * Works like @ref destroy() but doesn't destroy the object until the current tick has ended.
+     */
+    void Destroyable::destroyLater()
+    {
+        // register in DestroyLaterManager - this ensures that a smartPtr points to this object and keeps it alive for a while
+        DestroyLaterManager::getInstance().retain(this);
+
+        // request destruction -> object will be deleted after all smartPtrs (including the one in DestroyLaterManager) were destroyed.
+        this->destroy();
+    }
 }

Modified: code/branches/core7/src/libraries/core/object/Destroyable.h
===================================================================
--- code/branches/core7/src/libraries/core/object/Destroyable.h	2015-05-03 11:54:43 UTC (rev 10418)
+++ code/branches/core7/src/libraries/core/object/Destroyable.h	2015-05-03 12:39:30 UTC (rev 10419)
@@ -56,6 +56,7 @@
             virtual ~Destroyable();
 
             void destroy();
+            void destroyLater();
 
             /// Returns the number of @ref orxonox::SmartPtr "smart pointers" that point to this object.
             inline unsigned int getReferenceCount() const




More information about the Orxonox-commit mailing list