source: Revenant/geoworld/src/gmregistry.cpp@ 8125274

port/mars-tycoon
Last change on this file since 8125274 was 80a6a52, checked in by Jonathan Neufeld <support@…>, 3 years ago

Get to a compile state for terrain procedural generation

  • Property mode set to 100644
File size: 3.3 KB
Line 
1#include "gmregistry.h"
2
3#include <assert.h>
4#include <mars_log.h>
5
6namespace geoworld
7{
8 GeoMorphRegistry::GeoMorphRegistry()
9 {
10 }
11
12 GeoMorphRegistry::~GeoMorphRegistry()
13 {
14 }
15
16 void GeoMorphRegistry::registerFactory( const std::string & name, IGeoFactoryBase * pFactory )
17 {
18 if (_factories.find(name) != _factories.end())
19 throw GMRegistryException("Factory has already registered");
20
21 _factories[name] = pFactory;
22 }
23
24 IGeoFactoryBase * GeoMorphRegistry::unregisterFactory( const std::string & name )
25 {
26 FactoryMap::iterator i = _factories.find(name);
27
28 if (i != _factories.end())
29 {
30 for (GeoMorphRegistry::FactoryMap::iterator j = _factconf.begin(); j != _factconf.end(); ++j)
31 if (j->second == i->second)
32 j = _factconf.erase(j);
33
34 IGeoFactoryBase * pRemoved = i->second;
35
36 _factories.erase(name);
37
38 {
39 GeoMorphFactory * pGMFact = dynamic_cast< GeoMorphFactory * > (pRemoved);
40 if (pGMFact != NULL)
41 _gmfact.erase(pGMFact);
42 }
43
44 {
45 GeoTemplateFactory * pGTFact = dynamic_cast< GeoTemplateFactory * > (pRemoved);
46 if (pGTFact != NULL)
47 _gtfact[pGTFact->getPhase()].erase(pGTFact);
48 }
49
50 return pRemoved;
51 } else
52 return NULL;
53 }
54
55 void GeoMorphRegistry::configure( IFactoryConfigReader & reader )
56 {
57 IConfigFactoryBase * pFactoryConfig;
58 const std::string sStdMsg = "Failed to configure factory ";
59
60 try {
61 while (NULL != (pFactoryConfig = reader.readNext()))
62 {
63 if (_factories.find(pFactoryConfig->factory) != _factories.end())
64 {
65 IGeoFactoryBase * pFact = _factories[pFactoryConfig->factory];
66
67 try
68 {
69 pFact->configure(pFactoryConfig);
70
71 if (_factconf.find(pFactoryConfig->nom) != _factconf.end())
72 throw GMInvalidFactoryConfigEx("Factory is already registered '" + pFactoryConfig->nom + "'");
73
74 _factconf[pFactoryConfig->nom] = pFact;
75
76 {
77 GeoMorphFactory * pGMFact = dynamic_cast< GeoMorphFactory * > (pFact);
78 if (pGMFact != NULL)
79 _gmfact.insert(pGMFact);
80 }
81
82 {
83 GeoTemplateFactory * pGTFact = dynamic_cast< GeoTemplateFactory * > (pFact);
84 if (pGTFact != NULL)
85 _gtfact[pGTFact->getPhase()].insert(pGTFact);
86 }
87 }
88 catch (GMInvalidFactoryConfigEx & e)
89 {
90 LOG(mars::Log::Warn) << sStdMsg << pFactoryConfig->factory << " {" << e.what() << "}";
91 }
92 }
93 }
94 }
95 catch (GMMalformedConfigEx & e)
96 {
97 LOG(mars::Log::Warn) << "Configuration is malformed {" << e.what() << "}";
98 }
99 }
100
101 const IGeoFactoryBase * GeoMorphRegistry::getFactory( const std::string & name ) const
102 {
103 FactoryMap::const_iterator i = _factories.find(name);
104
105 if (i != _factories.end())
106 return i->second;
107 else
108 return NULL;
109 }
110
111 void GeoMorphFactory::configure( IConfigFactoryBase * pFactoryConfig )
112 {
113 IConfigGMFactory * pGMFactoryConfig = dynamic_cast< IConfigGMFactory * > (pFactoryConfig);
114
115 assert(pGMFactoryConfig != NULL && "Expected object of class IConfigGMFactory");
116 _settings = pGMFactoryConfig->settings;
117 configure(pGMFactoryConfig, _settings);
118 }
119
120 void GeoTemplateFactory::configure( IConfigFactoryBase * pFactoryConfig )
121 {
122 IConfigGTFactory * pGTFactoryConfig = dynamic_cast< IConfigGTFactory * > (pFactoryConfig);
123
124 assert(pGTFactoryConfig != NULL && "Expected object of class IConfigGTFactory");
125 _settings = pGTFactoryConfig->settings;
126 configure(pGTFactoryConfig, _settings);
127 }
128}
Note: See TracBrowser for help on using the repository browser.