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;
// ...
}
Definition: ge_autoptr.h:37

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)
 
 MAXON_OPERATOR_MOVE_ASSIGNMENT (AutoAlloc)
 
 ~AutoAlloc ()
 
 operator const TYPE * () const
 
 operator TYPE * ()
 
const TYPEGetPointer () const
 
TYPEGetPointer ()
 
 operator const TYPE & () const
 
 operator TYPE & ()
 
const TYPEoperator-> () const
 
TYPEoperator-> ()
 
const TYPE *const * operator& () const
 
TYPE *const * operator& ()
 
TYPERelease ()
 
void Free ()
 
void Assign (TYPE *p)
 

Private Member Functions

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

Private Attributes

TYPEptr
 

Friends

MAXON_ATTRIBUTE_FORCE_INLINE friend void operator% (const AutoAlloc< TYPE > &alloc, maxon::ThreadReferencedError &err)
 

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=()

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

◆ MAXON_OPERATOR_MOVE_ASSIGNMENT()

MAXON_OPERATOR_MOVE_ASSIGNMENT ( AutoAlloc< TYPE )

◆ operator const TYPE *()

operator const 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 * ( )

◆ GetPointer() [1/2]

const TYPE* GetPointer ( ) const

◆ GetPointer() [2/2]

TYPE* GetPointer ( )

◆ operator const TYPE &()

operator const 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 TYPE & ( )

◆ operator->() [1/2]

const TYPE* operator-> ( ) 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->() [2/2]

TYPE* operator-> ( )

◆ operator&() [1/2]

const 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.

◆ operator&() [2/2]

TYPE* const* operator& ( )

◆ Release()

TYPE* Release ( )

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 ( )

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.

Friends And Related Function Documentation

◆ operator%

MAXON_ATTRIBUTE_FORCE_INLINE friend void operator% ( const AutoAlloc< TYPE > &  alloc,
maxon::ThreadReferencedError err 
)
friend

Member Data Documentation

◆ ptr

TYPE* ptr
private