Atlas  0.7.0
Networking protocol for the Worldforge system.
Atlas::Objects::Allocator< T > Class Template Reference

#include <BaseObject.h>

Collaboration diagram for Atlas::Objects::Allocator< T >:

Public Member Functions

 Allocator ()
 
 ~Allocator ()
 
T * getDefaultObjectInstance ()
 
T * alloc ()
 
void free (T *instance)
 
void release ()
 

Public Attributes

std::map< std::string, uint32_t > attr_flags_Data
 

Protected Attributes

m_defaults_Data
 
std::mutex m_begin_Data_mutex
 
T * m_begin_Data
 

Detailed Description

template<typename T>
class Atlas::Objects::Allocator< T >

Trait which handles allocation of instances of BaseObject.

This class handles allocation and deallocation of templates BaseObject instances. Instead of creating and deleting BaseObjects as they are used we keep a small pool around, reusing old instances when needed.

This class is tightly coupled with both SmartPtr and BaseObject and rely on a couple of templated naming conventions. Any subclass of BaseObject should therefore keep a static instance of this in a field named "allocator".

Definition at line 78 of file BaseObject.h.

Constructor & Destructor Documentation

template<typename T >
Atlas::Objects::Allocator< T >::~Allocator ( )

Dtor.

Definition at line 159 of file BaseObject.h.

References Atlas::Objects::Allocator< T >::release().

Member Function Documentation

template<typename T >
T * Atlas::Objects::Allocator< T >::alloc ( )
inline

Allocates a new instance to be used.

This will either reuse and existing instance or create a new, depending of whether there's a free unused instance available.

Returns
An instance ready to be used.

Definition at line 170 of file BaseObject.h.

References Atlas::Objects::Allocator< T >::m_begin_Data, Atlas::Objects::Allocator< T >::m_begin_Data_mutex, and Atlas::Objects::Allocator< T >::m_defaults_Data.

template<typename T>
void Atlas::Objects::Allocator< T >::free ( T *  instance)
inline

Frees up an instance.

This means that the instance will be returned to the pool, ready to be used again.

Parameters
instanceThe instance to free.

Definition at line 188 of file BaseObject.h.

References Atlas::Objects::Allocator< T >::m_begin_Data, and Atlas::Objects::Allocator< T >::m_begin_Data_mutex.

Referenced by Atlas::Objects::BaseObjectData::getClassNo().

template<typename T >
T * Atlas::Objects::Allocator< T >::getDefaultObjectInstance ( )
inline

Gets the default object instance, which acts as a prototype for all other instances in the system.

Any alterations made to the prototype instance will reflect on all other instances of the same class.

Returns
The default object instance.

Definition at line 164 of file BaseObject.h.

References Atlas::Objects::Allocator< T >::m_defaults_Data.

template<typename T >
void Atlas::Objects::Allocator< T >::release ( )

Deletes all pooled but unused instances.

Definition at line 199 of file BaseObject.h.

References Atlas::Objects::Allocator< T >::m_begin_Data.

Referenced by Atlas::Objects::Allocator< T >::~Allocator().

Member Data Documentation

template<typename T>
std::map<std::string, uint32_t> Atlas::Objects::Allocator< T >::attr_flags_Data

A map of attributes and their flags.

Definition at line 104 of file BaseObject.h.

Referenced by Atlas::Objects::Allocator< T >::Allocator().

template<typename T>
T* Atlas::Objects::Allocator< T >::m_begin_Data
protected

The first available instance, not currently in use.

If this is null, a new instance needs to be created.

Definition at line 97 of file BaseObject.h.

Referenced by Atlas::Objects::Allocator< T >::alloc(), Atlas::Objects::Allocator< T >::free(), and Atlas::Objects::Allocator< T >::release().

template<typename T>
std::mutex Atlas::Objects::Allocator< T >::m_begin_Data_mutex
protected

Mutex for whenever the m_being_Data_mutex field need to be accessed. A std::atomic might be nicer, but the current code requires a mutex.

Definition at line 90 of file BaseObject.h.

Referenced by Atlas::Objects::Allocator< T >::alloc(), and Atlas::Objects::Allocator< T >::free().

template<typename T>
T Atlas::Objects::Allocator< T >::m_defaults_Data
protected

The default instance, acting as a prototype for all other instances.

Definition at line 83 of file BaseObject.h.

Referenced by Atlas::Objects::Allocator< T >::alloc(), Atlas::Objects::Allocator< T >::Allocator(), and Atlas::Objects::Allocator< T >::getDefaultObjectInstance().


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