Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5823


Ignore:
Timestamp:
Sep 28, 2009, 5:16:36 PM (15 years ago)
Author:
landauf
Message:

added WeakPtr (a pointer which becomes 0 if the target object is deleted)

Location:
code/branches/core5/src/libraries/core
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core5/src/libraries/core/OrxonoxClass.cc

    r5805 r5823  
    3636#include "MetaObjectList.h"
    3737#include "Identifier.h"
     38#include "WeakPtr.h"
    3839
    3940namespace orxonox
     
    6263        if (this->parents_)
    6364            delete this->parents_;
     65           
     66        // reset all weak pointers pointing to this object
     67        for (std::set<WeakPtr<OrxonoxClass>*>::iterator it = this->weakPointers_.begin(); it != this->weakPointers_.end(); )
     68            (*(it++))->reset();
    6469    }
    6570
  • code/branches/core5/src/libraries/core/OrxonoxClass.h

    r5805 r5823  
    5757        template <class T>
    5858        friend class SmartPtr;
     59
     60        template <class T>
     61        friend class WeakPtr;
    5962
    6063        public:
     
    131134            inline void decrementReferenceCount()
    132135                { --this->referenceCount_; if (this->referenceCount_ == 0 && this->requestedDestruction_) { delete this; } }
     136               
     137            /** @brief Register a weak pointer which points to this object. */
     138            template <class T>
     139            inline void registerWeakPtr(WeakPtr<T>* pointer)
     140                { this->weakPointers_.insert(reinterpret_cast<WeakPtr<OrxonoxClass>*>(pointer)); }
     141            /** @brief Unegister a weak pointer which pointed to this object before. */
     142            template <class T>
     143            inline void unregisterWeakPtr(WeakPtr<T>* pointer)
     144                { this->weakPointers_.erase(reinterpret_cast<WeakPtr<OrxonoxClass>*>(pointer)); }
    133145
    134146            Identifier* identifier_;                   //!< The Identifier of the object
     
    137149            int referenceCount_;                       //!< Counts the references from smart pointers to this object
    138150            bool requestedDestruction_;                //!< Becomes true after someone called delete on this object
     151            std::set<WeakPtr<OrxonoxClass>*> weakPointers_; //!< All weak pointers which point to this object (and like to get notified if it dies)
    139152
    140153            //! 'Fast map' that holds this-pointers of all derived types
  • code/branches/core5/src/libraries/core/SmartPtr.h

    r5807 r5823  
    3535
    3636#include <cassert>
    37 #include <ostream>
     37
    3838#include "OrxonoxClass.h"
     39#include "WeakPtr.h"
    3940
    4041namespace orxonox
     
    7172            }
    7273
     74            template <class O>
     75            inline SmartPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.getBase())
     76            {
     77                if (this->base_)
     78                    this->base_->incrementReferenceCount();
     79            }
     80
    7381            inline ~SmartPtr()
    7482            {
     
    102110            }
    103111
     112            template <class O>
     113            inline const SmartPtr& operator=(const WeakPtr<O>& other)
     114            {
     115                SmartPtr(other).swap(*this);
     116                return *this;
     117            }
     118
    104119            inline T* get() const
    105120            {
    106121                return this->pointer_;
     122            }
     123
     124            inline OrxonoxClass* getBase() const
     125            {
     126                return this->base_;
    107127            }
    108128
     
    153173    };
    154174
    155     template <class A, class B>
    156     inline bool operator==(const SmartPtr<A>& a, const SmartPtr<B>& b)
    157     {
    158         return (a.get() == b.get());
    159     }
    160 
    161     template <class A, class B>
    162     inline bool operator!=(const SmartPtr<A>& a, const SmartPtr<B>& b)
    163     {
    164         return (a.get() != b.get());
    165     }
    166 
    167     template <class T>
    168     inline bool operator<(const SmartPtr<T>& a, const SmartPtr<T>& b)
    169     {
    170         return std::less<T*>()(a.get(), b.get());
    171     }
    172 
    173175    template <class T>
    174176    void swap(SmartPtr<T>& a, SmartPtr<T>& b)
     
    194196        return dynamic_cast<T*>(p.get());
    195197    }
    196 
    197     template <class T>
    198     std::ostream& operator<<(std::ostream& os, const SmartPtr<T>& p)
    199     {
    200         os << p.get();
    201         return os;
    202     }
    203198}
    204199
Note: See TracChangeset for help on using the changeset viewer.