base::UniquePtr< T > Class Template Reference

Smart pointer that contrains objects to having unique values. More...

#include <base_unique_ptr.hh>

Inheritance diagram for base::UniquePtr< T >:

base::Threadable

List of all members.

Public Member Functions

 UniquePtr (const T *obj)
 UniquePtr (const T &obj)
 ~UniquePtr ()
const UniquePtroperator= (const T *obj)
const UniquePtroperator= (const T &obj)
const T * operator-> (void) const
const T & operator * (void) const
bool operator== (const UniquePtr &other) const
bool operator!= (const UniquePtr &other) const
bool operator== (const T *obj) const
bool operator!= (const T *obj) const
bool operator== (const T &obj) const
bool operator!= (const T &obj) const
const T * CONST_PTR (void) const
const T & CONST_REF (void) const

Private Attributes

SafePtr< const T > mObj


Detailed Description

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

Smart pointer that contrains objects to having unique values.

///
/// Overview:
/// ---------
/// The problem that UniquePtr solves is wasted memory caused by constructors
/// that create a multitude of objects having the same value.
/// UniquePtr is given an object, and if an one exists that has the same value,
/// it frees the given one and references the existing one.  In other words,
/// UniquePtr discards duplicate objects.
///
/// Usage:
/// ------
/// UniquePtr is a self-managing smart pointer that owns its object.
/// It can be used where SharedPtr would be.
/// Construct it by passing a pointer to a new object or a reference.
///
/// When given a pointer, UniquePtr takes ownership of an object,
/// and will delete it if it's a duplicate.
///
/// UniquePtr can be given a reference.  This form is useful for assigning
/// a UniquePtr from an object passed by reference to a function.
///
/// Requirements:
/// -------------
/// - Class must derived from Unique (private).
/// - Class must be copyable.
/// - Class must define operator<().
/// - Class must define UniqueFactory& GetUniqueFactory(void) as a class method.
/// - Client must treat UniquePtr as a pointer to a const object.
///   Otherwise, modifying an unique object could turn it into a duplicate.
///
/// Factory object:
/// ---------------
/// UniquePtr needs access to a factory object to get an unique object.
/// Since the factory object is a form of shared data for the class,
/// the appropriate place for a factory object is as class member.
/// UniquePtr accesses it via T::GetUniqueFactory().
///
/// Examples:
/// ---------
///
/// Declaring a class with required support for UniquePtr:
///
///   class Color4 : public Unique  // derived from Unique (similar to Shared)
///   {
///   public:
///       CLASS_METHOD UniqueFactory<Color4>& GetUniqueFactory( void ) { return msUniqueFactory; }
///   private:
///       CLASS_VAR UniqueFactory<Color4> msUniqueFactory;
///   };
///   UniqueFactory<Color4> Color4::msUniqueFactory;
///
/// Constructing from a pointer:
///
///   UniquePtr<Color4> color4 = new Color4( rgba0, rgba1, rgba2, rgba3 );
///
/// Constructing from a reference:
///
///   class Quad( void )
///   {
///       void SetColors( const Color4& color4 )
///       {
///           mColor4 = color4;
///       }
///
///       UniquePtr<Color4>  mColor4;
///   };
///
/// UniquePtr can be used in the same ways as SharedPtr:
///
///   UniquePtr<Color4>
///   RandomColor( void )
///   {
///       return new Color4(...);  // invokes UniquePtr(T*)
///       [or]
///       return Color4(...);      // invokes UniquePtr(T&)
///   }
///
/// SharedPtr vs. UniquePtr:
/// ------------------------
/// A object can safely begin as a SharedPtr and then be turned into a UniquePtr.
/// UniquePtr will store a copy it owns that is independent of SharedPtr.
/// This mixing is useful for building an object as a SharedPtr then storing
/// its completed state as a UniquePtr.
///
/// Implementation:
/// ---------------
/// As an optimization and to simplify the implementation, objects stay in the STL set
/// whether still used or not.  Implementing reference-counting and auto-deletion
/// would add too much complexity.  Auto-deletion might save memory but trade-off speed
/// by repeatedly re-constructing and re-inserting the same values.
///
/// Requiring client classes to implement GetUniqueFactory() is intrusive,
/// but writing "UniquePtr<Object>" is done far more than writing classes.
/// UniquePtr can access the factory object by itself, so it doesn't requiring
/// being explicitly passed the factory object as non-intrusive alternatives would.
///
/// 

Constructor & Destructor Documentation

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

template<typename T>
base::UniquePtr< T >::UniquePtr ( const T &  obj  )  [inline]

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


Member Function Documentation

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

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

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

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

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

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

template<typename T>
bool base::UniquePtr< T >::operator== ( const T *  obj  )  const [inline]

template<typename T>
bool base::UniquePtr< T >::operator!= ( const T *  obj  )  const [inline]

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

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

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

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


Member Data Documentation

template<typename T>
SafePtr<const T> base::UniquePtr< 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:20 2007