Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 11, 2010, 12:34:00 AM (14 years ago)
Author:
landauf
Message:

merged doc branch back to trunk

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/core/WeakPtr.h

    r7284 r7401  
    2929// Inspired by boost::intrusive_ptr by Peter Dimov
    3030
     31/**
     32    @file
     33    @ingroup Object SmartPtr
     34    @brief Definition of WeakPtr<T>, wraps a pointer to an object.
     35
     36    @anchor WeakPtrExample
     37
     38    A WeakPtr wraps a pointer to an object. If the object gets deleted, the WeakPtr becomes
     39    NULL. This can be used to store pointers to objects without knowing when they will be
     40    destroyed.
     41
     42    WeakPtr works only with objects that are derived from orxonox::OrxonoxClass, because
     43    WeakPtr is intrusive and registers itself in the stored object, to get a notification if
     44    the object is being deleted.
     45
     46    Example:
     47    @code
     48    MyClass* object = new MyClass();                    // create an instance of MyClass
     49
     50    WeakPtr<MyClass> pointer = object;                  // create a WeakPtr and assign the object
     51
     52    if (pointer)                                        // checks if pointer is not NULL (which is true)
     53        pointer->someFunction();                        // calls MyClass::someFunction()
     54
     55    object->destroy();                                  // calls destroy() which deletes the object
     56
     57    if (pointer)                                        // checks if pointer is not NULL (which is now false)
     58        pointer->someFunction();                        // this will not be executed
     59    @endcode
     60    In this example we assumed that MyClass is derived of OrxonoxClass (otherwise it couldn't
     61    be used with a WeakPtr).
     62
     63    A callback can be registerd with the WeakPtr that will be called if the object gets deleted.
     64    @code
     65    void myCallback()                                   // definition of the callback function
     66    {
     67        COUT(0) << "Object destroyed" << std::endl;
     68    }
     69
     70    MyClass* object = new MyClass();                    // create an instance of MyClass
     71
     72    WeakPtr<MyClass> pointer = object;                  // create a WeakPtr and assign the object
     73
     74    pointer.setCallback(createFunctor(&myCallback));    // defines a callback
     75
     76    object->destroy();                                  // calls destroy() which deletes the object. prints "Object destroyed" to the console
     77    @endcode
     78*/
     79
    3180#ifndef _WeakPtr_H__
    3281#define _WeakPtr_H__
     
    4190namespace orxonox
    4291{
     92    /**
     93        @brief WeakPtr wraps a pointer to an object, which becomes NULL if the object is deleted.
     94
     95        @see See @ref WeakPtrExample "this description" for more information and an example.
     96    */
    4397    template <class T>
    4498    class WeakPtr
     
    47101
    48102        public:
     103            /// Constructor: Initializes the weak pointer with a null pointer.
    49104            inline WeakPtr() : pointer_(0), base_(0), callback_(0)
    50105            {
    51106            }
    52107
     108            /// Constructor: Used to explicitly initialize the weak pointer with a null pointer
    53109            inline WeakPtr(int) : pointer_(0), base_(0), callback_(0)
    54110            {
    55111            }
    56112
     113            /// Constructor: Initializes the weak pointer with a pointer to an object.
    57114            inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer), callback_(0)
    58115            {
     
    61118            }
    62119
     120            /// Copy-constructor
    63121            inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_), callback_(0)
    64122            {
     
    67125            }
    68126
     127            /// Copy-constructor for weak pointers to objects of another class.
    69128            template <class O>
    70129            inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.base_), callback_(0)
     
    74133            }
    75134
     135            /// Destructor
    76136            inline ~WeakPtr()
    77137            {
     
    81141            }
    82142
     143            /// Used to assign a null pointer.
    83144            inline WeakPtr& operator=(int)
    84145            {
     
    87148            }
    88149
     150            /// Assigns a new pointer.
    89151            inline WeakPtr& operator=(T* pointer)
    90152            {
     
    93155            }
    94156
     157            /// Assigns the wrapped pointer of another WeakPtr.
    95158            inline WeakPtr& operator=(const WeakPtr& other)
    96159            {
     
    99162            }
    100163
     164            /// Assigns the wrapped pointer of a WeakPtr of another class
    101165            template <class O>
    102166            inline WeakPtr& operator=(const WeakPtr<O>& other)
     
    106170            }
    107171
     172            /// Returns the wrapped pointer as @c T*
    108173            inline T* get() const
    109174            {
     
    111176            }
    112177
     178            /// Returns the wrapped pointer as @c OrxonoxClass*
    113179            inline OrxonoxClass* getBase() const
    114180            {
     
    116182            }
    117183
     184            /// Implicitly converts the WeakPtr to a pointer of type @c T*
    118185            inline operator T*() const
    119186            {
     
    121188            }
    122189
     190            /// Overloaded operator, returns a pointer to the stored object.
    123191            inline T* operator->() const
    124192            {
     
    127195            }
    128196
     197            /// Overloaded operator, returns a reference to the stored object.
    129198            inline T& operator*() const
    130199            {
     
    133202            }
    134203
     204            /// Returns true if the wrapped pointer is NULL.
    135205            inline bool operator!() const
    136206            {
     
    138208            }
    139209
     210            /// Swaps the contents of two weak pointers.
    140211            inline void swap(WeakPtr& other)
    141212            {
     
    162233            }
    163234
     235            /// Resets the weak pointer (equivalent to assigning a NULL pointer).
    164236            inline void reset()
    165237            {
     
    167239            }
    168240
     241            /// Registers a callback that will be executed if the stored object is destroyed.
    169242            inline void setCallback(const FunctorPtr& callback)
    170243            {
     
    172245            }
    173246
     247            /// Returns the registered callback.
    174248            inline const FunctorPtr& getCallback() const
    175249            {
     
    178252
    179253        private:
     254            /// Will be called by OrxonoxClass::~OrxonoxClass() if the stored object is deleted. Resets the wrapped pointer and executes the callback.
    180255            inline void objectDeleted()
    181256            {
     
    186261            }
    187262
    188             T* pointer_;
    189             OrxonoxClass* base_;
    190             FunctorPtr callback_;
     263            T* pointer_;            ///< The wrapped pointer to an object of type @a T
     264            OrxonoxClass* base_;    ///< The wrapped pointer, casted up to OrxonoxClass (this is needed because with just a T* pointer, WeakPtr couln't be used with forward declarations)
     265            FunctorPtr callback_;   ///< This callback will be executed if the stored object is deleted
    191266    };
    192267
     268    /// Swaps the contents of two weak pointers.
    193269    template <class T>
    194270    void swap(WeakPtr<T>& a, WeakPtr<T>& b)
     
    197273    }
    198274
     275    /// Uses a static_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new WeakPtr<T>.
    199276    template <class T, class U>
    200277    WeakPtr<T> static_pointer_cast(const WeakPtr<U>& p)
     
    203280    }
    204281
     282    /// Uses a const_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new WeakPtr<T>.
    205283    template <class T, class U>
    206284    WeakPtr<T> const_pointer_cast(const WeakPtr<U>& p)
     
    209287    }
    210288
     289    /// Uses a dynamic_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new WeakPtr<T>.
    211290    template <class T, class U>
    212291    WeakPtr<T> dynamic_pointer_cast(const WeakPtr<U>& p)
Note: See TracChangeset for help on using the changeset viewer.