[Orxonox-commit 4901] r9570 - in code/branches/core6/src/libraries/core: . class object
landauf at orxonox.net
landauf at orxonox.net
Sun Mar 24 20:51:37 CET 2013
Author: landauf
Date: 2013-03-24 20:51:37 +0100 (Sun, 24 Mar 2013)
New Revision: 9570
Added:
code/branches/core6/src/libraries/core/object/Destroyable.cc
code/branches/core6/src/libraries/core/object/Destroyable.h
Modified:
code/branches/core6/src/libraries/core/CorePrereqs.h
code/branches/core6/src/libraries/core/class/Identifiable.h
code/branches/core6/src/libraries/core/class/OrxonoxClass.cc
code/branches/core6/src/libraries/core/class/OrxonoxClass.h
code/branches/core6/src/libraries/core/object/CMakeLists.txt
Log:
moved functions and attributes needed to safely destroy objects from OrxonoxClass to Destroyable
Modified: code/branches/core6/src/libraries/core/CorePrereqs.h
===================================================================
--- code/branches/core6/src/libraries/core/CorePrereqs.h 2013-03-24 18:31:22 UTC (rev 9569)
+++ code/branches/core6/src/libraries/core/CorePrereqs.h 2013-03-24 19:51:37 UTC (rev 9570)
@@ -142,6 +142,7 @@
class ConfigValueContainer;
class Context;
class Core;
+ class Destroyable;
class DestructionListener;
class DynLib;
class DynLibManager;
Modified: code/branches/core6/src/libraries/core/class/Identifiable.h
===================================================================
--- code/branches/core6/src/libraries/core/class/Identifiable.h 2013-03-24 18:31:22 UTC (rev 9569)
+++ code/branches/core6/src/libraries/core/class/Identifiable.h 2013-03-24 19:51:37 UTC (rev 9570)
@@ -56,7 +56,6 @@
Identifiable();
virtual ~Identifiable();
- void destroy();
void unregisterObject();
/// Returns the Identifier of the object.
Modified: code/branches/core6/src/libraries/core/class/OrxonoxClass.cc
===================================================================
--- code/branches/core6/src/libraries/core/class/OrxonoxClass.cc 2013-03-24 18:31:22 UTC (rev 9569)
+++ code/branches/core6/src/libraries/core/class/OrxonoxClass.cc 2013-03-24 19:51:37 UTC (rev 9570)
@@ -34,9 +34,6 @@
#include "OrxonoxClass.h"
#include <cassert>
-#include "core/object/MetaObjectList.h"
-#include "core/object/Context.h"
-#include "Identifier.h"
namespace orxonox
{
@@ -45,8 +42,6 @@
*/
OrxonoxClass::OrxonoxClass()
{
- this->referenceCount_ = 0;
- this->requestedDestruction_ = false;
}
/**
@@ -54,25 +49,5 @@
*/
OrxonoxClass::~OrxonoxClass()
{
- assert(this->referenceCount_ <= 0);
-
- // notify all destruction listeners
- for (std::set<DestructionListener*>::iterator it = this->destructionListeners_.begin(); it != this->destructionListeners_.end(); )
- (*(it++))->objectDeleted();
}
-
- /**
- @brief Deletes the object if no @ref orxonox::SmartPtr "smart pointers" point to this object. Otherwise schedules the object to be deleted as soon as possible.
- */
- void OrxonoxClass::destroy()
- {
- assert(this); // Just in case someone tries to delete a NULL pointer
- this->requestedDestruction_ = true;
- if (this->referenceCount_ == 0)
- {
- this->preDestroy();
- if (this->referenceCount_ == 0)
- delete this;
- }
- }
}
Modified: code/branches/core6/src/libraries/core/class/OrxonoxClass.h
===================================================================
--- code/branches/core6/src/libraries/core/class/OrxonoxClass.h 2013-03-24 18:31:22 UTC (rev 9569)
+++ code/branches/core6/src/libraries/core/class/OrxonoxClass.h 2013-03-24 19:51:37 UTC (rev 9570)
@@ -42,9 +42,8 @@
#include "core/CorePrereqs.h"
-#include <set>
-//#include "Super.h"
#include "Identifiable.h"
+#include "core/object/Destroyable.h"
namespace orxonox
{
@@ -53,72 +52,15 @@
The BaseObject and Interfaces are derived with @c virtual @c public @c OrxonoxClass from OrxonoxClass.
*/
- class _CoreExport OrxonoxClass : public Identifiable
+ class _CoreExport OrxonoxClass : public Identifiable, public Destroyable
{
- template <class T>
- friend class SmartPtr;
-
- friend class DestructionListener;
-
public:
OrxonoxClass();
virtual ~OrxonoxClass();
- void destroy();
-
/// Function to collect the SetConfigValue-macro calls.
void setConfigValues() {};
-
- /// Returns the number of @ref orxonox::SmartPtr "smart pointers" that point to this object.
- inline unsigned int getReferenceCount() const
- { return this->referenceCount_; }
-
- protected:
- /// This virtual function is called if destroy() is called and no SmartPtr points to this object. Used in some cases to create a new SmartPtr to prevent destruction.
- virtual void preDestroy() {}
-
- private:
- /// Increments the reference counter (for smart pointers).
- inline void incrementReferenceCount()
- { ++this->referenceCount_; }
- /// Decrements the reference counter (for smart pointers).
- inline void decrementReferenceCount()
- {
- --this->referenceCount_;
- if (this->referenceCount_ == 0 && this->requestedDestruction_)
- this->destroy();
- }
-
- /// 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); }
-
- 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)
};
-
- /**
- @brief This listener is used to inform weak pointers if an object of type OrxonoxClass gets destroyed.
- */
- class _CoreExport DestructionListener
- {
- friend class OrxonoxClass;
-
- protected:
- virtual ~DestructionListener() {}
-
- inline void registerAsDestructionListener(OrxonoxClass* object)
- { if (object) { object->registerDestructionListener(this); } }
- inline void unregisterAsDestructionListener(OrxonoxClass* object)
- { if (object) { object->unregisterDestructionListener(this); } }
-
- virtual void objectDeleted() = 0;
- };
-
}
#endif /* _OrxonoxClass_H__ */
Modified: code/branches/core6/src/libraries/core/object/CMakeLists.txt
===================================================================
--- code/branches/core6/src/libraries/core/object/CMakeLists.txt 2013-03-24 18:31:22 UTC (rev 9569)
+++ code/branches/core6/src/libraries/core/object/CMakeLists.txt 2013-03-24 19:51:37 UTC (rev 9570)
@@ -1,6 +1,7 @@
ADD_SOURCE_FILES(CORE_SRC_FILES
Context.cc
ContextObject.cc
+ Destroyable.cc
MetaObjectList.cc
ObjectListBase.cc
)
Copied: code/branches/core6/src/libraries/core/object/Destroyable.cc (from rev 9565, code/branches/core6/src/libraries/core/class/OrxonoxClass.cc)
===================================================================
--- code/branches/core6/src/libraries/core/object/Destroyable.cc (rev 0)
+++ code/branches/core6/src/libraries/core/object/Destroyable.cc 2013-03-24 19:51:37 UTC (rev 9570)
@@ -0,0 +1,75 @@
+/*
+ * 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:
+ * ...
+ *
+ */
+
+/**
+ @file
+ @brief Implementation of Destroyable.
+*/
+
+#include "Destroyable.h"
+
+#include <cassert>
+
+namespace orxonox
+{
+ /**
+ @brief Constructor: Sets the default values.
+ */
+ Destroyable::Destroyable()
+ {
+ this->referenceCount_ = 0;
+ this->requestedDestruction_ = false;
+ }
+
+ /**
+ @brief Destructor: Notifies all DestructionListener (for example @ref WeakPtr "weak pointers") that this object is being deleted.
+ */
+ Destroyable::~Destroyable()
+ {
+ assert(this->referenceCount_ <= 0);
+
+ // notify all destruction listeners
+ for (std::set<DestructionListener*>::iterator it = this->destructionListeners_.begin(); it != this->destructionListeners_.end(); )
+ (*(it++))->objectDeleted();
+ }
+
+ /**
+ @brief Deletes the object if no @ref orxonox::SmartPtr "smart pointers" point to this object. Otherwise schedules the object to be deleted as soon as possible.
+ */
+ void Destroyable::destroy()
+ {
+ assert(this); // Just in case someone tries to delete a NULL pointer
+ this->requestedDestruction_ = true;
+ if (this->referenceCount_ == 0)
+ {
+ this->preDestroy();
+ if (this->referenceCount_ == 0)
+ delete this;
+ }
+ }
+}
Copied: code/branches/core6/src/libraries/core/object/Destroyable.h (from rev 9565, code/branches/core6/src/libraries/core/class/OrxonoxClass.h)
===================================================================
--- code/branches/core6/src/libraries/core/object/Destroyable.h (rev 0)
+++ code/branches/core6/src/libraries/core/object/Destroyable.h 2013-03-24 19:51:37 UTC (rev 9570)
@@ -0,0 +1,112 @@
+/*
+ * 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:
+ * ...
+ *
+ */
+
+/**
+ @file
+ @ingroup Object
+ @brief Declaration of Destroyable, the base class of all objects which can be used with SmartPtr and WeakPtr.
+*/
+
+#ifndef _Destroyable_H__
+#define _Destroyable_H__
+
+#include "core/CorePrereqs.h"
+
+#include <set>
+
+namespace orxonox
+{
+ /**
+ @brief Classes must inherit from this class if they should be used with SmartPtr or WeakPtr.
+ */
+ class _CoreExport Destroyable
+ {
+ template <class T>
+ friend class SmartPtr;
+
+ friend class DestructionListener;
+
+ public:
+ Destroyable();
+ virtual ~Destroyable();
+
+ void destroy();
+
+ /// Returns the number of @ref orxonox::SmartPtr "smart pointers" that point to this object.
+ inline unsigned int getReferenceCount() const
+ { return this->referenceCount_; }
+
+ protected:
+ /// This virtual function is called if destroy() is called and no SmartPtr points to this object. Used in some cases to create a new SmartPtr to prevent destruction.
+ virtual void preDestroy() {}
+
+ private:
+ /// Increments the reference counter (for smart pointers).
+ inline void incrementReferenceCount()
+ { ++this->referenceCount_; }
+ /// Decrements the reference counter (for smart pointers).
+ inline void decrementReferenceCount()
+ {
+ --this->referenceCount_;
+ if (this->referenceCount_ == 0 && this->requestedDestruction_)
+ this->destroy();
+ }
+
+ /// 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); }
+
+ 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)
+ };
+
+ /**
+ @brief This listener is used to inform weak pointers if an object of type Destroyable gets destroyed.
+ */
+ class _CoreExport DestructionListener
+ {
+ friend class Destroyable;
+
+ protected:
+ virtual ~DestructionListener() {}
+
+ inline void registerAsDestructionListener(Destroyable* object)
+ { if (object) { object->registerDestructionListener(this); } }
+ inline void unregisterAsDestructionListener(Destroyable* object)
+ { if (object) { object->unregisterDestructionListener(this); } }
+
+ virtual void objectDeleted() = 0;
+ };
+
+}
+
+#endif /* _Destroyable_H__ */
More information about the Orxonox-commit
mailing list