AutoAlloc< TYPE > Class Template Reference

#include <ge_autoptr.h>

Detailed Description

template<class TYPE>
class AutoAlloc< TYPE >

This class handles automatic allocation and freeing of objects with static Alloc() and Free() functions. Most classes in the API with private constructors uses this scheme.
Through AutoAlloc it is possible to use these classes with a scope-based life time.
They are allocated when AutoAlloc is created and freed when the scope is exited and AutoAlloc is destroyed:

{
if (!bitmap) return;
// ...
}

Note that the bitmap variable still needs to be checked. If the allocation fails then bitmap == nullptr. Otherwise the bitmap variable can be used just like any pointer.
It is possible to pass parameters to the Alloc() function via AutoAlloc's constructor:

AutoAlloc<VariableTag> hermite(Thermite2d, 100);
Warning
If it is ever needed to give away the allocated object to any other function AutoAlloc cannot be used. It will not release the ownership, the object will still be deleted when the scope is exited.
See also
AutoNew AutoPtr

Public Member Functions

 AutoAlloc ()
 
 AutoAlloc (Int32 id)
 
 AutoAlloc (Int32 p1, Int32 p2)
 
 AutoAlloc (TYPE *initptr)
 
 AutoAlloc (AutoAlloc &&other)
 
AutoAllocoperator= (AutoAlloc &&src)
 
 ~AutoAlloc ()
 
 operator TYPE * () const
 
 operator TYPE & () const
 
TYPE * operator-> () const
 
TYPE *const * operator& () const
 
TYPE * Release ()
 
void Free ()
 
void Assign (TYPE *p)
 

Private Member Functions

const AutoAlloc< TYPE > & operator= (const AutoAlloc< TYPE > &p)
 
 AutoAlloc (const AutoAlloc< TYPE > &p)
 

Private Attributes

TYPE * ptr
 

Constructor & Destructor Documentation

§ AutoAlloc() [1/6]

AutoAlloc ( const AutoAlloc< TYPE > &  p)
private

§ AutoAlloc() [2/6]

AutoAlloc ( )

Calls TYPE::Alloc() and stores the returned pointer internally.

§ AutoAlloc() [3/6]

AutoAlloc ( Int32  id)
explicit

Calls TYPE::Alloc(id) and stores the returned pointer internally.

Parameters
[in]idThe parameter.

§ AutoAlloc() [4/6]

AutoAlloc ( Int32  p1,
Int32  p2 
)

Calls TYPE::Alloc(p1, p2) and stores the returned pointer internally.

Parameters
[in]p1The first parameter.
[in]p2The second parameter.

§ AutoAlloc() [5/6]

AutoAlloc ( TYPE *  initptr)
explicit

Stores the returned pointer internally without calling TYPE::Alloc().

Parameters
[in]initptrA pointer to an already allocated object. The AutoAlloc instance takes over the ownership of the pointed object.

§ AutoAlloc() [6/6]

AutoAlloc ( AutoAlloc< TYPE > &&  other)

Moves the returned pointer internally without calling TYPE::Alloc().

Parameters
[in]otherA pointer to an already allocated object. The AutoAlloc instance takes over the ownership of the pointed object.

§ ~AutoAlloc()

~AutoAlloc ( )

Calls TYPE::Free(ptr), where ptr is the internally stored pointer.

Member Function Documentation

§ operator=() [1/2]

const AutoAlloc<TYPE>& operator= ( const AutoAlloc< TYPE > &  p)
private

§ operator=() [2/2]

AutoAlloc& operator= ( AutoAlloc< TYPE > &&  src)

§ operator TYPE *()

operator TYPE * ( ) const

Conversion to a raw pointer to TYPE. Makes it possible to pass the object directly to functions like Function(TYPE* t).

Returns
The internal pointer. The AutoAlloc instance owns the pointed object.

§ operator TYPE &()

operator TYPE & ( ) const

Conversion to a reference to TYPE. Makes it possible to pass the object directly to functions like Function(TYPE& t).

Note
This dereferences the internal pointer. Hence, it must not be nullptr.
Returns
A reference to the pointed object.

§ operator->()

TYPE* operator-> ( void  ) const

Used for calls like myauto->Function().

Note
This dereferences the internal pointer. Hence, it must not be nullptr.
Returns
The internal pointer. The AutoAlloc instance owns the pointed object.

§ operator&()

TYPE* const* operator & ( ) const

Used for expressions like &myauto. Extracts a pointer to the internal pointer.

Note
This means that it is impossible to get the address of the actual AutoAlloc instance.
Returns
A pointer to the internal pointer. The AutoAlloc instance owns the pointed object.

§ Release()

TYPE* Release ( void  )

Retrieves the internal pointer and then sets it to nullptr. Thus the ownership is transfered to the caller.

Returns
The internal pointer. The caller takes over the ownership of the pointed object.

§ Free()

void Free ( void  )

Calls TYPE::Free(ptr) with the internal pointer and sets it to nullptr.

§ Assign()

void Assign ( TYPE *  p)

Assigns p as the internal pointer.

Parameters
[in]pA pointer to an object allocated with TYPE::Alloc(). The AutoAlloc instance takes over the ownership of the pointed object.

Member Data Documentation

§ ptr

TYPE* ptr
private