77 lines
2.7 KiB
C++
77 lines
2.7 KiB
C++
//----------------------------------------------------------------------------
|
|
// EgalS
|
|
//----------------------------------------------------------------------------
|
|
// File : GeoObjFactory.h Data : 13.10.13 Versione : 1.2a3
|
|
// Contenuto : Factory della classe CGeoObj.
|
|
//
|
|
//
|
|
//
|
|
// Modifiche : 13.10.13 DS Creazione modulo.
|
|
//
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include "/EgtDev/Include/EGkGeoObj.h"
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
#define GEOOBJ_REGISTER( sKey, T) static const bool bReg_##T = GeoObjRegister<class T>::DoRegister( sKey)
|
|
#define GEOOBJ_GETKEY( T) GeoObjRegister<class T>::GetKey()
|
|
#define GEOOBJ_CREATE( sKey) GeoObjFactory::Create( sKey)
|
|
|
|
//----------------------------------------------------------------------------
|
|
template <class T>
|
|
class GeoObjRegister
|
|
{
|
|
public :
|
|
static bool DoRegister( std::string sKey)
|
|
{ if ( ! GeoObjFactory::Register( sKey, Create))
|
|
return false ;
|
|
GetKeyPrivate() = sKey ;
|
|
return true ; }
|
|
static IGeoObj* Create( void)
|
|
{ return new(nothrow) T ; }
|
|
static const std::string& GetKey( void)
|
|
{ return GetKeyPrivate() ; }
|
|
|
|
private :
|
|
GeoObjRegister( void) {}
|
|
~GeoObjRegister( void) {}
|
|
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( std::string sKey, GeoCreator Creator)
|
|
{ return GetCreatorMap().insert( std::pair< std::string, GeoCreator>( sKey, Creator)).second ; }
|
|
// per creare l'oggetto richiesto
|
|
static IGeoObj* Create( std::string sKey)
|
|
{ CreatorMap::iterator Iter = GetCreatorMap().find( sKey) ;
|
|
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, funzione di creazione
|
|
typedef std::unordered_map<std::string, GeoCreator> CreatorMap ;
|
|
//
|
|
static CreatorMap& GetCreatorMap( void)
|
|
{ static CreatorMap s_CreatorMap ;
|
|
return s_CreatorMap ; }
|
|
} ;
|