//---------------------------------------------------------------------------- // EgalTech 2015-2015 //---------------------------------------------------------------------------- // File : MachiningDataFactory.h Data : 05.06.15 Versione : 1.6f1 // Contenuto : Factory della classe MachiningData. // // // // Modifiche : 06.05.15 DS Creazione modulo. // // //---------------------------------------------------------------------------- #pragma once #include "MachiningData.h" #include #include //---------------------------------------------------------------------------- #define MCHDATA_REGISTER( nType, sName, T) static const bool bReg_##T = \ MachiningDataRegister::DoRegister( nType, sName) #define MCHDATA_GETTYPE( T) MachiningDataRegister::GetType() #define MCHDATA_GETNAME( T) MachiningDataRegister::GetName() #define MCHDATA_NAMETOTYPE( sName) MachiningDataFactory::NameToType( sName) #define MCHDATA_CREATE( nType) MachiningDataFactory::Create( nType) #define MCHDATA_GETLIST( vsList) MachiningDataFactory::GetList( vsList) //---------------------------------------------------------------------------- template class MachiningDataRegister { public : static bool DoRegister( int nType, const std::string& sName) { if ( ! MachiningDataFactory::Register( nType, sName, Create)) return false ; GetTypePrivate() = nType ; GetNamePrivate() = sName ; return true ; } static MachiningData* Create( void) { return new(nothrow) T ; } static int GetType( void) { return GetTypePrivate() ; } static const std::string& GetName( void) { return GetNamePrivate() ; } private : MachiningDataRegister( void) {} ~MachiningDataRegister( void) {} static int& GetTypePrivate( void) { static int s_nType ; return s_nType ; } static std::string& GetNamePrivate( void) { static std::string s_sName ; return s_sName ; } } ; //---------------------------------------------------------------------------- class MachiningDataFactory { public : // definizione del tipo funzione di creazione typedef MachiningData* ( *MachiningDataCreator) ( void) ; // per registrare le funzioni di creazione static bool Register( int nType, const std::string& sName, MachiningDataCreator Creator) { return GetCreatorMap().emplace( nType, Creator).second && GetNameTypeMap().emplace( sName, nType).second ; } // per restituire il tipo dato il nome static int NameToType( const std::string& sName) { auto Iter = GetNameTypeMap().find( sName) ; if ( Iter != GetNameTypeMap().end()) return Iter->second ; return 0 ; } // per creare l'oggetto richiesto static MachiningData* Create( int nType) { auto Iter = GetCreatorMap().find( nType) ; if ( Iter != GetCreatorMap().end()) { if ( Iter->second != nullptr) return Iter->second() ; } return nullptr ; } // per restituire la lista degli oggetti registrati static bool GetList( STRVECTOR& vsList) {// verifico parametro di ritorno if ( &vsList == nullptr) return false ; // ciclo sugli oggetti registrati for ( auto Iter = GetNameTypeMap().cbegin() ; Iter != GetNameTypeMap().cend() ; ++ Iter) vsList.emplace_back( Iter->first) ; return true ; } private : MachiningDataFactory( void) {} ~MachiningDataFactory( void) {} // definizione del tipo mappa con coppie nome, tipo typedef std::unordered_map NameTypeMap ; // metodo di accesso alla mappa statica static NameTypeMap& GetNameTypeMap( void) { static NameTypeMap s_NameTypeMap ; return s_NameTypeMap ; } // 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 ; } } ;