diff options
Diffstat (limited to 'project2/common/scriptLoader.h')
-rw-r--r-- | project2/common/scriptLoader.h | 133 |
1 files changed, 2 insertions, 131 deletions
diff --git a/project2/common/scriptLoader.h b/project2/common/scriptLoader.h index 6e0bfe5..181292c 100644 --- a/project2/common/scriptLoader.h +++ b/project2/common/scriptLoader.h @@ -4,13 +4,10 @@ #include <set> #include <string> #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 "genLoader.h" #include "scripts_fwd.h" -#include "exceptions.h" #include <glibmm/ustring.h> #include <map> #include <vector> @@ -30,85 +27,12 @@ class LoaderBase { LoaderBase(); virtual ~LoaderBase(); - void collectAll(const CommonObjects * co, bool childrenOnly, ScriptNodePtr script); + void collectAll(const CommonObjects * co, bool childrenOnly, ScriptNodePtr script); void addLoadTarget(ScriptNodePtr src, boost::intrusive_ptr<Storer> target); void addLoadTargetSub(ScriptNodePtr src, const Glib::ustring & name, bool required, boost::intrusive_ptr<Storer> target); 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 (...) { - } - } - } - - 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() - { - static std::map<std::string, boost::shared_ptr<T> > * _objLoaders = NULL; - if (!_objLoaders) { - _objLoaders = new std::map<std::string, boost::shared_ptr<T> >(); - } - return _objLoaders; - } - - 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)); - ComponentType<BT>::components()->insert(boost::shared_ptr<T>(p)); - } - - 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<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); - if (o->empty()) { - delete o; - o = NULL; - } - if (c->empty()) { - delete c; - c = NULL; - } - } - - template <class L, class E> - static boost::shared_ptr<L> getLoader(const std::string & n) - { - typename std::map<std::string, boost::shared_ptr<L> >::const_iterator i = objLoaders<L>()->find(n); - if (i != objLoaders<L>()->end()) { - return i->second; - } - else { - throw E(n); - } - } - private: void collectAll(ScriptNodePtr script, bool childrenOnly, const StorerPtrs & sts) const; static ScriptNodePtr getSub(ScriptNodePtr root, const Glib::ustring & name, bool required); @@ -123,59 +47,6 @@ class LoaderBase { const Glib::ustring ns; }; -#define TOKENPASTE(x, y) x ## y -#define TOKENPASTE2(x, y) TOKENPASTE(x, y) -#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, ComponentLoader>(N, new T()); } \ - static void kill_loader_##I() __attribute__ ((destructor(201))); \ - static void kill_loader_##I() { LoaderBase::removeLoader<B, ComponentLoader>(N); } \ -} -#define DECLARE_CUSTOM_LOADER(N, T) \ - DECLARE_CUSTOM_COMPONENT_LOADER(N, T, T, ElementLoader) -#define DECLARE_COMPONENT_LOADER(N, T, B) \ - DECLARE_CUSTOM_COMPONENT_LOADER(N, T, B::For<T>, B) -#define DECLARE_LOADER(N, T) \ - DECLARE_COMPONENT_LOADER(N, T, ElementLoader) -#define DECLARE_GENERIC_LOADER(N, B, T) \ - DECLARE_CUSTOM_COMPONENT_LOADER(N, T, B::For<T>, B); - -/// Helper for loading and maintaining Project2 components -class ComponentLoader { - public: - virtual ~ComponentLoader() = 0; - virtual void onBegin(); // App engine start up (before settings are processed) - virtual void onBefore(); // Before the app engine processes a request (after settings are processed) - virtual void onIdle(); // When the app engine goes idle - 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 bool cacheable() const { return true; } // The component can be cached for next run -}; - -template <class Impl, typename... Params> -class GenLoader : public ComponentLoader { - public: - template <class T> - class For : public GenLoader<Impl, Params...> { - public: - inline Impl * create(const Params & ... p) const - { - return new T(p...); - } - }; - virtual Impl * create(const Params & ...) const = 0; - inline static Impl * createNew(const std::string & n, const Params & ... p) - { - return LoaderBase::getLoader<GenLoader<Impl, Params...>, NotSupported>(n)->create(p...); - } - inline static boost::shared_ptr<GenLoader<Impl, Params...>> getFor(const std::string & n) - { - return LoaderBase::getLoader<GenLoader<Impl, Params...>, NotSupported>(n); - } -}; - /// Helper for loading and maintaining Project2 script components typedef GenLoader<SourceObject, ScriptNodePtr> ElementLoader; |