#include <smartptr.h>
Inheritance diagram for SmartPtr:
Public Member Functions | |
SmartPtr () | |
Construct a smart pointer that points to nothing. | |
SmartPtr (IRefCount< T > *ptr) | |
Construct a smart pointer to an object. | |
SmartPtr (const SmartPtr< T > &sp) | |
Construct a smart pointer that takes its target from another smart pointer. | |
virtual | ~SmartPtr () |
Detatch this pointer from the object before destruction. | |
T * | GetPtr () const |
Get the contained pointer. | |
IRefCount< T > * | GetRef () const |
Get the contained object's refcount. | |
SmartPtr & | operator= (const SmartPtr< T > &sp) |
Assign another smart pointer. | |
SmartPtr & | operator= (IRefCount< T > *ptr) |
Assign pointer or NULL. | |
T * | operator-> () |
Give access to members of T. | |
const T * | operator-> () const |
Give const access to members of T. | |
operator T * () const | |
Conversion to T* (for function calls). | |
bool | operator! () |
Test for NULL. | |
bool | operator== (const SmartPtr &sp) |
Test for equality (ie. do both pointers point to the same object). | |
bool | operator!= (const SmartPtr &sp) |
Test for inequality (ie. do pointers point to different objects). | |
bool | operator< (SmartPtr &Other) |
Comparison function to allow sorting by indexed value. | |
template<class U> | |
U * | Cast (U *) |
Get a cast version of the pointer. | |
Protected Member Functions | |
virtual void | __Assign (IRefCount< T > *refcount) |
Assign a 'smart' object to this smart pointer. | |
Protected Attributes | |
IRefCount< T > * | __m_refcount |
Pointer to the reference counted object. | |
Classes | |
class | __RefCounter |
Default IRefCount implementation used by SmartPtr. More... |
Usage:
1. In a program block
SmartPtr<MyClass> ptr1(new MyClass); // creates object 1
SmartPtr<MyClass> ptr2(new MyClass); // creates object 2
ptr1 = ptr2; // destroys object 1
ptr2 = NULL;
ptr1 = new MyClass; // creates object 3, destroys object 2
ptr1->methodcall(...);
MyClass o1;
// ptr1 = &o1; // DON'T ! only memory allocated by new operator should be used
CMyObject *o2 = new MyClass;
ptr1 = o2;
//ptr2 = o2; // DON'T ! unless MyClass implements IRefCount
// try to use ptr1 = ptr2 instead, it's always safe;
2. In a function call
void func(MyClass *o) {...}
...
SmartPtr<MyClass> ptr(new MyClass);
func(ptr);
3. As a return value
SmartPtr<MyClass> f()
{
SmartPtr<MyClass> ptr(new MyClass);
return ptr;
}
4. Accessing members
SmartPtr<MyClass> ptr(new MyClass);
ptr->ClassMember = 0;
SmartPtr | ( | ) | [inline] |
Construct a smart pointer that points to nothing.
Construct a smart pointer that takes its target from another smart pointer.
virtual ~SmartPtr | ( | ) | [inline, virtual] |
Detatch this pointer from the object before destruction.
virtual void __Assign | ( | IRefCount< T > * | refcount | ) | [inline, protected, virtual] |
Assign a 'smart' object to this smart pointer.
This method is picked over __Assign(void *ptr) if T implements IRefCount. This allows some memory usage optimization
Reimplemented in ParentPtr, ParentPtr< mxflib::Partition >, ParentPtr< mxflib::MDObject >, ParentPtr< mxflib::Track >, and ParentPtr< mxflib::Package >.
T* GetPtr | ( | ) | const [inline] |
Get the contained pointer.
IRefCount<T>* GetRef | ( | ) | const [inline] |
Get the contained object's refcount.
Assign another smart pointer.
Reimplemented in ParentPtr, ParentPtr< mxflib::Partition >, ParentPtr< mxflib::MDObject >, ParentPtr< mxflib::Track >, and ParentPtr< mxflib::Package >.
T* operator-> | ( | ) | [inline] |
Give access to members of T.
const T* operator-> | ( | ) | const [inline] |
Give const access to members of T.
operator T * | ( | ) | const [inline] |
Conversion to T* (for function calls).
bool operator! | ( | ) | [inline] |
Test for NULL.
bool operator== | ( | const SmartPtr< T > & | sp | ) | [inline] |
Test for equality (ie. do both pointers point to the same object).
bool operator!= | ( | const SmartPtr< T > & | sp | ) | [inline] |
Test for inequality (ie. do pointers point to different objects).
bool operator< | ( | SmartPtr< T > & | Other | ) | [inline] |
Comparison function to allow sorting by indexed value.
U* Cast | ( | U * | ) | [inline] |
Get a cast version of the pointer.
This is used via the SmartPtr_Cast() Macro to allow MSVC 6 to work!! The reason for this is that MSVC 6 name mangling is only based on the function arguments so it cannot cope when two functions differ in the template type, but not the argument list!! The solution is a dummy argument that gets filled in by the macro (to avoid messy code!)
IRefCount<T>* __m_refcount [protected] |
Pointer to the reference counted object.