//---------------------------------------------------------------------------- // EgalS //---------------------------------------------------------------------------- // File : GeoObjFactory.h Data : 26.11.13 Versione : 1.3a1 // Contenuto : Factory della classe CGeoObj. // // // // Modifiche : 13.10.13 DS Creazione modulo. // 26.11.13 DS Aggiunta KeyToType e creazione da Type anzichè Key. // //---------------------------------------------------------------------------- #pragma once #include #include #include "/EgtDev/Include/EGkGeoObj.h" //---------------------------------------------------------------------------- #define GEOOBJ_REGISTER(nKey,sKey,T) static const bool bReg_##T = GeoObjRegister::DoRegister(nKey,sKey) #define GEOOBJ_GETTYPE( T) GeoObjRegister::GetType() #define GEOOBJ_GETKEY( T) GeoObjRegister::GetKey() #define GEOOBJ_KEYTOTYPE( sKey) GeoObjFactory::KeyToType( sKey) #define GEOOBJ_CREATE( nKey) GeoObjFactory::Create( nKey) //---------------------------------------------------------------------------- template class GeoObjRegister { public : static bool DoRegister( int nKey, std::string sKey) { if ( ! GeoObjFactory::Register( nKey, sKey, Create)) return false ; GetTypePrivate() = nKey ; GetKeyPrivate() = sKey ; return true ; } static IGeoObj* Create( void) { return new(nothrow) T ; } static const int& GetType( void) { return GetTypePrivate() ; } static const std::string& GetKey( void) { return GetKeyPrivate() ; } private : GeoObjRegister( void) {} ~GeoObjRegister( void) {} static int& GetTypePrivate( void) { static int s_nType ; return s_nType ; } static std::string& GetKeyPrivate( void) { static std::string s_sKey ; return s_sKey ; } } ; //---------------------------------------------------------------------------- class GeoObjFactory { public : // definizione del tipo funzione di creazione typedef IGeoObj* ( *GeoCreator) ( void) ; // per registrare le funzioni di creazione static bool Register( int nKey, std::string sKey, GeoCreator Creator) { return GetKeyTypeMap().insert( std::pair< std::string, int>( sKey, nKey)).second && GetCreatorMap().insert( std::pair< int, GeoCreator>( nKey, Creator)).second ; } // per restituire il tipo data la chiave static int KeyToType( std::string sKey) { KeyTypeMap::iterator Iter = GetKeyTypeMap().find( sKey) ; if ( Iter != GetKeyTypeMap().end()) return Iter->second ; return 0 ; } // per creare l'oggetto richiesto static IGeoObj* Create( int nKey) { CreatorMap::iterator Iter = GetCreatorMap().find( nKey) ; if ( Iter != GetCreatorMap().end()) { if ( Iter->second != nullptr) return Iter->second() ; } return nullptr ; } private : GeoObjFactory( void) {} ~GeoObjFactory( void) {} // definizione del tipo mappa con coppie nome, tipo typedef std::unordered_map KeyTypeMap ; // metodo di accesso alla mappa statica static KeyTypeMap& GetKeyTypeMap( void) { static KeyTypeMap s_KeyTypeMap ; return s_KeyTypeMap ; } // definizione del tipo mappa con coppie tipo, funzione di creazione typedef std::unordered_map CreatorMap ; // metodo di accesso alla mappa statica static CreatorMap& GetCreatorMap( void) { static CreatorMap s_CreatorMap ; return s_CreatorMap ; } } ;