Go to the documentation of this file.00001 #ifndef __NITRO_ALLOCATOR_H__
00002 #define __NITRO_ALLOCATOR_H__
00003
00004 #include <memory>
00005
00006 #include <utilities/exception.h>
00007
00008 #if !defined( WIN32_PLATFORM ) && !defined( NIX_PLATFORM ) && !defined( MINGW_PLATFORM ) && !defined( CYGWIN_PLATFORM )
00009 #define WIN32_PLATFORM
00010 #endif
00011
00012 #ifdef ALLOCATOR
00013 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) || defined( CYGWIN_PLATFORM )
00014 #define ALLOCATOR_DLL_ENTITY __declspec( dllexport )
00015 #endif
00016
00017 #if defined( NIX_PLATFORM )
00018 #define ALLOCATOR_DLL_ENTITY
00019 #endif
00020 #else
00021 #if defined( WIN32_PLATFORM )
00022 #define ALLOCATOR_DLL_ENTITY __declspec( dllimport )
00023 #pragma comment( lib , "allocator.lib" )
00024 #endif
00025
00026 #if defined( CYGWIN_PLATFORM ) || defined( MINGW_PLATFORM )
00027 #define ALLOCATOR_DLL_ENTITY __declspec( dllimport )
00028 #endif
00029
00030 #if defined( NIX_PLATFORM )
00031 #define ALLOCATOR_DLL_ENTITY
00032 #endif
00033 #endif
00034
00035 namespace nitro{
00036
00049 class ALLOCATOR_DLL_ENTITY Allocator{
00050 public:
00051
00070 template< class type >type * AllocateBlock( void );
00071
00094 template< class type >type * AllocateArray( const std::size_t & ArrayLength );
00095
00114 template< class type >void DeallocateBlock( type * Block );
00115
00134 template< class type >void DeallocateArray( type * Array );
00135 };
00136
00137 template< class type >type * Allocator::AllocateBlock( void )
00138 {
00139 try
00140 {
00141 return( new type() );
00142 }
00143 catch( nitro::exception e )
00144 {
00145 throw( nitro::exception( std::string( "Allocator::AllocateBlock( void )::" ) + e.what() , e.code() ) );
00146 }
00147 catch( ... )
00148 {
00149 throw( nitro::exception( std::string( "Allocator::AllocateBlock( void )::An error occured" ) , 1 ) );
00150 }
00151 }
00152
00153 template< class type >type * Allocator::AllocateArray( const std::size_t & ArrayLength )
00154 {
00155 try
00156 {
00157 return( new type[ ArrayLength ] );
00158 }
00159 catch( nitro::exception e )
00160 {
00161 throw( nitro::exception( std::string( "Allocator::AllocateArray( std::size_t ArrayLength )::" ) + e.what() , e.code() ) );
00162 }
00163 catch( ... )
00164 {
00165 throw( nitro::exception( std::string( "Allocator::AllocateArray( std::size_t ArrayLength )::An error occured" ) , 1 ) );
00166 }
00167 }
00168
00169 template< class type >void Allocator::DeallocateBlock( type * Block )
00170 {
00171 try
00172 {
00173 delete Block;
00174 }
00175 catch( nitro::exception e )
00176 {
00177 throw( nitro::exception( std::string( "Allocator::DeallocateBlock( type * Block )::" ) + e.what() , e.code() ) );
00178 }
00179 catch( ... )
00180 {
00181 throw( nitro::exception( std::string( "Allocator::DeallocateBlock( type * Block )::An error occured" ) , 1 ) );
00182 }
00183 }
00184
00185 template< class type >void Allocator::DeallocateArray( type * Array )
00186 {
00187 try
00188 {
00189 delete [] Array;
00190 }
00191 catch( nitro::exception e )
00192 {
00193 throw( nitro::exception( std::string( "Allocator::DeallocateArray( type * Array )::" ) + e.what() , e.code() ) );
00194 }
00195 catch( ... )
00196 {
00197 throw( nitro::exception( std::string( "Allocator::DeallocateArray( type * Array )::An error occured" ) , 1 ) );
00198 }
00199 }
00200
00215 ALLOCATOR_DLL_ENTITY Allocator * GetAllocator( void );
00216 }
00217
00218 #endif