Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7849


Ignore:
Timestamp:
Feb 10, 2011, 9:52:23 PM (13 years ago)
Author:
landauf
Message:

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.

Location:
code/trunk/src/libraries/core
Files:
4 edited

Legend:

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

    r7284 r7849  
    138138    class ConfigValueContainer;
    139139    class Core;
     140    class DestructionListener;
    140141    class DynLib;
    141142    class DynLibManager;
  • code/trunk/src/libraries/core/OrxonoxClass.cc

    r7401 r7849  
    3737#include "MetaObjectList.h"
    3838#include "Identifier.h"
    39 #include "WeakPtr.h"
    4039
    4140namespace orxonox
     
    5655
    5756    /**
    58         @brief Destructor: Removes the object from the object-lists, notifies all @ref WeakPtr "weak pointers" that this object is being deleted.
     57        @brief Destructor: Removes the object from the object-lists, notifies all DestructionListener (for example @ref WeakPtr "weak pointers") that this object is being deleted.
    5958    */
    6059    OrxonoxClass::~OrxonoxClass()
     
    7170            delete this->parents_;
    7271
    73         // reset all weak pointers pointing to this object
    74         for (std::set<WeakPtr<OrxonoxClass>*>::iterator it = this->weakPointers_.begin(); it != this->weakPointers_.end(); )
     72        // notify all destruction listeners
     73        for (std::set<DestructionListener*>::iterator it = this->destructionListeners_.begin(); it != this->destructionListeners_.end(); )
    7574            (*(it++))->objectDeleted();
    7675    }
  • code/trunk/src/libraries/core/OrxonoxClass.h

    r7401 r7849  
    7474        friend class SmartPtr;
    7575
    76         template <class T>
    77         friend class WeakPtr;
     76        friend class DestructionListener;
    7877
    7978        public:
     
    169168            }
    170169
    171             /// Register a weak pointer which points to this object.
    172             template <class T>
    173             inline void registerWeakPtr(WeakPtr<T>* pointer)
    174                 { this->weakPointers_.insert(reinterpret_cast<WeakPtr<OrxonoxClass>*>(pointer)); }
    175             /// Unegister a weak pointer which pointed to this object before.
    176             template <class T>
    177             inline void unregisterWeakPtr(WeakPtr<T>* pointer)
    178                 { this->weakPointers_.erase(reinterpret_cast<WeakPtr<OrxonoxClass>*>(pointer)); }
    179 
    180             Identifier* identifier_;                            //!< The Identifier of the object
    181             std::set<const Identifier*>* parents_;              //!< List of all parents of the object
    182             MetaObjectList* metaList_;                          //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in
    183             int referenceCount_;                                //!< Counts the references from smart pointers to this object
    184             bool requestedDestruction_;                         //!< Becomes true after someone called delete on this object
    185             std::set<WeakPtr<OrxonoxClass>*> weakPointers_;     //!< All weak pointers which point to this object (and like to get notified if it dies)
     170            /// Register a destruction listener (for example a weak pointer which points to this object).
     171            inline void registerDestructionListener(DestructionListener* pointer)
     172                { this->destructionListeners_.insert(pointer); }
     173            /// Unegister a destruction listener (for example a weak pointer which pointed to this object before).
     174            inline void unregisterDestructionListener(DestructionListener* pointer)
     175                { this->destructionListeners_.erase(pointer); }
     176
     177            Identifier* identifier_;                                //!< The Identifier of the object
     178            std::set<const Identifier*>* parents_;                  //!< List of all parents of the object
     179            MetaObjectList* metaList_;                              //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in
     180            int referenceCount_;                                    //!< Counts the references from smart pointers to this object
     181            bool requestedDestruction_;                             //!< Becomes true after someone called delete on this object
     182            std::set<DestructionListener*> destructionListeners_;   //!< All destruction listeners (for example weak pointers which point to this object and like to get notified if it dies)
    186183
    187184            /// 'Fast map' that holds this-pointers of all derived types
     
    189186    };
    190187
     188    /**
     189        @brief This listener is used to inform weak pointers if an object of type OrxonoxClass gets destroyed.
     190    */
     191    class _CoreExport DestructionListener
     192    {
     193        friend class OrxonoxClass;
     194
     195        protected:
     196            inline void registerAsDestructionListener(OrxonoxClass* object)
     197                { object->registerDestructionListener(this); }
     198            inline void unregisterAsDestructionListener(OrxonoxClass* object)
     199                { object->unregisterDestructionListener(this); }
     200
     201            virtual void objectDeleted() = 0;
     202    };
     203
    191204    SUPER_FUNCTION(11, OrxonoxClass, clone, false);
    192 
    193205}
    194206
  • code/trunk/src/libraries/core/WeakPtr.h

    r7401 r7849  
    9696    */
    9797    template <class T>
    98     class WeakPtr
    99     {
    100         friend class OrxonoxClass;
    101 
     98    class WeakPtr : public DestructionListener
     99    {
    102100        public:
    103101            /// Constructor: Initializes the weak pointer with a null pointer.
     
    115113            {
    116114                if (this->base_)
    117                     this->base_->registerWeakPtr(this);
     115                    this->registerAsDestructionListener(this->base_);
    118116            }
    119117
     
    122120            {
    123121                if (this->base_)
    124                     this->base_->registerWeakPtr(this);
     122                    this->registerAsDestructionListener(this->base_);
    125123            }
    126124
     
    130128            {
    131129                if (this->base_)
    132                     this->base_->registerWeakPtr(this);
     130                    this->registerAsDestructionListener(this->base_);
    133131            }
    134132
     
    137135            {
    138136                if (this->base_)
    139                     this->base_->unregisterWeakPtr(this);
     137                    this->unregisterAsDestructionListener(this->base_);
    140138
    141139            }
     
    212210            {
    213211                if (this->base_)
    214                     this->base_->unregisterWeakPtr(this);
     212                    this->unregisterAsDestructionListener(this->base_);
    215213                if (other.base_)
    216                     other.base_->unregisterWeakPtr(&other);
     214                    other.unregisterAsDestructionListener(other.base_);
    217215
    218216                {
     
    228226
    229227                if (this->base_)
    230                     this->base_->registerWeakPtr(this);
     228                    this->registerAsDestructionListener(this->base_);
    231229                if (other.base_)
    232                     other.base_->registerWeakPtr(&other);
     230                    other.registerAsDestructionListener(other.base_);
    233231            }
    234232
Note: See TracChangeset for help on using the changeset viewer.