Logo Search packages:      
Sourcecode: blender version File versions  Download package

MEM_SmartPtr< T > Class Template Reference

#include <MEM_SmartPtr.h>

List of all members.

Detailed Description

template<class T>
class MEM_SmartPtr< T >


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 explicitely decalre 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 enforcable in C++ but this class attempts to make the 1-1 relationship clear.


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

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 107 of file MEM_SmartPtr.h.

Public Member Functions

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

Private Attributes

T * m_val
 The ptr owned by this class.

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

Generated by  Doxygen 1.6.0   Back to index