00001 #ifndef __OBJECT_MANAGER_H__
00002 #define __OBJECT_MANAGER_H__
00003
00004 #include <string>
00005 #include <utility>
00006 #include <vector>
00007
00008 #include <utilities/exception.h>
00009
00010 namespace nitro
00011 {
00012
00025 template< class managing_class >class ObjectManagerBasic{
00026
00027 public:
00028
00051 managing_class * operator[]( const int ObjectCursor ) const;
00052
00075 managing_class * operator[]( const std::size_t ObjectCursor ) const;
00076
00099 managing_class * operator[]( const char * ObjectName ) const;
00100
00123 managing_class * operator[]( const std::string & ObjectName ) const;
00124
00143 managing_class * Back( void );
00144
00159 virtual void Release( void );
00160
00183 std::size_t GetObjectCursor( const std::string & ObjectName ) const;
00184
00207 std::size_t GetObjectCursor( const char * ObjectName ) const;
00208
00231 bool ObjectExists( const std::string & ObjectName ) const;
00232
00255 bool ObjectExists( const char * ObjectName ) const;
00256
00279 void DeleteObject( const std::string & ObjectName );
00280
00303 void DeleteObject( const char * ObjectName );
00304
00323 std::size_t Count( void ) const;
00324
00335 virtual ~ObjectManagerBasic();
00336
00337 protected:
00338
00349 std::vector< std::pair< std::string , managing_class * > > Objects;
00350 };
00351
00352 template< class managing_class >managing_class * ObjectManagerBasic< managing_class >::operator[]( const int ObjectCursor ) const
00353 {
00354 try
00355 {
00356 if( ObjectCursor <= 0 )
00357 {
00358 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::operator[]( const int ObjectCursor )::Negative cursors are illegal" ) , 1 ) );
00359 }
00360
00361 return( ( * this )[ ( std::size_t )ObjectCursor ] );
00362 }
00363 catch( nitro::exception e )
00364 {
00365 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::operator[]( const int ObjectCursor )::" ) + e.what() , e.code() ) );
00366 }
00367 catch( ... )
00368 {
00369 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::operator[]( const int ObjectCursor )::An error occured" ) , 1 ) );
00370 }
00371 }
00372
00373 template< class managing_class >managing_class * ObjectManagerBasic< managing_class >::operator[]( const std::size_t ObjectCursor ) const
00374 {
00375 try
00376 {
00377 if( ( std::size_t )ObjectCursor >= Objects.size() )
00378 {
00379 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::operator[]( const std::size_t ObjectCursor )::An error occured while getting object by cursor" ) , 1 ) );
00380 }
00381
00382 return( Objects[ ObjectCursor ].second );
00383 }
00384 catch( nitro::exception e )
00385 {
00386 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::operator[]( const std::size_t ObjectCursor )::" ) + e.what() , e.code() ) );
00387 }
00388 catch( ... )
00389 {
00390 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::operator[]( const std::size_t ObjectCursor )::An error occured" ) , 1 ) );
00391 }
00392 }
00393
00394 template< class managing_class >managing_class * ObjectManagerBasic< managing_class >::operator[]( const char * ObjectName ) const
00395 {
00396 try
00397 {
00398 for( typename std::vector< std::pair< std::string , managing_class * > >::const_iterator i( Objects.begin() ) ; i != Objects.end() ; i++ )
00399 {
00400 if( i->first == ObjectName )
00401 {
00402 return( i->second );
00403 }
00404 }
00405
00406 throw( nitro::exception( std::string( "An error occured while getting object \"" ) + ObjectName + "\"" , 1 ) );
00407 }
00408 catch( nitro::exception e )
00409 {
00410 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::operator[]( const char * ObjectName )::" ) + e.what() , e.code() ) );
00411 }
00412 catch( ... )
00413 {
00414 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::operator[]( const char * ObjectName )::An error occured" ) , 1 ) );
00415 }
00416
00417
00418 return( ( managing_class * )NULL );
00419 }
00420
00421 template< class managing_class >managing_class * ObjectManagerBasic< managing_class >::operator[]( const std::string & ObjectName ) const
00422 {
00423 try
00424 {
00425 return( ( * this )[ ObjectName.c_str() ] );
00426 }
00427 catch( nitro::exception e )
00428 {
00429 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::operator[]( const std::string & ObjectName )::" ) + e.what() , e.code() ) );
00430 }
00431 catch( ... )
00432 {
00433 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::operator[]( const std::string & ObjectName )::An error occured" ) , 1 ) );
00434 }
00435 }
00436
00437 template< class managing_class >managing_class * ObjectManagerBasic< managing_class >::Back( void )
00438 {
00439 try
00440 {
00441 if( Objects.size() == 0 )
00442 {
00443 throw( nitro::exception( std::string( "There are no objects in the manager" ) , 1 ) );
00444 }
00445
00446 return( Objects.back() );
00447 }
00448 catch( nitro::exception e )
00449 {
00450 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::operator[]( const str_type ObjectName )::" ) + e.what() , e.code() ) );
00451 }
00452 catch( ... )
00453 {
00454 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::operator[]( const str_type ObjectName )::An error occured" ) , 1 ) );
00455 }
00456
00457
00458 return( ( managing_class * )NULL );
00459 }
00460
00461 template< class managing_class >void ObjectManagerBasic< managing_class >::Release( void )
00462 {
00463 try
00464 {
00465 Objects.erase( Objects.begin(), Objects.end() );
00466 }
00467 catch( nitro::exception e )
00468 {
00469 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::Release( void )::" ) + e.what() , e.code() ) );
00470 }
00471 catch( ... )
00472 {
00473 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::Release( void )::An error occured" ) , 1 ) );
00474 }
00475 }
00476
00477 template< class managing_class >std::size_t ObjectManagerBasic< managing_class >::GetObjectCursor( const std::string & ObjectName ) const
00478 {
00479 try
00480 {
00481 for( std::size_t i( 0 ) ; i < Objects.size() ; i++ )
00482 {
00483 if( Objects[ i ].first == ObjectName )
00484 {
00485 return( i );
00486 }
00487 }
00488
00489 throw( nitro::exception( std::string( "Object \"" ) + ObjectName + "\" was not found" , 1 ) );
00490 }
00491 catch( nitro::exception e )
00492 {
00493 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::GetObjectCursor( const std::string & ObjectName )::" ) + e.what() , e.code() ) );
00494 }
00495 catch( ... )
00496 {
00497 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::GetObjectCursor( const std::string & ObjectName )::An error occured" ) , 1 ) );
00498 }
00499 }
00500
00501 template< class managing_class >std::size_t ObjectManagerBasic< managing_class >::GetObjectCursor( const char * ObjectName ) const
00502 {
00503 try
00504 {
00505 for( std::size_t i( 0 ) ; i < Objects.size() ; i++ )
00506 {
00507 if( Objects[ i ].first == ObjectName )
00508 {
00509 return( i );
00510 }
00511 }
00512
00513 throw( nitro::exception( std::string( "Object \"" ) + ObjectName + "\" was not found" , 1 ) );
00514 }
00515 catch( nitro::exception e )
00516 {
00517 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::GetObjectCursor( const char * ObjectName )::" ) + e.what() , e.code() ) );
00518 }
00519 catch( ... )
00520 {
00521 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::GetObjectCursor( const char * ObjectName )::An error occured" ) , 1 ) );
00522 }
00523 }
00524
00525 template< class managing_class >bool ObjectManagerBasic< managing_class >::ObjectExists( const std::string & ObjectName ) const
00526 {
00527 try
00528 {
00529 return( ObjectExists( ObjectName.c_str() ) );
00530 }
00531 catch( nitro::exception e )
00532 {
00533 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::ObjectExists( const std::string & ObjectName )::" ) + e.what() , e.code() ) );
00534 }
00535 catch( ... )
00536 {
00537 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::ObjectExists( const std::string & ObjectName )::An error occured" ) , 1 ) );
00538 }
00539 }
00540
00541 template< class managing_class >bool ObjectManagerBasic< managing_class >::ObjectExists( const char * ObjectName ) const
00542 {
00543 try
00544 {
00545 for( std::size_t i( 0 ) ; i < Objects.size() ; i++ )
00546 {
00547 if( Objects[ i ].first == ObjectName )
00548 {
00549 return( true );
00550 }
00551 }
00552
00553 return( false );
00554 }
00555 catch( nitro::exception e )
00556 {
00557 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::GetObjectCursor( const char * ObjectName )::" ) + e.what() , e.code() ) );
00558 }
00559 catch( ... )
00560 {
00561 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::GetObjectCursor( const char * ObjectName )::An error occured" ) , 1 ) );
00562 }
00563 }
00564
00565 template< class managing_class >void ObjectManagerBasic< managing_class >::DeleteObject( const std::string & ObjectName )
00566 {
00567 try
00568 {
00569 DeleteObject( ObjectName.c_str() );
00570 }
00571 catch( nitro::exception e )
00572 {
00573 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::DeleteObject( const str_type & ObjectName )::" ) + e.what() , e.code() ) );
00574 }
00575 catch( ... )
00576 {
00577 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::DeleteObject( const str_type & ObjectName )::An error occured" ) , 1 ) );
00578 }
00579 }
00580
00581 template< class managing_class >void ObjectManagerBasic< managing_class >::DeleteObject( const char * ObjectName )
00582 {
00583 try
00584 {
00585 if( ObjectExists( ObjectName ) == false )
00586 {
00587 throw( std::string( "Object \"" ) + ObjectName + "\" does not exist" );
00588 }
00589 else
00590 {
00591
00592 Objects.erase( Objects.begin() + GetObjectCursor( ObjectName ) );
00593
00594
00595 }
00596 }
00597 catch( nitro::exception e )
00598 {
00599 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::DeleteObject( const char * ObjectName )::" ) + e.what() , e.code() ) );
00600 }
00601 catch( ... )
00602 {
00603 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::DeleteObject( const char * ObjectName )::An error occured" ) , 1 ) );
00604 }
00605 }
00606
00607 template< class managing_class >std::size_t ObjectManagerBasic< managing_class >::Count( void ) const
00608 {
00609 try
00610 {
00611 return( Objects.size() );
00612 }
00613 catch( nitro::exception e )
00614 {
00615 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::Count( void )::" ) + e.what() , e.code() ) );
00616 }
00617 catch( ... )
00618 {
00619 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::Count( void )::An error occured" ) , 1 ) );
00620 }
00621 }
00622
00623 template< class managing_class > ObjectManagerBasic< managing_class >::~ObjectManagerBasic()
00624 {
00625 try
00626 {
00627 Release();
00628 }
00629 catch( ... )
00630 {
00631 }
00632 }
00633
00650 template< class managing_class >class ObjectManager : public ObjectManagerBasic< managing_class >{
00651
00652 public:
00653
00676 void AddObject( const std::string & ObjectName , managing_class * Ptr );
00677
00700 void AddObject( const char * ObjectName , managing_class * Ptr );
00701
00712 virtual ~ObjectManager();
00713 };
00714
00715 template< class managing_class >void ObjectManager< managing_class >::AddObject( const std::string & ObjectName , managing_class * Ptr )
00716 {
00717 try
00718 {
00719 ObjectManagerBasic< managing_class >::Objects.push_back(
00720 std::pair< std::string , managing_class * >( ObjectName , Ptr )
00721 );
00722 }
00723 catch( nitro::exception e )
00724 {
00725 throw( nitro::exception( std::string( "ObjectManager< managing_class >::AddObject( const std::string & ObjectName , managing_class * Ptr )::" ) + e.what() , e.code() ) );
00726 }
00727 catch( ... )
00728 {
00729 throw( nitro::exception( std::string( "ObjectManager< managing_class >::AddObject( const std::string & ObjectName )::An error occured" ) , 1 ) );
00730 }
00731 }
00732
00733 template< class managing_class >void ObjectManager< managing_class >::AddObject( const char * ObjectName , managing_class * Ptr )
00734 {
00735 try
00736 {
00737 ObjectManagerBasic< managing_class >::Objects.push_back(
00738 std::pair< std::string , managing_class * >( ObjectName , Ptr )
00739 );
00740 }
00741 catch( nitro::exception e )
00742 {
00743 throw( nitro::exception( std::string( "ObjectManager< managing_class >::AddObject( const char * ObjectName , managing_class * Ptr )::" ) + e.what() , e.code() ) );
00744 }
00745 catch( ... )
00746 {
00747 throw( nitro::exception( std::string( "ObjectManager< managing_class >::AddObject( const char * ObjectName )::An error occured" ) , 1 ) );
00748 }
00749 }
00750
00751 template< class managing_class > ObjectManager< managing_class >::~ObjectManager()
00752 {
00753 try
00754 {
00755 ObjectManagerBasic< managing_class >::Release();
00756 }
00757 catch( ... )
00758 {
00759 }
00760 }
00761
00778 template< class managing_class >class LocalObjectManager : public ObjectManagerBasic< managing_class >{
00779
00780 public:
00781
00800 void CreateObject( const std::string & ObjectName );
00801
00820 void CreateObject( const char * ObjectName );
00821
00836 virtual void Release( void );
00837
00848 virtual ~LocalObjectManager();
00849 };
00850
00851 template< class managing_class >void LocalObjectManager< managing_class >::CreateObject( const std::string & ObjectName )
00852 {
00853 try
00854 {
00855 ObjectManagerBasic< managing_class >::Objects.push_back(
00856 std::pair< std::string , managing_class * >( ObjectName , new managing_class() )
00857 );
00858 }
00859 catch( nitro::exception e )
00860 {
00861 throw( nitro::exception( std::string( "LocalObjectManager< managing_class >::CreateObject( const std::string & ObjectName )::" ) + e.what() , e.code() ) );
00862 }
00863 catch( ... )
00864 {
00865 throw( nitro::exception( std::string( "LocalObjectManager< managing_class >::CreateObject( const std::string & ObjectName )::An error occured" ) , 1 ) );
00866 }
00867 }
00868
00869 template< class managing_class >void LocalObjectManager< managing_class >::CreateObject( const char * ObjectName )
00870 {
00871 try
00872 {
00873 ObjectManagerBasic< managing_class >::Objects.push_back(
00874 std::pair< std::string , managing_class * >( ObjectName , new managing_class() )
00875 );
00876 }
00877 catch( nitro::exception e )
00878 {
00879 throw( nitro::exception( std::string( "LocalObjectManager< managing_class >::CreateObject( const char * ObjectName )::" ) + e.what() , e.code() ) );
00880 }
00881 catch( ... )
00882 {
00883 throw( nitro::exception( std::string( "LocalObjectManager< managing_class >::CreateObject( const char * ObjectName )::An error occured" ) , 1 ) );
00884 }
00885 }
00886
00887 template< class managing_class >void LocalObjectManager< managing_class >::Release( void )
00888 {
00889 try
00890 {
00891 for( std::size_t i( 0 ) ; i < ObjectManagerBasic< managing_class >::Objects.size() ; i++ )
00892 {
00893 delete ObjectManagerBasic< managing_class >::Objects[ i ];
00894 }
00895
00896 ObjectManagerBasic< managing_class >::Objects.erase( ObjectManagerBasic< managing_class >::Objects.begin(), ObjectManagerBasic< managing_class >::Objects.end() );
00897 }
00898 catch( nitro::exception e )
00899 {
00900 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::Release( void )::" ) + e.what() , e.code() ) );
00901 }
00902 catch( ... )
00903 {
00904 throw( nitro::exception( std::string( "ObjectManagerBasic< managing_class >::Release( void )::An error occured" ) , 1 ) );
00905 }
00906 }
00907
00908 template< class managing_class > LocalObjectManager< managing_class >::~LocalObjectManager()
00909 {
00910 try
00911 {
00912 ObjectManagerBasic< managing_class >::Release();
00913 }
00914 catch( ... )
00915 {
00916 }
00917 }
00918
00919 #define DECLARE_SQUARE_BRACE_OPERATOR_REF_LOCATOR( TYPE_NAME )\
00920 TYPE_NAME * operator[]( const char * ObjectName );
00921
00922 #define DEFINE_SQUARE_BRACE_OPERATOR_REF_LOCATOR( TYPE_NAME , CLASS_NAME , FIELD_NAME )\
00923 TYPE_NAME * CLASS_NAME::operator[]( const char * ObjectName )\
00924 {\
00925 try\
00926 {\
00927 return( &( * FIELD_NAME[ ObjectName ] ) );\
00928 }\
00929 catch( nitro::exception e )\
00930 {\
00931 throw( nitro::exception( std::string( "INHERITER::operator[]( const char * ObjectName )::" ) + e.what() , e.code() ) );\
00932 }\
00933 catch( ... )\
00934 {\
00935 throw( nitro::exception( std::string( "INHERITER::operator[]( const char * ObjectName )::An error occured" ) , 1 ) );\
00936 }\
00937 }
00938
00939 #define DECLARE_SQUARE_BRACE_OPERATOR_REF_CURSOR( TYPE_NAME )\
00940 template< class numeric >TYPE_NAME * operator[]( const numeric ObjectCursor );
00941
00942 #define DEFINE_SQUARE_BRACE_OPERATOR_REF_CURSOR( TYPE_NAME , CLASS_NAME , FIELD_NAME )\
00943 template< class numeric >TYPE_NAME * CLASS_NAME::operator[]( const numeric ObjectCursor )\
00944 {\
00945 try\
00946 {\
00947 return( &( * FIELD_NAME[ ObjectCursor ] ) );\
00948 }\
00949 catch( nitro::exception e )\
00950 {\
00951 throw( nitro::exception( std::string( "INHERITER::operator[]( numeric ObjectCursor )::" ) + e.what() , e.code() ) );\
00952 }\
00953 catch( ... )\
00954 {\
00955 throw( nitro::exception( std::string( "INHERITER::operator[]( numeric ObjectCursor )::An error occured" ) , 1 ) );\
00956 }\
00957 }
00958
00959 #define DECLARE_RELEASE_FUNCTION( FUNCTION_NAME , FIELD_NAME )\
00960 void FUNCTION_NAME( void );
00961
00962 #define DEFINE_RELEASE_FUNCTION( CLASS_NAME , FUNCTION_NAME , FIELD_NAME )\
00963 void CLASS_NAME::FUNCTION_NAME( void )\
00964 {\
00965 try\
00966 {\
00967 for( std::size_t i( 0 ) ; i < FIELD_NAME.Count() ; i++ )\
00968 {\
00969 delete FIELD_NAME[ ( int )i ];\
00970 }\
00971 FIELD_NAME.Release();\
00972 }\
00973 catch( nitro::exception e )\
00974 {\
00975 throw( nitro::exception( std::string( "INHERITER::Release( void )::" ) + e.what() , e.code() ) );\
00976 }\
00977 catch( ... )\
00978 {\
00979 throw( nitro::exception( std::string( "INHERITER::Release( void )::An error occured" ) , 1 ) );\
00980 }\
00981 }
00982 }
00983
00984 #endif