Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 7, 2010, 11:24:47 PM (14 years ago)
Author:
landauf
Message:

added documentation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/doc/src/libraries/core/WeakPtr.h

    r7363 r7373  
    3232    @file
    3333    @ingroup Object SmartPtr
     34    @brief Definition of WeakPtr<T>.
     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
    3478*/
    3579
     
    4690namespace orxonox
    4791{
     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    */
    4897    template <class T>
    4998    class WeakPtr
     
    52101
    53102        public:
     103            /// Constructor: Initializes the weak pointer with a null pointer.
    54104            inline WeakPtr() : pointer_(0), base_(0), callback_(0)
    55105            {
    56106            }
    57107
     108            /// Constructor: Used to explicitly initialize the weak pointer with a null pointer
    58109            inline WeakPtr(int) : pointer_(0), base_(0), callback_(0)
    59110            {
    60111            }
    61112
     113            /// Constructor: Initializes the weak pointer with a pointer to an object.
    62114            inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer), callback_(0)
    63115            {
     
    66118            }
    67119
     120            /// Copy-constructor
    68121            inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_), callback_(0)
    69122            {
     
    72125            }
    73126
     127            /// Copy-constructor for weak pointers to objects of another class.
    74128            template <class O>
    75129            inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.base_), callback_(0)
     
    79133            }
    80134
     135            /// Destructor
    81136            inline ~WeakPtr()
    82137            {
     
    86141            }
    87142
     143            /// Used to assign a null pointer.
    88144            inline WeakPtr& operator=(int)
    89145            {
     
    92148            }
    93149
     150            /// Assigns a new pointer.
    94151            inline WeakPtr& operator=(T* pointer)
    95152            {
     
    98155            }
    99156
     157            /// Assigns the wrapped pointer of another WeakPtr.
    100158            inline WeakPtr& operator=(const WeakPtr& other)
    101159            {
     
    104162            }
    105163
     164            /// Assigns the wrapped pointer of a WeakPtr of another class
    106165            template <class O>
    107166            inline WeakPtr& operator=(const WeakPtr<O>& other)
     
    111170            }
    112171
     172            /// Returns the wrapped pointer as @c T*
    113173            inline T* get() const
    114174            {
     
    116176            }
    117177
     178            /// Returns the wrapped pointer as @c OrxonoxClass*
    118179            inline OrxonoxClass* getBase() const
    119180            {
     
    121182            }
    122183
     184            /// Implicitly converts the WeakPtr to a pointer of type @c T*
    123185            inline operator T*() const
    124186            {
     
    126188            }
    127189
     190            /// Overloaded operator, returns a pointer to the stored object.
    128191            inline T* operator->() const
    129192            {
     
    132195            }
    133196
     197            /// Overloaded operator, returns a reference to the stored object.
    134198            inline T& operator*() const
    135199            {
     
    138202            }
    139203
     204            /// Returns true if the wrapped pointer is NULL.
    140205            inline bool operator!() const
    141206            {
     
    143208            }
    144209
     210            /// Swaps the contents of two weak pointers.
    145211            inline void swap(WeakPtr& other)
    146212            {
     
    167233            }
    168234
     235            /// Resets the weak pointer (equivalent to assigning a NULL pointer).
    169236            inline void reset()
    170237            {
     
    172239            }
    173240
     241            /// Registers a callback that will be executed if the stored object is destroyed.
    174242            inline void setCallback(const FunctorPtr& callback)
    175243            {
     
    177245            }
    178246
     247            /// Returns the registered callback.
    179248            inline const FunctorPtr& getCallback() const
    180249            {
     
    183252
    184253        private:
     254            /// Will be called by OrxonoxClass::~OrxonoxClass() if the stored object is deleted. Resets the wrapped pointer and executes the callback.
    185255            inline void objectDeleted()
    186256            {
     
    191261            }
    192262
    193             T* pointer_;
    194             OrxonoxClass* base_;
    195             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
    196266    };
    197267
     268    /// Swaps the contents of two weak pointers.
    198269    template <class T>
    199270    void swap(WeakPtr<T>& a, WeakPtr<T>& b)
     
    202273    }
    203274
     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>.
    204276    template <class T, class U>
    205277    WeakPtr<T> static_pointer_cast(const WeakPtr<U>& p)
     
    208280    }
    209281
     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>.
    210283    template <class T, class U>
    211284    WeakPtr<T> const_pointer_cast(const WeakPtr<U>& p)
     
    214287    }
    215288
     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>.
    216290    template <class T, class U>
    217291    WeakPtr<T> dynamic_pointer_cast(const WeakPtr<U>& p)
Note: See TracChangeset for help on using the changeset viewer.