#include "gmregistry.h" #include #include namespace geoworld { GeoMorphRegistry::GeoMorphRegistry() { } GeoMorphRegistry::~GeoMorphRegistry() { } void GeoMorphRegistry::registerFactory( const std::string & name, IGeoFactoryBase * pFactory ) { if (_factories.find(name) != _factories.end()) throw GMRegistryException("Factory has already registered"); _factories[name] = pFactory; } IGeoFactoryBase * GeoMorphRegistry::unregisterFactory( const std::string & name ) { FactoryMap::iterator i = _factories.find(name); if (i != _factories.end()) { for (GeoMorphRegistry::FactoryMap::iterator j = _factconf.begin(); j != _factconf.end(); ++j) if (j->second == i->second) j = _factconf.erase(j); IGeoFactoryBase * pRemoved = i->second; _factories.erase(name); { GeoMorphFactory * pGMFact = dynamic_cast< GeoMorphFactory * > (pRemoved); if (pGMFact != NULL) _gmfact.erase(pGMFact); } { GeoTemplateFactory * pGTFact = dynamic_cast< GeoTemplateFactory * > (pRemoved); if (pGTFact != NULL) _gtfact[pGTFact->getPhase()].erase(pGTFact); } return pRemoved; } else return NULL; } void GeoMorphRegistry::configure( IFactoryConfigReader & reader ) { IConfigFactoryBase * pFactoryConfig; const std::string sStdMsg = "Failed to configure factory "; try { while (NULL != (pFactoryConfig = reader.readNext())) { if (_factories.find(pFactoryConfig->factory) != _factories.end()) { IGeoFactoryBase * pFact = _factories[pFactoryConfig->factory]; try { pFact->configure(pFactoryConfig); if (_factconf.find(pFactoryConfig->nom) != _factconf.end()) throw GMInvalidFactoryConfigEx("Factory is already registered '" + pFactoryConfig->nom + "'"); _factconf[pFactoryConfig->nom] = pFact; { GeoMorphFactory * pGMFact = dynamic_cast< GeoMorphFactory * > (pFact); if (pGMFact != NULL) _gmfact.insert(pGMFact); } { GeoTemplateFactory * pGTFact = dynamic_cast< GeoTemplateFactory * > (pFact); if (pGTFact != NULL) _gtfact[pGTFact->getPhase()].insert(pGTFact); } } catch (GMInvalidFactoryConfigEx & e) { LOG(mars::Log::Warn) << sStdMsg << pFactoryConfig->factory << " {" << e.what() << "}"; } } } } catch (GMMalformedConfigEx & e) { LOG(mars::Log::Warn) << "Configuration is malformed {" << e.what() << "}"; } } const IGeoFactoryBase * GeoMorphRegistry::getFactory( const std::string & name ) const { FactoryMap::const_iterator i = _factories.find(name); if (i != _factories.end()) return i->second; else return NULL; } void GeoMorphFactory::configure( IConfigFactoryBase * pFactoryConfig ) { IConfigGMFactory * pGMFactoryConfig = dynamic_cast< IConfigGMFactory * > (pFactoryConfig); assert(pGMFactoryConfig != NULL && "Expected object of class IConfigGMFactory"); _settings = pGMFactoryConfig->settings; configure(pGMFactoryConfig, _settings); } void GeoTemplateFactory::configure( IConfigFactoryBase * pFactoryConfig ) { IConfigGTFactory * pGTFactoryConfig = dynamic_cast< IConfigGTFactory * > (pFactoryConfig); assert(pGTFactoryConfig != NULL && "Expected object of class IConfigGTFactory"); _settings = pGTFactoryConfig->settings; configure(pGTFactoryConfig, _settings); } }