diff options
Diffstat (limited to 'project2/xmlObjectLoader.cpp')
-rw-r--r-- | project2/xmlObjectLoader.cpp | 70 |
1 files changed, 26 insertions, 44 deletions
diff --git a/project2/xmlObjectLoader.cpp b/project2/xmlObjectLoader.cpp index 30e2865..9187c82 100644 --- a/project2/xmlObjectLoader.cpp +++ b/project2/xmlObjectLoader.cpp @@ -1,55 +1,32 @@ #include "xmlObjectLoader.h" #include "xmlStorage.h" -#include "exceptions.h" #include "logger.h" +#include "library.h" +#include "appEngine.h" #include <boost/shared_ptr.hpp> #include <boost/foreach.hpp> -#include <dlfcn.h> #include <libxml++/nodes/textnode.h> unsigned int LoaderBase::depth = 0; std::set<SourceObjectPtr> LoaderBase::loadedObjects; typedef std::map<std::string, boost::shared_ptr<ElementLoader> > ElementLoaderMap; -ElementLoaderMap & +ElementLoaderMap * & objLoaders() { - static ElementLoaderMap * _objLoaders = new ElementLoaderMap(); - return *_objLoaders; + static ElementLoaderMap * _objLoaders = NULL; + if (!_objLoaders) { + _objLoaders = new ElementLoaderMap(); + } + return _objLoaders; } -SimpleMessageException(LoadLibraryFailed); -SimpleMessageException(UnloadLibraryFailed); - -class Library : public SourceObject { - public: - Library(const xmlpp::Element * p) : - SourceObject(p), - handle(dlopen(p->get_attribute_value("path").c_str(), RTLD_NOW)) - { - if (!handle) { - throw LoadLibraryFailed(dlerror()); - } - } - ~Library() { - if (dlclose(handle)) { - throw UnloadLibraryFailed(dlerror()); - } - } - void loadComplete(const CommonObjects*) { - } - private: - void * handle; -}; -Storage<Library>::Objects libraries; -class LibraryLoader : public ElementLoaderImpl<Library> { - public: - void onIteration() - { - libraries.clear(); - } -}; -DECLARE_CUSTOM_LOADER("library", LibraryLoader); +LoaderBase::LoaderBase(bool r) : + recursive(r), + ns(ApplicationEngine::getCurrent()->env()->getXmlNamespace()) +{ + supportedStorers.insert(Storer::into(&libraries)); +} LoaderBase::LoaderBase(const Glib::ustring & n, bool r) : recursive(r), @@ -73,8 +50,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); @@ -120,19 +97,24 @@ 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, boost::shared_ptr<ElementLoader>(l))); + objLoaders()->insert(ElementLoaderMap::value_type(n, boost::shared_ptr<ElementLoader>(l))); } void LoaderBase::removeLoader(const std::string & n) { - objLoaders().erase(n); + ElementLoaderMap * & o = objLoaders(); + o->erase(n); + if (o->empty()) { + delete o; + o = NULL; + } } void LoaderBase::onIdle() { - BOOST_FOREACH(ElementLoaderMap::value_type l, objLoaders()) { + BOOST_FOREACH(ElementLoaderMap::value_type l, *objLoaders()) { l.second->onIdle(); } } @@ -144,7 +126,7 @@ LoaderBase::onIteration() depth = 0; loadedObjects.clear(); - BOOST_FOREACH(ElementLoaderMap::value_type l, objLoaders()) { + BOOST_FOREACH(ElementLoaderMap::value_type l, *objLoaders()) { l.second->onIteration(); } } @@ -152,7 +134,7 @@ LoaderBase::onIteration() void LoaderBase::onPeriodic() { - BOOST_FOREACH(ElementLoaderMap::value_type l, objLoaders()) { + BOOST_FOREACH(ElementLoaderMap::value_type l, *objLoaders()) { l.second->onPeriodic(); } } |