base::SafePtr< T > Class Template Reference

Template class to shield an object being deleting by its users. More...

#include <base_safe_ptr.hh>

List of all members.

Public Member Functions

 SafePtr (T *obj)
 ~SafePtr ()
template<typename T2>
 SafePtr (T2 *obj)
template<typename T2>
 SafePtr (const SafePtr< T2 > ptr)
T * operator-> (void) const
T & operator * (void) const
SafePtr< T > & operator= (T *obj)
bool operator== (const SafePtr &other) const
bool operator!= (const SafePtr &other) const
template<typename T2>
SafePtr< T > & operator= (T2 *obj)
template<typename T2>
SafePtr< T > & operator= (SafePtr< T2 > ptr)
T * PTR (void) const
const T * CONST_PTR (void) const
T & REF (void) const
const T & CONST_REF (void) const

Private Attributes

T * mObj

Friends

class SafePtr


Detailed Description

template<typename T>
class base::SafePtr< T >

Template class to shield an object being deleting by its users.

///
/// Template class to shield an object being deleting by its pointer
/// and to help the object's creator to retain ownership.
/// It prevents the pitfall of passing pointers around
/// and some subroutine deletes it by mistake.
///
/// Examples:
/// ---------
/// SafePtr<Object> object = new Object;
/// SafePtr<const Object> object = new Object;
///
/// Remember:
/// ---------
/// - Return "const SafePtr<>" instead of just "SafePtr<>" (covers pitfall).
/// - Passing "const SafePtr<>" is recommended.
/// - Pass SafePtr<> by value (by reference is actually slower)
/// - Be careful using SafePtr<> in existing code that casts pointers.
///
/// WARNING:
/// --------
/// SafePtr<> suffers from the assignment-to-temporary pitfall.
/// The cure is const: functions should return "const SafePtr<>", not just "SafePtr<>".
/// For example, changing mData to private and interfacing it with
/// a getter method opens the pitfall:
///     SafePtr<Object> GetObject(void);
///     mObject = obj;      // old code
///     GetObject() = obj;  // new code: oops! A temporary Ptr<> object was assigned!
///
/// Design notes:
/// -------------
/// Omitted is a conversion operator from SafePtr<T> to T* as that
/// would defeat the purpose by making "delete ptr" compile.
/// operator bool() is implicitly defined for actual pointers but omitted for SafePtr<>,
/// so writing explicitly writing NULL is required: "if (ptr != NULL)"
/// and the requirement helps finding/eliminating NULL states.
///
/// 

Constructor & Destructor Documentation

template<typename T>
base::SafePtr< T >::SafePtr ( T *  obj  )  [inline]

template<typename T>
base::SafePtr< T >::~SafePtr (  )  [inline]

template<typename T>
template<typename T2>
base::SafePtr< T >::SafePtr ( T2 *  obj  )  [inline]

template<typename T>
template<typename T2>
base::SafePtr< T >::SafePtr ( const SafePtr< T2 >  ptr  )  [inline]


Member Function Documentation

template<typename T>
T* base::SafePtr< T >::operator-> ( void   )  const [inline]

template<typename T>
T& base::SafePtr< T >::operator * ( void   )  const [inline]

template<typename T>
SafePtr<T>& base::SafePtr< T >::operator= ( T *  obj  )  [inline]

template<typename T>
bool base::SafePtr< T >::operator== ( const SafePtr< T > &  other  )  const [inline]

template<typename T>
bool base::SafePtr< T >::operator!= ( const SafePtr< T > &  other  )  const [inline]

template<typename T>
template<typename T2>
SafePtr<T>& base::SafePtr< T >::operator= ( T2 *  obj  )  [inline]

template<typename T>
template<typename T2>
SafePtr<T>& base::SafePtr< T >::operator= ( SafePtr< T2 >  ptr  )  [inline]

template<typename T>
T* base::SafePtr< T >::PTR ( void   )  const [inline]

template<typename T>
const T* base::SafePtr< T >::CONST_PTR ( void   )  const [inline]

template<typename T>
T& base::SafePtr< T >::REF ( void   )  const [inline]

template<typename T>
const T& base::SafePtr< T >::CONST_REF ( void   )  const [inline]


Friends And Related Function Documentation

template<typename T>
friend class SafePtr [friend]


Member Data Documentation

template<typename T>
T* base::SafePtr< T >::mObj [private]


The documentation for this class was generated from the following file: Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:19 2007