From ce514c46cf62c57cb05f2ceb076a1e89d7653a73 Mon Sep 17 00:00:00 2001 From: randomdan Date: Sun, 12 Jun 2011 15:43:18 +0000 Subject: Allow callbacks into any component loader type --- project2/xmlObjectLoader.cpp | 23 +++++++++++++++++------ project2/xmlObjectLoader.h | 16 ++++++++++++++-- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/project2/xmlObjectLoader.cpp b/project2/xmlObjectLoader.cpp index 372081b..b33bf85 100644 --- a/project2/xmlObjectLoader.cpp +++ b/project2/xmlObjectLoader.cpp @@ -11,6 +11,7 @@ unsigned int LoaderBase::depth = 0; std::set LoaderBase::loadedObjects; typedef std::map > ElementLoaderMap; +typedef std::set > ComponentLoaderSet; LoaderBase::LoaderBase(bool r) : recursive(r), @@ -30,6 +31,16 @@ LoaderBase::~LoaderBase() { } +std::set > * & +LoaderBase::componentLoaders() +{ + static std::set > * _compLoaders = NULL; + if (!_compLoaders) { + _compLoaders = new std::set >(); + } + return _compLoaders; +} + void LoaderBase::collectAll(const xmlpp::Element * node, bool childrenOnly, UnsupportedHandling uh) const { @@ -82,8 +93,8 @@ LoaderBase::collectAll(const CommonObjects * co, const xmlpp::Element * node, bo void LoaderBase::onIdle() { - BOOST_FOREACH(ElementLoaderMap::value_type l, *objLoaders()) { - l.second->onIdle(); + BOOST_FOREACH(ComponentLoaderSet::value_type l, *componentLoaders()) { + l->onIdle(); } } @@ -94,16 +105,16 @@ LoaderBase::onIteration() depth = 0; loadedObjects.clear(); - BOOST_FOREACH(ElementLoaderMap::value_type l, *objLoaders()) { - l.second->onIteration(); + BOOST_FOREACH(ComponentLoaderSet::value_type l, *componentLoaders()) { + l->onIteration(); } } void LoaderBase::onPeriodic() { - BOOST_FOREACH(ElementLoaderMap::value_type l, *objLoaders()) { - l.second->onPeriodic(); + BOOST_FOREACH(ComponentLoaderSet::value_type l, *componentLoaders()) { + l->onPeriodic(); } } diff --git a/project2/xmlObjectLoader.h b/project2/xmlObjectLoader.h index e68a390..c86720d 100644 --- a/project2/xmlObjectLoader.h +++ b/project2/xmlObjectLoader.h @@ -16,6 +16,7 @@ Glib::ustring xmlChildText(const xmlpp::Node * p, const Glib::ustring & n); enum UnsupportedHandling { ErrorOnUnsupported, WarnOnUnsupported, IgnoreUnsupported }; class ElementLoader; +class ComponentLoader; class CommonObjects; class Storer; @@ -35,6 +36,8 @@ class LoaderBase { static void onIteration(); static void onPeriodic(); + static std::set > * & componentLoaders(); + template static std::map > * & objLoaders() { @@ -48,18 +51,27 @@ class LoaderBase { template static void newLoader(const std::string & n, T * l) { - objLoaders()->insert(std::pair >(n, boost::shared_ptr(l))); + boost::shared_ptr p = boost::shared_ptr(l); + objLoaders()->insert(std::pair >(n, p)); + componentLoaders()->insert(boost::shared_ptr(p)); } template static void removeLoader(const std::string & n) { std::map > * & o = objLoaders(); - o->erase(n); + std::set > * & c = componentLoaders(); + typename std::map >::iterator i = o->find(n); + c->erase(i->second); + o->erase(i); if (o->empty()) { delete o; o = NULL; } + if (c->empty()) { + delete c; + c = NULL; + } } template -- cgit v1.2.3