Blender V2.61 - r43446
Public Member Functions

MEM_SmartPtr< T > Class Template Reference

#include <MEM_SmartPtr.h>

Inheritance diagram for MEM_SmartPtr< T >:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 MEM_SmartPtr (const MEM_SmartPtr &rhs)
 MEM_SmartPtr (T *val)
 MEM_SmartPtr ()
 operator T * () const
T & Ref () const
MEM_SmartPtroperator= (const MEM_SmartPtr &rhs)
T * operator-> () const
T * Release () const
void Delete ()
 ~MEM_SmartPtr ()

Detailed Description

template<class T>
class MEM_SmartPtr< T >

MEM_SmartPtr

This class defines a smart pointer similar to that defined in the Standard Template Library but without the painful get() semantics to access the internal c style pointer.

It is often useful to explicitly declare ownership of memory allocated on the heap within class or function scope. This class helps you to encapsulate this ownership within a value type. When an instance of this class goes out of scope it makes sure that any memory associated with it's internal pointer is deleted. It can help to inform users of an aggregate class that it owns instances of it's members and these instances should not be shared. This is not reliably enforceable in C++ but this class attempts to make the 1-1 relationship clear.

subclass

class foo { ...constructors accessors etc. int x[1000]; }

class bar { public : static bar * New( ) { MEM_SmartPtr<foo> afoo = new foo(); MEM_SmartPtr<bar> that = new bar();

if (foo == NULL || that == NULL) return NULL;

that->m_foo = afoo.Release(); return that.Release(); }

~bar() { // smart ptr takes care of deletion } private : MEM_SmartPtr<foo> m_foo; }

You may also safely construct vectors of MEM_SmartPtrs and have the vector own stuff you put into it.

e.g. { std::vector<MEM_SmartPtr<foo> > foo_vector; foo_vector.push_back( new foo()); foo_vector.push_back( new foo());

foo_vector[0]->bla(); } // foo_vector out of scope => heap memory freed for both foos

Warning:
this class should only be used for objects created on the heap via the new function. It will not behave correctly if you pass ptrs to objects created with new[] nor with objects declared on the stack. Doing this is likely to crash the program or lead to memory leaks.

Definition at line 111 of file MEM_SmartPtr.h.


Constructor & Destructor Documentation

template<class T>
MEM_SmartPtr< T >::MEM_SmartPtr ( const MEM_SmartPtr< T > &  rhs) [inline]

Construction from reference - this class always assumes ownership from the rhs.

Definition at line 120 of file MEM_SmartPtr.h.

template<class T>
MEM_SmartPtr< T >::MEM_SmartPtr ( T *  val) [inline]

Construction from ptr - this class always assumes that it now owns the memory associated with the ptr.

Definition at line 132 of file MEM_SmartPtr.h.

template<class T>
MEM_SmartPtr< T >::MEM_SmartPtr ( ) [inline]

Defalut constructor

Definition at line 143 of file MEM_SmartPtr.h.

template<class T>
MEM_SmartPtr< T >::~MEM_SmartPtr ( ) [inline]

Destructor - deletes object if it exists

Definition at line 229 of file MEM_SmartPtr.h.


Member Function Documentation

template<class T>
void MEM_SmartPtr< T >::Delete ( ) [inline]

Force destruction of the internal object.

Definition at line 219 of file MEM_SmartPtr.h.

template<class T>
MEM_SmartPtr< T >::operator T * ( ) const [inline]

Type conversion from this class to the type of a pointer to the template parameter. This means you can pass an instance of this class to a function expecting a ptr of type T.

Definition at line 156 of file MEM_SmartPtr.h.

template<class T>
T* MEM_SmartPtr< T >::operator-> ( ) const [inline]

Overload the operator -> so that it's possible to access all the normal methods of the internal ptr.

Definition at line 197 of file MEM_SmartPtr.h.

template<class T>
MEM_SmartPtr& MEM_SmartPtr< T >::operator= ( const MEM_SmartPtr< T > &  rhs) [inline]

Assignment operator - ownership is transfered from rhs to lhs. There is an intenional side-effect of function of transferring ownership from the const parameter rhs. This is to insure the 1-1 relationship. The object associated with this instance is deleted if it is not the same as that contained in the rhs.

Definition at line 181 of file MEM_SmartPtr.h.

template<class T>
T& MEM_SmartPtr< T >::Ref ( ) const [inline]
template<class T>
T* MEM_SmartPtr< T >::Release ( void  ) const [inline]

The documentation for this class was generated from the following file: