Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ion::base::ScopedAllocation< T > Class Template Reference

This template class can be used in situations where you want to allocate an object that is not necessarily derived from Allocatable, but you still want to use an Allocator for the memory. More...

#include "scopedallocation.h"

Public Member Functions

 ScopedAllocation (const AllocatorPtr &allocator)
 This constructor allocates a single T instance using allocator, or the default allocator if a NULL pointer is passed. More...
 
 ScopedAllocation (AllocationLifetime lifetime)
 This constructor allocates a single T instance using the default allocator for lifetime. More...
 
 ScopedAllocation (const AllocatorPtr &allocator, size_t count)
 This constructor allocates count T instances using allocator, or the default allocator if a NULL pointer is passed. More...
 
 ScopedAllocation (AllocationLifetime lifetime, size_t count)
 This constructor allocates count T instances the default allocator for lifetime. More...
 
 ~ScopedAllocation ()
 The destructor deletes the instance(s) using the allocator. More...
 
TGet () const
 Returns a pointer to the allocated T instance(s). More...
 
DataContainerPtr TransferToDataContainer (bool is_wipeable)
 Creates a DataContainer of the same type and transfers the data from the ScopedAllocation to it. More...
 

Detailed Description

template<typename T>
class ion::base::ScopedAllocation< T >

This template class can be used in situations where you want to allocate an object that is not necessarily derived from Allocatable, but you still want to use an Allocator for the memory.

Note that T must be default-constructable.

Examples: ///< Allocating a local variable: ScopedAllocation<Thing> s1(allocator); ///< Creates a Thing instance. DoSomethingWithAThing(s1.Get()); ///< Accesses the instance. ///< When s1 goes out of scope, the Thing is deleted using the Allocator.

///< Allocating a local POD array: ScopedAllocation<char> s2(allocator, 9); ///< Allocates an array of size 9. printf(s2.Get()); ///< Accesses the array. ///< When s2 goes out of scope, the array is deleted using the Allocator.

///< Transferring the contents of a ScopedAllocation to a DataContainer that ///< uses the same Allocator. (This can be useful for allocating an array of ///< objects with an Allocator for use in a DataContainer.): ScopedAllocation<int> s3(allocator, 9); ... ///< Fill in the data inside the array. DataContainerPtr dc = s3.TransferToDataContainer(true); ///< s3 is now empty; dc contains the data.

Note
a lifetime can be used instead of an Allocator pointer, in which case the default allocator for that lifetime is used.

Definition at line 57 of file scopedallocation.h.

Constructor & Destructor Documentation

template<typename T>
ion::base::ScopedAllocation< T >::ScopedAllocation ( const AllocatorPtr allocator)
inlineexplicit

This constructor allocates a single T instance using allocator, or the default allocator if a NULL pointer is passed.

Definition at line 61 of file scopedallocation.h.

template<typename T>
ion::base::ScopedAllocation< T >::ScopedAllocation ( AllocationLifetime  lifetime)
inlineexplicit

This constructor allocates a single T instance using the default allocator for lifetime.

Definition at line 67 of file scopedallocation.h.

References ion::base::AllocationManager::GetDefaultAllocatorForLifetime().

template<typename T>
ion::base::ScopedAllocation< T >::ScopedAllocation ( const AllocatorPtr allocator,
size_t  count 
)
inline

This constructor allocates count T instances using allocator, or the default allocator if a NULL pointer is passed.

Definition at line 73 of file scopedallocation.h.

template<typename T>
ion::base::ScopedAllocation< T >::ScopedAllocation ( AllocationLifetime  lifetime,
size_t  count 
)
inline

This constructor allocates count T instances the default allocator for lifetime.

Definition at line 79 of file scopedallocation.h.

References ion::base::AllocationManager::GetDefaultAllocatorForLifetime().

template<typename T>
ion::base::ScopedAllocation< T >::~ScopedAllocation ( )
inline

The destructor deletes the instance(s) using the allocator.

Definition at line 84 of file scopedallocation.h.

References allocator_.

Member Function Documentation

template<typename T>
T* ion::base::ScopedAllocation< T >::Get ( ) const
inline

Returns a pointer to the allocated T instance(s).

This returns NULL if a count of 0 was passed to the constructor.

Definition at line 90 of file scopedallocation.h.

Referenced by ion::base::ZipAssetManager::RegisterAssetData().

template<typename T>
DataContainerPtr ion::base::ScopedAllocation< T >::TransferToDataContainer ( bool  is_wipeable)
inline

Creates a DataContainer of the same type and transfers the data from the ScopedAllocation to it.

After this call, the ScopedAllocation will be empty, meaning that Get() will return a NULL pointer.

Definition at line 95 of file scopedallocation.h.

References allocator_.


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