type_pool  1.2
 All Classes Files Functions Typedefs
Public Types | Public Member Functions
type_pool< T, Allocator > Class Template Reference

The only class you need. More...

#include <type_pool.h>

Inheritance diagram for type_pool< T, Allocator >:
Inheritance graph
[legend]
Collaboration diagram for type_pool< T, Allocator >:
Collaboration graph
[legend]

List of all members.

Public Types

typedef std::unique_ptr< T,
std::function< void(T *)> > 
unique_ptr
 Re-mapping of std::unqiue_ptr<T> to hide away the signature of the deleter.

Public Member Functions

 type_pool (size_t preallocate=0, size_t cap=chunk_pool< sizeof(T), Allocator >::max_size())
 Constructor.
template<typename U >
std::enable_if< std::is_same
< T, U >::value, T * >::type 
get_ptr (U &&t)
 Returns a pointer to an instance of T.
template<typename U , typename... Args>
std::enable_if< std::is_same
< T, U >::value, T * >::type 
get_ptr (Args &&...args)
 Returns a pointer to an instance of T.
template<typename U >
std::enable_if< std::is_same
< T, U >::value,
std::shared_ptr< T > >::type 
get_shared (U &&t)
 Wraps a raw pointer returned from get_ptr in a std::shared_ptr<T>.
template<typename U , typename... Args>
std::enable_if< std::is_same
< T, U >::value,
std::shared_ptr< T > >::type 
get_shared (Args &&...args)
 Wraps a raw pointer returned from get_ptr in a std::shared_ptr<T>.
template<typename U >
std::enable_if< std::is_same
< T, U >::value, typename
type_pool< T, Allocator >
::unique_ptr >::type 
get_unique (U &&t)
 Wraps a raw pointer returned from get_ptr in a type_pool<T>::unique_ptr. Use the return value as you would a std::unique_ptr<T>.
template<typename U , typename... Args>
std::enable_if< std::is_same
< T, U >::value, typename
type_pool< T, Allocator >
::unique_ptr >::type 
get_unique (Args &&...args)
 Wraps a raw pointer returned from get_ptr in a type_pool<T>::unique_ptr. Use the return value as you would a std::unique_ptr<T>.

Detailed Description

template<typename T, class Allocator = typename chunk_pool<sizeof(T)>::allocator_type>
class type_pool< T, Allocator >

The only class you need.

Use this class to pool memory dynamically allocated for objects of type T (and only T) in a free store instead of releasing it to the heap. The pool's free store starts empty. When allocation is requested:

Use get_ptr to get a pointer that will not automatically be returned to the pool's free store. Use get_shared or get_unique to get a pointer that will automatically be returned to the pool's store. When pointers are returned, the objects they point to are destroyed using their destructor and their memory space is pushed on the free store. Note that the pool itself must be kept alive for as long as their exists shared_ptr<T>'s and unique_ptr<T>'s that have not been returned to it.

It is possible, when it is instantiated, to request the pool to preallocate a certain amount of memory. Preallocation will occur on a seperate thread. The first allocation requested will wait until preallocation is over.

Parameters:
TThe type of objects to pool.
AllocatorThe allocation policy class to use when allocating and deallocating memory.

Constructor & Destructor Documentation

template<typename T, class Allocator = typename chunk_pool<sizeof(T)>::allocator_type>
type_pool< T, Allocator >::type_pool ( size_t  preallocate = 0,
size_t  cap = chunk_pool<sizeof(T), Allocator>::max_size() 
) [inline]

Constructor.

Parameters:
preallocateThe number of Ts for which to preallocate memory.
capThe maximum number of Ts to keep in the store.

Member Function Documentation

template<typename T, class Allocator = typename chunk_pool<sizeof(T)>::allocator_type>
template<typename U >
std::enable_if<std::is_same<T, U>::value, T*>::type type_pool< T, Allocator >::get_ptr ( U &&  t) [inline]

Returns a pointer to an instance of T.

The returned pointer will not be automatically be returned to the pool and must be manually destroyed and deallocated with Allocator::destroy and Allocator::deallocate.

Parameters:
tThe object to move into the allocated memory.
template<typename T, class Allocator = typename chunk_pool<sizeof(T)>::allocator_type>
template<typename U , typename... Args>
std::enable_if<std::is_same<T, U>::value, T*>::type type_pool< T, Allocator >::get_ptr ( Args &&...  args) [inline]

Returns a pointer to an instance of T.

The returned pointer will not be automatically be returned to the pool and must be manually destroyed and deallocated with Allocator::destroy and Allocator::deallocate.

Parameters:
argsThe arguments to forward to T's constructor.
Note:
Only for g++ v. 4.4 and above.
template<typename T, class Allocator = typename chunk_pool<sizeof(T)>::allocator_type>
template<typename U >
std::enable_if<std::is_same<T, U>::value, std::shared_ptr<T> >::type type_pool< T, Allocator >::get_shared ( U &&  t) [inline]

Wraps a raw pointer returned from get_ptr in a std::shared_ptr<T>.

Parameters:
tThe object to move into the allocated memory.
template<typename T, class Allocator = typename chunk_pool<sizeof(T)>::allocator_type>
template<typename U , typename... Args>
std::enable_if<std::is_same<T, U>::value, std::shared_ptr<T> >::type type_pool< T, Allocator >::get_shared ( Args &&...  args) [inline]

Wraps a raw pointer returned from get_ptr in a std::shared_ptr<T>.

Parameters:
argsThe arguments to forward to T's constructor.
Note:
Only for g++ v. 4.4 and above.
template<typename T, class Allocator = typename chunk_pool<sizeof(T)>::allocator_type>
template<typename U >
std::enable_if<std::is_same<T, U>::value, typename type_pool<T, Allocator>::unique_ptr>::type type_pool< T, Allocator >::get_unique ( U &&  t) [inline]

Wraps a raw pointer returned from get_ptr in a type_pool<T>::unique_ptr. Use the return value as you would a std::unique_ptr<T>.

Parameters:
tThe object to move into the allocated memory.
template<typename T, class Allocator = typename chunk_pool<sizeof(T)>::allocator_type>
template<typename U , typename... Args>
std::enable_if<std::is_same<T, U>::value, typename type_pool<T, Allocator>::unique_ptr>::type type_pool< T, Allocator >::get_unique ( Args &&...  args) [inline]

Wraps a raw pointer returned from get_ptr in a type_pool<T>::unique_ptr. Use the return value as you would a std::unique_ptr<T>.

Parameters:
argsThe arguments to forward to T's constructor.
Note:
Only for g++ v. 4.4 and above.

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