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/SmartPtr.h

    r7363 r7373  
    3737    @file
    3838    @ingroup Object SmartPtr
     39    @brief Definition of SmartPtr<T>.
     40
     41    @anchor SmartPtrExample
     42
     43    orxonox::SmartPtr is an implementation of a smart pointer - it wraps a pointer to an
     44    object  and keeps this object alive until no SmartPtr points to this object anymore.
     45    In contrast to orxonox::SharedPtr, SmartPtr works only with classes that are derived
     46    from orxonox::OrxonoxClass, because it's an intrusive implementation, meaning the
     47    reference counter is stored in the object itself.
     48
     49    It's possible to use normal pointers and smart pointers to an object simultaneously.
     50    You don't have to use SmartPtr all the time, you can create a SmartPtr for an object
     51    at any time and also convert it back to a normal pointer if you like. This is possible
     52    because the reference counter is stored in the object itself and not in SmartPtr (in
     53    contrast to SharedPtr).
     54
     55    @b Important: If you want to delete an object, you must not use @c delete @c object but
     56    rather @c object->destroy(). This function will check if there are smart pointers
     57    pointing to the object. If yes, the object will be kept alive until all smart pointes
     58    are destroyed. If no, the object is deleted instantly.
     59
     60    If all smart pointers that point to an object are destroyed, but you never called
     61    @c object->destroy() before, the object will not be deleted! All a SmartPtr will do
     62    is to really just keep an object alive, but it will not delete it automatically
     63    unless you tried to destroy it before.
     64
     65    Example:
     66    @code
     67    class MyClass                                           // class declaration
     68    {
     69        public:
     70            void setObject(OtherClass* object)              // passes a normal pointer which will be stored in a SmartPtr
     71                { this->object_ = object; }
     72
     73            OtherClass* getObject() const                   // converts the SmartPtr to a normal pointer and returns it
     74                { return this->object_; }
     75
     76        private:
     77            SmartPtr<OtherClass> object_;                   // a pointer to an instance of OtherClass is stored in a SmartPtr
     78    };
     79    @endcode
     80    In this example we assume that OtherClass is a child of OrxonoxClass. We don't care
     81    about the inheritance of MyClass though.
     82
     83    Now we create an instance of MyClass and assign a pointer to an instance of OtherClass:
     84    @code
     85    MyClass* myclass = new MyClass();                       // create an instance of MyClass
     86    OtherClass* object = new OtherClass();                  // create an instance of OtherClass
     87    myclass->setObject(object);                             // the object is now stored in a SmartPtr inside myclass
     88
     89    object->destroy();                                      // we try to destroy object, but there's still a SmartPtr pointing at it.
     90
     91    # object still exists at this point (because a SmartPtr points at it)
     92
     93    delete myclass;                                         // now we delete myclass, which also destroys the SmartPtr
     94
     95    # object doesn't exist anymore (because the SmartPtr is now destroyed)
     96    @endcode
     97
     98    Now we look at the same example, but we first delete myclass, then destroy object:
     99    @code
     100    MyClass* myclass = new MyClass();                       // create an instance of MyClass
     101    OtherClass* object = new OtherClass();                  // create an instance of OtherClass
     102    myclass->setObject(object);                             // the object is now stored in a SmartPtr inside myclass
     103
     104    delete myclass;                                         // we delete myclass, which also destroys the SmartPtr
     105
     106    # object still exists at this point (because destroy() was not called yet)
     107
     108    object->destroy();                                      // now we try to destroy object, which works instantly
     109
     110    # object doesn't exist anymore (because we just destroyed it)
     111    @endcode
     112
     113    Note that in any case @c object->destroy() has to be called to delete the object.
     114    However if a SmartPtr points at it, the destruction is delayed until all SmartPtr
     115    are destroyed.
    39116*/
    40117
     
    52129namespace orxonox
    53130{
     131    /**
     132        @brief A smart pointer which wraps a pointer to an object and keeps this object alive as long as the smart pointer exists.
     133
     134        @see See @ref SmartPtrExample "this description" for more information and an example.
     135    */
    54136    template <class T>
    55137    class SmartPtr
    56138    {
    57139        public:
     140            /// Constructor: Initializes the smart pointer with a null pointer.
    58141            inline SmartPtr() : pointer_(0), base_(0)
    59142            {
    60143            }
    61144
     145            /// Constructor: Used to explicitly initialize the smart pointer with a null pointer
    62146            inline SmartPtr(int) : pointer_(0), base_(0)
    63147            {
    64148            }
    65149
     150            /// Constructor: Initializes the smart pointer with a pointer to an object. @param pointer The pointer @param bAddRef If true, the reference counter is increased. Don't set this to false unless you know exactly what you're doing! (for example to avoid circular references if the @c this pointer of the possessing object is stored)
    66151            inline SmartPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer)
    67152            {
     
    70155            }
    71156
     157            /// Copy-constructor
    72158            inline SmartPtr(const SmartPtr& other) : pointer_(other.pointer_), base_(other.base_)
    73159            {
     
    76162            }
    77163
     164            /// Copy-constructor for smart pointers to objects of another class.
    78165            template <class O>
    79166            inline SmartPtr(const SmartPtr<O>& other) : pointer_(other.get()), base_(other.base_)
     
    83170            }
    84171
     172            /// Constructor: Initializes the smart pointer with the pointer that is stored in a WeakPtr.
    85173            template <class O>
    86174            inline SmartPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.getBase())
     
    90178            }
    91179
     180            /// Destructor: Decrements the reference counter.
    92181            inline ~SmartPtr()
    93182            {
     
    96185            }
    97186
     187            /// Used to assign a null pointer.
    98188            inline SmartPtr& operator=(int)
    99189            {
     
    102192            }
    103193
     194            /// Assigns a new pointer.
    104195            inline SmartPtr& operator=(T* pointer)
    105196            {
     
    108199            }
    109200
     201            /// Assigns the wrapped pointer of another SmartPtr.
    110202            inline SmartPtr& operator=(const SmartPtr& other)
    111203            {
     
    114206            }
    115207
     208            /// Assigns the wrapped pointer of a SmartPtr of another class
    116209            template <class O>
    117210            inline SmartPtr& operator=(const SmartPtr<O>& other)
     
    121214            }
    122215
     216            /// Assigns the wrapped pointer of a WeakPtr.
    123217            template <class O>
    124218            inline SmartPtr& operator=(const WeakPtr<O>& other)
     
    128222            }
    129223
     224            /// Returns the wrapped pointer as @c T*
    130225            inline T* get() const
    131226            {
     
    133228            }
    134229
     230            /// Returns the wrapped pointer as @c OrxonoxClass*
    135231            inline OrxonoxClass* getBase() const
    136232            {
     
    138234            }
    139235
     236            /// Implicitly converts the SmartPtr to a pointer of type @c T*
    140237            inline operator T*() const
    141238            {
     
    143240            }
    144241
     242            /// Overloaded operator, returns a pointer to the stored object.
    145243            inline T* operator->() const
    146244            {
     
    149247            }
    150248
     249            /// Overloaded operator, returns a reference to the stored object.
    151250            inline T& operator*() const
    152251            {
     
    155254            }
    156255
     256            /// Returns true if the wrapped pointer is NULL.
    157257            inline bool operator!() const
    158258            {
     
    160260            }
    161261
     262            /// Swaps the contents of two smart pointers.
    162263            inline void swap(SmartPtr& other)
    163264            {
     
    174275            }
    175276
     277            /// Resets the smart pointer (equivalent to assigning a NULL pointer).
    176278            inline void reset()
    177279            {
     
    180282
    181283        private:
    182             T* pointer_;
    183             OrxonoxClass* base_;
     284            T* pointer_;            ///< The wrapped pointer to an object of type @a T
     285            OrxonoxClass* base_;    ///< The wrapped pointer, casted up to OrxonoxClass (this is needed because with just a T* pointer, SmartPtr couln't be used with forward declarations)
    184286    };
    185287
     288    /// Swaps the contents of two smart pointers.
    186289    template <class T>
    187290    void swap(SmartPtr<T>& a, SmartPtr<T>& b)
     
    190293    }
    191294
     295    /// Uses a static_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new SmartPtr<T>.
    192296    template <class T, class U>
    193297    SmartPtr<T> static_pointer_cast(const SmartPtr<U>& p)
     
    196300    }
    197301
     302    /// Uses a const_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new SmartPtr<T>.
    198303    template <class T, class U>
    199304    SmartPtr<T> const_pointer_cast(const SmartPtr<U>& p)
     
    202307    }
    203308
     309    /// Uses a dynamic_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new SmartPtr<T>.
    204310    template <class T, class U>
    205311    SmartPtr<T> dynamic_pointer_cast(const SmartPtr<U>& p)
Note: See TracChangeset for help on using the changeset viewer.