Files
EgtGeomKernel/GeoObjFactory.h
T
Dario Sassi 6dd3ea5cc2 EgtGeomKernel 2.7k1 :
- aggiustamenti e ricompilazione per passaggio a C++ 20
2025-11-01 17:24:43 +01:00

120 lines
4.9 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2014-2015
//----------------------------------------------------------------------------
// 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 "NgeKeyW.h"
#include "/EgtDev/Include/EGkGeoObj.h"
#include <string>
#include <unordered_map>
//----------------------------------------------------------------------------
#define GEOOBJ_REGISTER( nKey, nNgeId, T) static const bool bReg_##T = \
GeoObjRegister<T>::DoRegister( nKey, nNgeId)
#define GEOOBJ_GETTYPE( T) GeoObjRegister<T>::GetType()
#define GEOOBJ_GETKEY( T) GeoObjRegister<T>::GetKey()
#define GEOOBJ_GETNGEID( T) GeoObjRegister<T>::GetNgeId()
#define GEOOBJ_KEYTOTYPE( sKey) GeoObjFactory::KeyToType( sKey)
#define GEOOBJ_NGEIDTOTYPE( nNgeId) GeoObjFactory::NgeIdToType( nNgeId)
#define GEOOBJ_CREATE( nKey) GeoObjFactory::Create( nKey)
//----------------------------------------------------------------------------
class GeoObjFactory
{
public :
// definizione del tipo funzione di creazione
typedef IGeoObj* ( *GeoCreator) ( void) ;
// per registrare le funzioni di creazione
static bool Register( int nKey, const std::string& sKey, int nNgeId, GeoCreator Creator)
{ return GetKeyTypeMap().emplace( sKey, nKey).second &&
GetNgeIdTypeMap().emplace( nNgeId, nKey).second &&
GetCreatorMap().emplace( nKey, Creator).second ; }
// per restituire il tipo data la chiave
static int KeyToType( const std::string& sKey)
{ auto Iter = GetKeyTypeMap().find( sKey) ;
if ( Iter != GetKeyTypeMap().end())
return Iter->second ;
return 0 ; }
// per restituire il tipo dato l'indice Nge
static int NgeIdToType( int nNgeId)
{ auto Iter = GetNgeIdTypeMap().find( nNgeId) ;
if ( Iter != GetNgeIdTypeMap().end())
return Iter->second ;
return 0 ; }
// per creare l'oggetto richiesto
static IGeoObj* Create( int nKey)
{ auto 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<std::string, int> KeyTypeMap ;
// metodo di accesso alla mappa statica
static KeyTypeMap& GetKeyTypeMap( void)
{ static KeyTypeMap s_KeyTypeMap ;
return s_KeyTypeMap ; }
// definizione del tipo mappa con coppie NgeId, tipo
typedef std::unordered_map<int, int> NgeIdTypeMap ;
// metodo di accesso alla mappa statica
static NgeIdTypeMap& GetNgeIdTypeMap( void)
{ static NgeIdTypeMap s_NgeIdTypeMap ;
return s_NgeIdTypeMap ; }
// definizione del tipo mappa con coppie tipo, funzione di creazione
typedef std::unordered_map<int, GeoCreator> CreatorMap ;
// metodo di accesso alla mappa statica
static CreatorMap& GetCreatorMap( void)
{ static CreatorMap s_CreatorMap ;
return s_CreatorMap ; }
} ;
//----------------------------------------------------------------------------
template <class T>
class GeoObjRegister
{
public :
static bool DoRegister( int nKey, int nNgeId)
{ if ( ! GeoObjFactory::Register( nKey, NgeAscKeyW[nNgeId], nNgeId, Create))
return false ;
GetTypePrivate() = nKey ;
GetKeyPrivate() = NgeAscKeyW[nNgeId] ;
GetNgeIdPrivate() = nNgeId ;
return true ; }
static IGeoObj* Create( void)
{ return new( std::nothrow) T ; }
static int GetType( void)
{ return GetTypePrivate() ; }
static const std::string& GetKey( void)
{ return GetKeyPrivate() ; }
static int GetNgeId( void)
{ return GetNgeIdPrivate() ; }
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 ; }
static int& GetNgeIdPrivate( void)
{ static int s_nNgeId ;
return s_nNgeId ; }
} ;