PluginManager.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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