PluginManager.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2010 Jacek Sieka, arnetheduck on gmail point com
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00059 #ifndef ADCHPP_PLUGINMANAGER_H
00060 #define ADCHPP_PLUGINMANAGER_H
00061 
00062 #include "Singleton.h"
00063 #include "version.h"
00064 #include "Signal.h"
00065 #include "ClientManager.h"
00066 #include "Plugin.h"
00067 
00068 namespace adchpp {
00069 
00070 class SimpleXML;
00071 
00072 #ifdef _WIN32
00073 
00074 #ifdef BUILDING_ADCHPP
00075 #define PLUGIN_API
00076 #else
00077 #define PLUGIN_API __declspec(dllexport)
00078 #endif
00079 
00080 typedef HMODULE plugin_t;
00081 
00082 #else // WIN32
00083 
00084 #ifdef BUILDING_ADCHPP
00085 #define PLUGIN_API
00086 #else
00087 #define PLUGIN_API __attribute__ ((visibility("default")))
00088 #endif
00089 
00090 typedef void* plugin_t;
00091 
00092 #endif // WIN32
00093 
00100 typedef int (*PLUGIN_GET_VERSION)();
00101 
00113 typedef int (*PLUGIN_LOAD)();
00114 
00120 typedef void (*PLUGIN_UNLOAD)();
00121 
00122 class PluginManager : public Singleton<PluginManager>
00123 {
00124 public:
00125     typedef std::unordered_map<std::string, Plugin*> Registry;
00126     typedef Registry::iterator RegistryIter;
00127 
00133     ADCHPP_DLL void attention(const std::function<void()>& f);
00134 
00138     const StringList& getPluginList() const {
00139         return plugins;
00140     }
00141 
00142     void setPluginList(const StringList& pluginList) { plugins = pluginList; }
00143 
00147     const std::string& getPluginPath() const {
00148         return pluginPath;
00149     }
00150 
00151     void setPluginPath(const std::string& path) { pluginPath = path; }
00152 
00159     PluginDataHandle registerPluginData(const PluginDataDeleter& deleter_) { return PluginDataHandle(new PluginData(deleter_)); }
00160 
00165     bool registerPlugin(const std::string& name, Plugin* ptr) {
00166         return registry.insert(std::make_pair(name, ptr)).second;
00167     }
00168 
00170     bool unregisterPlugin(const std::string& name) {
00171         return registry.erase(name) > 0;
00172     }
00173 
00177     Plugin* getPlugin(const std::string& name) {
00178         RegistryIter i = registry.find(name);
00179         return i == registry.end() ? NULL : i->second;
00180     }
00181 
00185     const Registry& getPlugins() const {
00186         return registry;
00187     }
00188 
00189     typedef SignalTraits<void (Entity&, const StringList&, bool&)>::Signal CommandSignal;
00190     typedef CommandSignal::Slot CommandSlot;
00196     ADCHPP_DLL ClientManager::SignalReceive::Connection onCommand(const std::string& commandName, const CommandSlot& f);
00198     ADCHPP_DLL CommandSignal& getCommandSignal(const std::string& commandName);
00199 
00201     void load();
00203     void shutdown();
00204 
00205 private:
00206     virtual ~PluginManager() throw();
00207 
00208     class PluginInfo {
00209     public:
00210 
00211         PluginInfo(plugin_t h, PLUGIN_GET_VERSION v, PLUGIN_LOAD l, PLUGIN_UNLOAD u) :
00212         handle(h), pluginGetVersion(v), pluginLoad(l), pluginUnload(u) { }
00213 
00214         plugin_t handle;
00215         PLUGIN_GET_VERSION pluginGetVersion;
00216         PLUGIN_LOAD pluginLoad;
00217         PLUGIN_UNLOAD pluginUnload;
00218     };
00219 
00220     struct CommandDispatch {
00221         CommandDispatch(const std::string& name_, const PluginManager::CommandSlot& f_);
00222 
00223         void operator()(Entity& e, AdcCommand& cmd, bool& ok);
00224 
00225     private:
00226         std::string name;
00227         PluginManager::CommandSlot f;
00228     };
00229 
00230     friend struct CommandDispatch;
00231 
00232     friend class Singleton<PluginManager>;
00233     ADCHPP_DLL static PluginManager* instance;
00234 
00235     typedef std::vector<PluginInfo> PluginList;
00236     typedef PluginList::iterator PluginIter;
00237 
00238     PluginList active;
00239     Registry registry;
00240 
00241     StringList plugins;
00242     std::string pluginPath;
00243 
00244     static const std::string className;
00245 
00246     PluginManager() throw();
00247 
00248     bool loadPlugin(const std::string& file);
00249 
00250     typedef std::unordered_map<std::string, CommandSignal> CommandHandlers;
00251     CommandHandlers commandHandlers;
00252     bool handleCommand(Entity& e, const StringList& l);
00253 };
00254 
00255 }
00256 
00257 #endif // PLUGINMANAGER_H
Generated on Sat Nov 27 23:37:53 2010 for adchpp by  doxygen 1.6.3