RefPtr< RefObjectType, SafeRefObjectType, LifeTimePolicy > Class Template Reference
 
 
 
RefPtr< RefObjectType, SafeRefObjectType, LifeTimePolicy > Class Template Reference

#include <IRefObject.h>

Inheritance diagram for RefPtr< RefObjectType, SafeRefObjectType, LifeTimePolicy >:
MaxHeapOperators

Class Description

template<class RefObjectType, class SafeRefObjectType = SafeReferenceObjectProxy<RefObjectType>, class LifeTimePolicy = DefaultLifeTimePolicy<RefObjectType>>
class MaxSDK::Graphics::RefPtr< RefObjectType, SafeRefObjectType, LifeTimePolicy >

This template class act similar as CComPtr.

Using RefPtr<RefObjectType> will be much safer than RefObjectType* since this template class handles the AddRef and Release in its constructor and destructor.

Usage Example: class DerivedRefObject : public ARefObject { public: void foo(); };

Use DerivedRefObject without RefPtr: { DerivedRefObject* pObject = new DerivedRefObject; pObject->AddRef(); // Add reference pObject->foo(); pObject->Release(); // release the object pObject = NULL; }

Use DerivedRefObject with RefPtr: { RefPtr<DerivedRefObject> pObject = new DerivedRefObject; pObject->foo(); don't need to worry about deleting this object }

More RefPtr examples: RefPtr<DerivedRefObject> pObject2 = new DerivedRefObject; RefPtr<DerivedRefObject> pObject3 = pObject2; pObject2 = NULL; // pObject3 is still valid. pObject3->foo(); pObject3 = NULL; // Release the last reference to the object and the object is destroyed automatically.

Public Types

typedef RefObjectType  ObjectType
typedef RefPtr< RefObjectType,
SafeRefObjectType,
LifeTimePolicy > 
SelfType

Public Member Functions

  RefPtr ()
  RefPtr (RefObjectType *pPtr)
  RefPtr (const SelfType &other)
template<class OtherInterface , class OtherSafeRefObjectType , class OtherLifeTimePolicy >
  RefPtr (const RefPtr< OtherInterface, OtherSafeRefObjectType, OtherLifeTimePolicy > &other)
template<class OtherInterface >
  RefPtr (OtherInterface *pPtr)
  ~RefPtr ()
void  Swap (SelfType &other)
void  Release ()
RefObjectType &  operator* () const
RefObjectType *  GetPointer () const
SafeRefObjectType *  operator-> () const
RefObjectType *  operator= (RefObjectType *pPtr)
RefObjectType *  operator= (const SelfType &other)
template<class OtherInterface >
RefObjectType *  operator= (OtherInterface *pPtr)
template<class OtherInterface , class OtherSafeRefObjectType , class OtherLifeTimePolicy >
RefObjectType *  operator= (const RefPtr< OtherInterface, OtherSafeRefObjectType, OtherLifeTimePolicy > &other)
bool  operator! () const
bool  operator< (RefObjectType *pPtr) const
bool  operator< (const SelfType &pPtr) const
bool  operator== (RefObjectType *pPtr) const
bool  operator== (const SelfType &pPtr) const
bool  operator!= (RefObjectType *pPtr) const
bool  operator!= (const SelfType &pPtr) const

Member Typedef Documentation

typedef RefPtr<RefObjectType, SafeRefObjectType, LifeTimePolicy> SelfType

Constructor & Destructor Documentation

RefPtr ( ) [inline]
        {
                mpObject = NULL;
        }
RefPtr ( RefObjectType *  pPtr ) [inline]
        {
                mpObject = pPtr;
                if (mpObject != NULL)
                {
                        LifeTimePolicy::AddRef(mpObject);
                }
        }
RefPtr ( const SelfType other ) [inline]
        {
                mpObject = other.mpObject;
                if (mpObject != NULL)
                {
                        LifeTimePolicy::AddRef(mpObject);
                }
        }
RefPtr ( const RefPtr< OtherInterface, OtherSafeRefObjectType, OtherLifeTimePolicy > &  other ) [inline, explicit]
        {
                if (other == NULL)
                {
                        mpObject = NULL;
                        return;
                }
                mpObject = dynamic_cast<RefObjectType*>(other.GetPointer());
                if (NULL != mpObject)
                {
                        LifeTimePolicy::AddRef(mpObject);
                }
        }
RefPtr ( OtherInterface *  pPtr ) [inline, explicit]
        {
                if (pPtr == NULL)
                {
                        mpObject = NULL;
                }
                else
                {
                        mpObject = dynamic_cast<RefObjectType*>(pPtr);
                }
                if (NULL != mpObject)
                {
                        LifeTimePolicy::AddRef(mpObject);
                }
        }
~RefPtr ( ) [inline]
        {
                Release();
        }

Member Function Documentation

void Swap ( SelfType other ) [inline]
        {
                RefObjectType* pTemp = mpObject;
                mpObject = other.mpObject;
                other.mpObject = pTemp;
        }
void Release ( ) [inline]
        {
                RefObjectType* pTemp = mpObject;
                if (mpObject)
                {
                        mpObject = NULL;
                        LifeTimePolicy::Release(pTemp);
                }
        }
RefObjectType& operator* ( ) const [inline]
        {
                return *mpObject;
        }
RefObjectType* GetPointer ( ) const [inline]
        {
                return mpObject;
        }
SafeRefObjectType* operator-> ( ) const [inline]
        {
                return (SafeRefObjectType*)mpObject;
        }
RefObjectType* operator= ( RefObjectType *  pPtr ) [inline]
        {
                if (pPtr != NULL)
                {
                        LifeTimePolicy::AddRef(pPtr);
                }
                if (mpObject)
                {
                        LifeTimePolicy::Release(mpObject);
                }
                mpObject = pPtr;
                return pPtr;
        }
RefObjectType* operator= ( const SelfType other ) [inline]
        {
                RefObjectType* pPtr = other.mpObject;
                if (pPtr != NULL)
                {
                        LifeTimePolicy::AddRef(pPtr);
                }
                if (mpObject)
                {
                        LifeTimePolicy::Release(mpObject);
                }
                mpObject = pPtr;
                return pPtr;
        }
RefObjectType* operator= ( OtherInterface *  pPtr ) [inline]
        {
                RefObjectType* pBackup = mpObject;
                if (pPtr != NULL)
                {
                        mpObject = dynamic_cast<RefObjectType*>(pPtr);
                }
                else
                {
                        mpObject = NULL;
                }
                if (NULL != mpObject)
                {
                        LifeTimePolicy::AddRef(mpObject);
                }

                if (pBackup != NULL)
                {
                        LifeTimePolicy::Release(pBackup);
                }
                return mpObject;
        }
RefObjectType* operator= ( const RefPtr< OtherInterface, OtherSafeRefObjectType, OtherLifeTimePolicy > &  other ) [inline]
        {
                RefObjectType* pBackup = mpObject;
                if (other != NULL)
                {
                        mpObject = dynamic_cast<RefObjectType*>(other.GetPointer());
                }
                else
                {
                        mpObject = NULL;
                }
                if (NULL != mpObject)
                {
                        LifeTimePolicy::AddRef(mpObject);
                }

                if (pBackup != NULL)
                {
                        LifeTimePolicy::Release(pBackup);
                }
                return mpObject;
        }
bool operator! ( ) const [inline]
        {
                return mpObject == NULL;
        }
bool operator< ( RefObjectType *  pPtr ) const [inline]
        {
                return mpObject < pPtr;
        }
bool operator< ( const SelfType pPtr ) const [inline]
        {
                return mpObject < pPtr.mpObject;
        }
bool operator== ( RefObjectType *  pPtr ) const [inline]
        {
                return mpObject == pPtr;
        }
bool operator== ( const SelfType pPtr ) const [inline]
        {
                return mpObject == pPtr.mpObject;
        }
bool operator!= ( RefObjectType *  pPtr ) const [inline]
        {
                return mpObject != pPtr;
        }
bool operator!= ( const SelfType pPtr ) const [inline]
        {
                return mpObject != pPtr.mpObject;
        }