#include "object_factory.hpp"
#include "math/vector.hpp"
-Factories* object_factories = 0;
-
GameObject* create_object(const std::string& name, const lisp::Lisp& reader)
{
- Factories::iterator i = object_factories->find(name);
- if(i == object_factories->end()) {
+ Factory::Factories::iterator i = Factory::get_factories().find(name);
+ if(i == Factory::get_factories().end()) {
std::stringstream msg;
msg << "No factory for object '" << name << "' found.";
throw std::runtime_error(msg.str());
* Remember to delete the objects later
*/
virtual GameObject* create_object(const lisp::Lisp& reader) = 0;
-};
-typedef std::map<std::string, Factory*> Factories;
-extern Factories* object_factories;
+ typedef std::map<std::string, Factory*> Factories;
+ static Factories &get_factories()
+ {
+ static Factories object_factories;
+ return object_factories;
+ }
+};
GameObject* create_object(const std::string& name, const lisp::Lisp& reader);
GameObject* create_object(const std::string& name, const Vector& pos);
public: \
INTERN_##CLASS##Factory() \
{ \
- if(object_factories == 0) \
- object_factories = new Factories; \
+ get_factories()[NAME] = this; \
+ } \
\
- object_factories->insert(std::make_pair(NAME, this)); \
+ ~INTERN_##CLASS##Factory() \
+ { \
+ get_factories().erase(NAME); \
} \
\
virtual GameObject* create_object(const lisp::Lisp& reader) \