00001 #ifndef __NITRO_POINTER_H__
00002 #define __NITRO_POINTER_H__
00003
00004 #include <utilities/exception.h>
00005 #include <utilities/memory_handler.h>
00006 #include <utilities/pointer_interface.h>
00007
00008 namespace nitro
00009 {
00020 template< class stored_type , MemoryHandler * memory_handler >class Pointer : public PointerInterface{
00021 public:
00022
00033 Pointer( void );
00034
00049 Pointer( std::size_t theItemCount );
00050
00065 Pointer( const Pointer< stored_type , memory_handler > & Ptr );
00066
00085 Pointer< stored_type , memory_handler >
00086 operator=( const Pointer< stored_type , memory_handler > & Ptr );
00087
00106 stored_type & operator[]( std::size_t i );
00107
00126 stored_type * GetBuffer( void );
00127
00146 std::size_t GetBufferSize( void ) const;
00147
00166 virtual void Allocate( std::size_t theItemCount );
00167
00182 virtual void Release( void );
00183
00194 virtual ~Pointer();
00195
00196 private:
00197
00209 stored_type * StoredData;
00210
00211 };
00212
00213 template< class stored_type , MemoryHandler * memory_handler >Pointer< stored_type , memory_handler >::Pointer()
00214 {
00215 }
00216
00217 template< class stored_type , MemoryHandler * memory_handler >Pointer< stored_type , memory_handler >::Pointer( std::size_t theItemCount )
00218 {
00219 Allocate( theItemCount );
00220 }
00221
00222 template< class stored_type , MemoryHandler * memory_handler >Pointer< stored_type , memory_handler >::Pointer( const Pointer< stored_type , memory_handler > & Ptr )
00223 {
00224 try
00225 {
00226
00227 memory_handler->Copy( StoredData , const_cast< char * >( Ptr.StoredData ) );
00228
00229 ItemCount = Ptr.ItemCount;
00230 }
00231 catch( nitro::exception e )
00232 {
00233 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >::Pointer( const Pointer< stored_type , memory_handler > & Ptr )::" ) + e.what() , 0 ) );
00234 }
00235 catch( ... )
00236 {
00237 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >::Pointer( const Pointer< stored_type , memory_handler > & Ptr )::An error occured while getting data from pointer" ) , 0 ) );
00238 }
00239 }
00240
00241 template< class stored_type , MemoryHandler * memory_handler >Pointer< stored_type , memory_handler > Pointer< stored_type , memory_handler >::operator=( const Pointer< stored_type , memory_handler > & Ptr )
00242 {
00243 try
00244 {
00245 memory_handler->Copy( StoredData , const_cast< char * >( Ptr.StoredData ) );
00246
00247 ItemCount = Ptr.ItemCount;
00248
00249 return( * this );
00250 }
00251 catch( nitro::exception e )
00252 {
00253 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >::operator=( const Pointer< stored_type , memory_handler > & Ptr )::" ) + e.what() , 0 ) );
00254 }
00255 catch( ... )
00256 {
00257 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >::operator=( const Pointer< stored_type , memory_handler > & Ptr )::An error occured while getting data from pointer" ) , 0 ) );
00258 }
00259 }
00260
00261 template< class stored_type , MemoryHandler * memory_handler >stored_type & Pointer< stored_type , memory_handler >::operator[]( std::size_t i )
00262 {
00263 try
00264 {
00265 return( StoredData[ i ] );
00266 }
00267 catch( nitro::exception e )
00268 {
00269 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >::operator[]( std::size_t i )::" ) + e.what() , 0 ) );
00270 }
00271 catch( ... )
00272 {
00273 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >::operator[]( std::size_t i )::An error occured while getting data from pointer" ) , 0 ) );
00274 }
00275 }
00276
00277 template< class stored_type , MemoryHandler * memory_handler >stored_type * Pointer< stored_type , memory_handler >::GetBuffer( void )
00278 {
00279 try
00280 {
00281 return( StoredData );
00282 }
00283 catch( nitro::exception e )
00284 {
00285 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >::GetBuffer( void )::" ) + e.what() , 0 ) );
00286 }
00287 catch( ... )
00288 {
00289 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >::GetBuffer( void )::An error occured while getting data from pointer" ) , 0 ) );
00290 }
00291 }
00292
00293 template< class stored_type , MemoryHandler * memory_handler >std::size_t Pointer< stored_type , memory_handler >::GetBufferSize( void ) const
00294 {
00295 try
00296 {
00297 return( sizeof( stored_type ) * ItemCount );
00298 }
00299 catch( nitro::exception e )
00300 {
00301 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >GetBufferSize( void ) const::" ) + e.what() , 0 ) );
00302 }
00303 catch( ... )
00304 {
00305 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >GetBufferSize( void ) const::An error occured while getting data from pointer" ) , 0 ) );
00306 }
00307 }
00308
00309 template< class stored_type , MemoryHandler * memory_handler > void Pointer< stored_type , memory_handler >::Allocate( std::size_t theItemCount )
00310 {
00311 try
00312 {
00313 Release();
00314
00315 StoredData = memory_handler->Allocate< stored_type >( this , theItemCount );
00316
00317 ItemCount = theItemCount;
00318 }
00319 catch( nitro::exception e )
00320 {
00321 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >::Allocate( std::size_t theItemCount )::" ) + e.what() , 0 ) );
00322 }
00323 catch( ... )
00324 {
00325 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >::Allocate( std::size_t theItemCount )::An error occured while getting data from pointer" ) , 0 ) );
00326 }
00327 }
00328
00329 template< class stored_type , MemoryHandler * memory_handler > void Pointer< stored_type , memory_handler >::Release( void )
00330 {
00331 try
00332 {
00333 if( StoredData )
00334 {
00335 memory_handler->Deallocate( StoredData );
00336
00337 StoredData = NULL;
00338
00339 ItemCount = NULL;
00340 }
00341 }
00342 catch( nitro::exception e )
00343 {
00344 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >::Release( void )::" ) + e.what() , 0 ) );
00345 }
00346 catch( ... )
00347 {
00348 throw( nitro::exception( std::string( "Pointer< stored_type , memory_handler >::Release( void )::An error occured while getting data from pointer" ) , 0 ) );
00349 }
00350 }
00351
00352 template< class stored_type , MemoryHandler * memory_handler > Pointer< stored_type , memory_handler >::~Pointer()
00353 {
00354 try
00355 {
00356 Release();
00357 }
00358 catch( nitro::exception e )
00359 {
00360 }
00361 catch( ... )
00362 {
00363 }
00364 }
00365 }
00366
00367 #endif