diff options
author | randomdan <randomdan@localhost> | 2011-02-11 21:29:46 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2011-02-11 21:29:46 +0000 |
commit | c0cb3dd035d5873c433197fc50ccf34718d5be6f (patch) | |
tree | 33a00d89f41e662e64c9a9982d90ab7ed46e341a | |
parent | Don't rely on crazy dangerous global init/destroy for loader registration (diff) | |
download | project2-c0cb3dd035d5873c433197fc50ccf34718d5be6f.tar.bz2 project2-c0cb3dd035d5873c433197fc50ccf34718d5be6f.tar.xz project2-c0cb3dd035d5873c433197fc50ccf34718d5be6f.zip |
Use a static local var for the loader map to work around the link/load order issue
-rw-r--r-- | project2/xmlObjectLoader.cpp | 33 | ||||
-rw-r--r-- | project2/xmlObjectLoader.h | 8 |
2 files changed, 14 insertions, 27 deletions
diff --git a/project2/xmlObjectLoader.cpp b/project2/xmlObjectLoader.cpp index a40c66c..f14f773 100644 --- a/project2/xmlObjectLoader.cpp +++ b/project2/xmlObjectLoader.cpp @@ -6,17 +6,12 @@ unsigned int LoaderBase::depth = 0; std::set<SourceObjectPtr> LoaderBase::loadedObjects; -typedef std::map<std::string, ElementLoader *> ElementLoaderMap; -static ElementLoaderMap * objLoaders; -void init_loaders() __attribute__ ((constructor(101))); -void kill_loaders() __attribute__ ((destructor(101))); -void init_loaders() +typedef std::map<std::string, boost::shared_ptr<ElementLoader> > ElementLoaderMap; +ElementLoaderMap & +objLoaders() { - objLoaders = new ElementLoaderMap(); -} -void kill_loaders() -{ - delete objLoaders; + static ElementLoaderMap _objLoaders; + return _objLoaders; } LoaderBase::LoaderBase(const Glib::ustring & n, bool r) : @@ -40,8 +35,8 @@ LoaderBase::collectAll(const xmlpp::Element * node, bool childrenOnly, Unsupport if (!childrenOnly && node->get_namespace_uri() == ns) { Glib::ustring name = node->get_name(); unsigned int stored = 0; - ElementLoaderMap::const_iterator i = objLoaders->find(name); - if (i != objLoaders->end()) { + ElementLoaderMap::const_iterator i = objLoaders().find(name); + if (i != objLoaders().end()) { SourceObjectPtr o = i->second->go(node); created += 1; loadedObjects.insert(o); @@ -87,23 +82,19 @@ LoaderBase::collectAll(const CommonObjects * co, const xmlpp::Element * node, bo void LoaderBase::newLoader(const std::string & n, ElementLoader * l) { - objLoaders->insert(ElementLoaderMap::value_type(n, l)); + objLoaders().insert(ElementLoaderMap::value_type(n, boost::shared_ptr<ElementLoader>(l))); } void LoaderBase::removeLoader(const std::string & n) { - ElementLoaderMap::const_iterator i = objLoaders->find(n); - if (i != objLoaders->end()) { - delete i->second; - } - objLoaders->erase(n); + objLoaders().erase(n); } void LoaderBase::onIdle() { - BOOST_FOREACH(ElementLoaderMap::value_type l, *objLoaders) { + BOOST_FOREACH(ElementLoaderMap::value_type l, objLoaders()) { l.second->onIdle(); } } @@ -115,7 +106,7 @@ LoaderBase::onIteration() depth = 0; loadedObjects.clear(); - BOOST_FOREACH(ElementLoaderMap::value_type l, *objLoaders) { + BOOST_FOREACH(ElementLoaderMap::value_type l, objLoaders()) { l.second->onIteration(); } } @@ -123,7 +114,7 @@ LoaderBase::onIteration() void LoaderBase::onPeriodic() { - BOOST_FOREACH(ElementLoaderMap::value_type l, *objLoaders) { + BOOST_FOREACH(ElementLoaderMap::value_type l, objLoaders()) { l.second->onPeriodic(); } } diff --git a/project2/xmlObjectLoader.h b/project2/xmlObjectLoader.h index 2ec44e1..7dec077 100644 --- a/project2/xmlObjectLoader.h +++ b/project2/xmlObjectLoader.h @@ -115,14 +115,10 @@ class LoaderBase { #define DECLARE_CUSTOM_LOADER(N, T) \ static void init_loader_##T() __attribute__ ((constructor(201))); \ - static void kill_loader_##T() __attribute__ ((destructor(201))); \ - static void init_loader_##T() { LoaderBase::newLoader(N, new T()); } \ - static void kill_loader_##T() { LoaderBase::removeLoader(N); } + static void init_loader_##T() { LoaderBase::newLoader(N, new T()); } #define DECLARE_LOADER(N, T) \ static void init_loader_##T() __attribute__ ((constructor(201))); \ - static void kill_loader_##T() __attribute__ ((destructor(201))); \ - static void init_loader_##T() { LoaderBase::newLoader(N, new ElementLoaderImpl<T>()); } \ - static void kill_loader_##T() { LoaderBase::removeLoader(N); } + static void init_loader_##T() { LoaderBase::newLoader(N, new ElementLoaderImpl<T>()); } class ElementLoader { public: |