diff options
Diffstat (limited to 'project2/common/scriptLoader.h')
-rw-r--r-- | project2/common/scriptLoader.h | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/project2/common/scriptLoader.h b/project2/common/scriptLoader.h index 2b1057b..6e0bfe5 100644 --- a/project2/common/scriptLoader.h +++ b/project2/common/scriptLoader.h @@ -6,6 +6,7 @@ #include <boost/intrusive_ptr.hpp> #include <boost/function.hpp> #include <boost/shared_ptr.hpp> +#include <boost/foreach.hpp> #include "intrusivePtrBase.h" #include "sourceObject.h" #include "scripts_fwd.h" @@ -36,8 +37,29 @@ class LoaderBase { void discardLoadTargets(); static void onAllComponents(const boost::function1<void, ComponentLoader *> &); + template <class CT> + static void onAll(const boost::function<void(CT *)> & func) { + BOOST_FOREACH(const auto & l, *ComponentType<CT>::components()) { + try { + func(l.get()); + } + catch (...) { + } + } + } - static std::set<boost::shared_ptr<ComponentLoader> > * & componentLoaders(); + template <class CT> + class ComponentType { + public: + static std::set<boost::shared_ptr<CT>> * & components() + { + static std::set<boost::shared_ptr<CT>> * _comp = NULL; + if (!_comp) { + _comp = new std::set<boost::shared_ptr<CT>>(); + } + return _comp; + } + }; template <class T> static std::map<std::string, boost::shared_ptr<T> > * & objLoaders() @@ -49,19 +71,19 @@ class LoaderBase { return _objLoaders; } - template <class T> + template <class T, class BT> static void newLoader(const std::string & n, T * l) { boost::shared_ptr<T> p = boost::shared_ptr<T>(l); objLoaders<T>()->insert(std::pair<std::string, boost::shared_ptr<T> >(n, p)); - componentLoaders()->insert(boost::shared_ptr<T>(p)); + ComponentType<BT>::components()->insert(boost::shared_ptr<T>(p)); } - template <class T> + template <class T, class BT> static void removeLoader(const std::string & n) { std::map<std::string, boost::shared_ptr<T> > * & o = objLoaders<T>(); - std::set<boost::shared_ptr<ComponentLoader> > * & c = componentLoaders(); + std::set<boost::shared_ptr<BT> > * & c = ComponentType<BT>::components(); typename std::map<std::string, boost::shared_ptr<T> >::iterator i = o->find(n); c->erase(i->second); o->erase(i); @@ -106,9 +128,9 @@ class LoaderBase { #define DECLARE_CUSTOM_COMPONENT_LOADER(N, I, T, B) \ namespace TOKENPASTE2(I, __LINE__) { \ static void init_loader_##I() __attribute__ ((constructor(201))); \ - static void init_loader_##I() { LoaderBase::newLoader<B>(N, new T()); } \ + static void init_loader_##I() { LoaderBase::newLoader<B, ComponentLoader>(N, new T()); } \ static void kill_loader_##I() __attribute__ ((destructor(201))); \ - static void kill_loader_##I() { LoaderBase::removeLoader<B>(N); } \ + static void kill_loader_##I() { LoaderBase::removeLoader<B, ComponentLoader>(N); } \ } #define DECLARE_CUSTOM_LOADER(N, T) \ DECLARE_CUSTOM_COMPONENT_LOADER(N, T, T, ElementLoader) @@ -120,7 +142,6 @@ namespace TOKENPASTE2(I, __LINE__) { \ DECLARE_CUSTOM_COMPONENT_LOADER(N, T, B::For<T>, B); /// Helper for loading and maintaining Project2 components -class Options; class ComponentLoader { public: virtual ~ComponentLoader() = 0; @@ -130,7 +151,6 @@ class ComponentLoader { virtual void onIteration(); // When the app engine has completed an iteration virtual void onPeriodic(); // When the app engine feels like it virtual void onConfigLoad(); // When the environment reloads the configuration - virtual const Options * options() const; // Options to be populated from the common config file/env/etc virtual bool cacheable() const { return true; } // The component can be cached for next run }; |