Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9570 for code/branches/core6


Ignore:
Timestamp:
Mar 24, 2013, 8:51:37 PM (12 years ago)
Author:
landauf
Message:

moved functions and attributes needed to safely destroy objects from OrxonoxClass to Destroyable

Location:
code/branches/core6/src/libraries/core
Files:
5 edited
2 copied

Legend:

Unmodified
Added
Removed
  • code/branches/core6/src/libraries/core/CorePrereqs.h

    r9565 r9570  
    143143    class Context;
    144144    class Core;
     145    class Destroyable;
    145146    class DestructionListener;
    146147    class DynLib;
  • code/branches/core6/src/libraries/core/class/Identifiable.h

    r9565 r9570  
    5757            virtual ~Identifiable();
    5858
    59             void destroy();
    6059            void unregisterObject();
    6160
  • code/branches/core6/src/libraries/core/class/OrxonoxClass.cc

    r9565 r9570  
    3535
    3636#include <cassert>
    37 #include "core/object/MetaObjectList.h"
    38 #include "core/object/Context.h"
    39 #include "Identifier.h"
    4037
    4138namespace orxonox
     
    4643    OrxonoxClass::OrxonoxClass()
    4744    {
    48         this->referenceCount_ = 0;
    49         this->requestedDestruction_ = false;
    5045    }
    5146
     
    5550    OrxonoxClass::~OrxonoxClass()
    5651    {
    57         assert(this->referenceCount_ <= 0);
    58 
    59         // notify all destruction listeners
    60         for (std::set<DestructionListener*>::iterator it = this->destructionListeners_.begin(); it != this->destructionListeners_.end(); )
    61             (*(it++))->objectDeleted();
    62     }
    63 
    64     /**
    65         @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.
    66     */
    67     void OrxonoxClass::destroy()
    68     {
    69         assert(this); // Just in case someone tries to delete a NULL pointer
    70         this->requestedDestruction_ = true;
    71         if (this->referenceCount_ == 0)
    72         {
    73             this->preDestroy();
    74             if (this->referenceCount_ == 0)
    75                 delete this;
    76         }
    7752    }
    7853}
  • code/branches/core6/src/libraries/core/class/OrxonoxClass.h

    r9565 r9570  
    4343#include "core/CorePrereqs.h"
    4444
    45 #include <set>
    46 //#include "Super.h"
    4745#include "Identifiable.h"
     46#include "core/object/Destroyable.h"
    4847
    4948namespace orxonox
     
    5453        The BaseObject and Interfaces are derived with @c virtual @c public @c OrxonoxClass from OrxonoxClass.
    5554    */
    56     class _CoreExport OrxonoxClass : public Identifiable
     55    class _CoreExport OrxonoxClass : public Identifiable, public Destroyable
    5756    {
    58         template <class T>
    59         friend class SmartPtr;
    60 
    61         friend class DestructionListener;
    62 
    6357        public:
    6458            OrxonoxClass();
    6559            virtual ~OrxonoxClass();
    6660
    67             void destroy();
    68 
    6961            /// Function to collect the SetConfigValue-macro calls.
    7062            void setConfigValues() {};
    71 
    72             /// Returns the number of @ref orxonox::SmartPtr "smart pointers" that point to this object.
    73             inline unsigned int getReferenceCount() const
    74                 { return this->referenceCount_; }
    75 
    76         protected:
    77             /// 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.
    78             virtual void preDestroy() {}
    79 
    80         private:
    81             /// Increments the reference counter (for smart pointers).
    82             inline void incrementReferenceCount()
    83                 { ++this->referenceCount_; }
    84             /// Decrements the reference counter (for smart pointers).
    85             inline void decrementReferenceCount()
    86             {
    87                 --this->referenceCount_;
    88                 if (this->referenceCount_ == 0 && this->requestedDestruction_)
    89                     this->destroy();
    90             }
    91 
    92             /// Register a destruction listener (for example a weak pointer which points to this object).
    93             inline void registerDestructionListener(DestructionListener* pointer)
    94                 { this->destructionListeners_.insert(pointer); }
    95             /// Unegister a destruction listener (for example a weak pointer which pointed to this object before).
    96             inline void unregisterDestructionListener(DestructionListener* pointer)
    97                 { this->destructionListeners_.erase(pointer); }
    98 
    99             int referenceCount_;                                    //!< Counts the references from smart pointers to this object
    100             bool requestedDestruction_;                             //!< Becomes true after someone called delete on this object
    101             std::set<DestructionListener*> destructionListeners_;   //!< All destruction listeners (for example weak pointers which point to this object and like to get notified if it dies)
    10263    };
    103 
    104     /**
    105         @brief This listener is used to inform weak pointers if an object of type OrxonoxClass gets destroyed.
    106     */
    107     class _CoreExport DestructionListener
    108     {
    109         friend class OrxonoxClass;
    110 
    111         protected:
    112             virtual ~DestructionListener() {}
    113 
    114             inline void registerAsDestructionListener(OrxonoxClass* object)
    115                 { if (object) { object->registerDestructionListener(this); } }
    116             inline void unregisterAsDestructionListener(OrxonoxClass* object)
    117                 { if (object) { object->unregisterDestructionListener(this); } }
    118 
    119             virtual void objectDeleted() = 0;
    120     };
    121 
    12264}
    12365
  • code/branches/core6/src/libraries/core/object/CMakeLists.txt

    r9562 r9570  
    22  Context.cc
    33  ContextObject.cc
     4  Destroyable.cc
    45  MetaObjectList.cc
    56  ObjectListBase.cc
  • code/branches/core6/src/libraries/core/object/Destroyable.cc

    r9565 r9570  
    2929/**
    3030    @file
    31     @brief Implementation of OrxonoxClass.
     31    @brief Implementation of Destroyable.
    3232*/
    3333
    34 #include "OrxonoxClass.h"
     34#include "Destroyable.h"
    3535
    3636#include <cassert>
    37 #include "core/object/MetaObjectList.h"
    38 #include "core/object/Context.h"
    39 #include "Identifier.h"
    4037
    4138namespace orxonox
     
    4441        @brief Constructor: Sets the default values.
    4542    */
    46     OrxonoxClass::OrxonoxClass()
     43    Destroyable::Destroyable()
    4744    {
    4845        this->referenceCount_ = 0;
     
    5350        @brief Destructor: Notifies all DestructionListener (for example @ref WeakPtr "weak pointers") that this object is being deleted.
    5451    */
    55     OrxonoxClass::~OrxonoxClass()
     52    Destroyable::~Destroyable()
    5653    {
    5754        assert(this->referenceCount_ <= 0);
     
    6562        @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.
    6663    */
    67     void OrxonoxClass::destroy()
     64    void Destroyable::destroy()
    6865    {
    6966        assert(this); // Just in case someone tries to delete a NULL pointer
  • code/branches/core6/src/libraries/core/object/Destroyable.h

    r9565 r9570  
    2828
    2929/**
    30     @defgroup OrxonoxClass OrxonoxClass
    31     @ingroup Class
     30    @file
     31    @ingroup Object
     32    @brief Declaration of Destroyable, the base class of all objects which can be used with SmartPtr and WeakPtr.
    3233*/
    3334
    34 /**
    35     @file
    36     @ingroup Class OrxonoxClass
    37     @brief Declaration of OrxonoxClass, the base class of all objects and interfaces in Orxonox.
    38 */
    39 
    40 #ifndef _OrxonoxClass_H__
    41 #define _OrxonoxClass_H__
     35#ifndef _Destroyable_H__
     36#define _Destroyable_H__
    4237
    4338#include "core/CorePrereqs.h"
    4439
    4540#include <set>
    46 //#include "Super.h"
    47 #include "Identifiable.h"
    4841
    4942namespace orxonox
    5043{
    5144    /**
    52         @brief The class all objects and interfaces of the game-logic (not the engine) are derived from.
    53 
    54         The BaseObject and Interfaces are derived with @c virtual @c public @c OrxonoxClass from OrxonoxClass.
     45        @brief Classes must inherit from this class if they should be used with SmartPtr or WeakPtr.
    5546    */
    56     class _CoreExport OrxonoxClass : public Identifiable
     47    class _CoreExport Destroyable
    5748    {
    5849        template <class T>
     
    6253
    6354        public:
    64             OrxonoxClass();
    65             virtual ~OrxonoxClass();
     55            Destroyable();
     56            virtual ~Destroyable();
    6657
    6758            void destroy();
    68 
    69             /// Function to collect the SetConfigValue-macro calls.
    70             void setConfigValues() {};
    7159
    7260            /// Returns the number of @ref orxonox::SmartPtr "smart pointers" that point to this object.
     
    10391
    10492    /**
    105         @brief This listener is used to inform weak pointers if an object of type OrxonoxClass gets destroyed.
     93        @brief This listener is used to inform weak pointers if an object of type Destroyable gets destroyed.
    10694    */
    10795    class _CoreExport DestructionListener
    10896    {
    109         friend class OrxonoxClass;
     97        friend class Destroyable;
    11098
    11199        protected:
    112100            virtual ~DestructionListener() {}
    113101
    114             inline void registerAsDestructionListener(OrxonoxClass* object)
     102            inline void registerAsDestructionListener(Destroyable* object)
    115103                { if (object) { object->registerDestructionListener(this); } }
    116             inline void unregisterAsDestructionListener(OrxonoxClass* object)
     104            inline void unregisterAsDestructionListener(Destroyable* object)
    117105                { if (object) { object->unregisterDestructionListener(this); } }
    118106
     
    122110}
    123111
    124 #endif /* _OrxonoxClass_H__ */
     112#endif /* _Destroyable_H__ */
Note: See TracChangeset for help on using the changeset viewer.