#pragma once //////////////////////////////////////////////////////////////////////// // To store those data which is specific for every plugin itself // Must be used header-only! // // [Usage] // // PluginOwnData::set("name","hello!"); // ...... // ...... // cout << PluginOwnData::get("name") << endl; // "hello!" // ...... // if(PluginOwnData::has("name")) // PluginOwnData::remove("name"); // //////////////////////////////////////////////////////////////////////// #include #include "WinHelper.h" #include #include #include "../Global.h" LIAPI extern std::unordered_map> ll_PluginOwnData; namespace PluginOwnData { inline bool hasImpl(HMODULE hPlugin, const std::string& key) { return ll_PluginOwnData[hPlugin].find(key) != ll_PluginOwnData[hPlugin].end(); } template inline void removeImpl(HMODULE hPlugin, const std::string& key) { if (hasImpl(hPlugin, key)) { delete (T*)ll_PluginOwnData[hPlugin][key]; } } template inline T& setImpl(HMODULE hPlugin, const std::string& key, const Args&... args) { removeImpl(hPlugin, key); T* res = new T(args...); ll_PluginOwnData[hPlugin][key] = res; return *res; } template inline T& setWithoutNewImpl(HMODULE hPlugin, const std::string& key, T* val) { removeImpl(hPlugin, key); ll_PluginOwnData[hPlugin][key] = val; return *val; } template inline T& getImpl(HMODULE hPlugin, const std::string& key) { if (!hasImpl(hPlugin, key)) { throw std::out_of_range("The specified key is not found!"); } return *(T*)ll_PluginOwnData[hPlugin][key]; } template inline T& getOrImpl(HMODULE hPlugin, const std::string& key, const Args&... args) { if (!hasImpl(hPlugin, key)) { return setImpl(hPlugin, key, args...); } return *(T*)ll_PluginOwnData[hPlugin][key]; } template inline T& set(const std::string& key, const Args&... args) { return setImpl(GetCurrentModule(), key, args...); } template inline T& setWithoutNew(const std::string& key, T* val) { return setWithoutNewImpl(GetCurrentModule(), key, val); } template inline T& get(const std::string& key) { return getImpl(GetCurrentModule(), key); } template inline T& getOr(const std::string& key, const Args& ... args) { return getOrImpl(GetCurrentModule(), key, args...); } inline bool has(const std::string& key) { return hasImpl(GetCurrentModule(), key); } template inline void remove(const std::string& key) { return removeImpl(GetCurrentModule(), key); } } // namespace PluginOwnData