X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/12aae76fbeaa3dc91ab5ecad5531a2c1925340d2..8bfc0bdce2bf4f53ac2c072e7a44e9a7d3b3cca8:/projects/stargazer/store_loader.cpp?ds=sidebyside diff --git a/projects/stargazer/store_loader.cpp b/projects/stargazer/store_loader.cpp index dbb6d36e..91646853 100644 --- a/projects/stargazer/store_loader.cpp +++ b/projects/stargazer/store_loader.cpp @@ -18,113 +18,103 @@ * Author : Maxim Mamontov */ -/* - $Revision: 1.6 $ - $Date: 2010/03/04 12:24:19 $ - $Author: faust $ - */ - -/* - * An implementation of RAII store plugin loader - */ - #include +#include "stg/common.h" +#include "stg/store.h" #include "store_loader.h" -#include "common.h" -#include "store.h" #include "settings_impl.h" -STORE_LOADER::STORE_LOADER(const SETTINGS_IMPL & settings) +using STG::StoreLoader; + +StoreLoader::StoreLoader(const SettingsImpl& settings) noexcept : isLoaded(false), handle(NULL), plugin(NULL), - errorStr(), storeSettings(settings.GetStoreModuleSettings()), pluginFileName(settings.GetModulesPath() + "/mod_" + storeSettings.moduleName + ".so") { } -STORE_LOADER::~STORE_LOADER() +StoreLoader::~StoreLoader() { -Unload(); + unload(); } -bool STORE_LOADER::Load() +bool StoreLoader::load() noexcept { -if (isLoaded) + if (isLoaded) { - errorStr = "Store plugin '" + pluginFileName + "' was alredy loaded!"; - printfd(__FILE__, "STORE_LOADER::Load() - %s\n", errorStr.c_str()); - return false; + errorStr = "Store plugin '" + pluginFileName + "' was already loaded!"; + printfd(__FILE__, "StoreLoader::load() - %s\n", errorStr.c_str()); + return false; } -if (pluginFileName.empty()) + if (pluginFileName.empty()) { - errorStr = "Empty store plugin filename"; - printfd(__FILE__, "STORE_LOADER::Load() - %s\n", errorStr.c_str()); - return true; + errorStr = "Empty store plugin filename"; + printfd(__FILE__, "StoreLoader::load() - %s\n", errorStr.c_str()); + return true; } -handle = dlopen(pluginFileName.c_str(), RTLD_NOW); + handle = dlopen(pluginFileName.c_str(), RTLD_NOW); -if (!handle) + if (!handle) { - errorStr = "Error loading plugin '" - + pluginFileName + "': '" + dlerror() + "'"; - printfd(__FILE__, "STORE_LOADER::Load() - %s\n", errorStr.c_str()); - return true; + errorStr = "Error loading plugin '" + + pluginFileName + "': '" + dlerror() + "'"; + printfd(__FILE__, "StoreLoader::Load() - %s\n", errorStr.c_str()); + return true; } -isLoaded = true; + isLoaded = true; -STORE * (*GetStore)(); -GetStore = (STORE * (*)())dlsym(handle, "GetStore"); -if (!GetStore) + using Getter = Store* (*)(); + auto GetStore = reinterpret_cast(dlsym(handle, "GetStore")); + if (!GetStore) { - errorStr = std::string("GetStore() not found! ") + dlerror(); - printfd(__FILE__, "STORE_LOADER::Load() - %s\n", errorStr.c_str()); - return true; + errorStr = std::string("GetStore() not found! ") + dlerror(); + printfd(__FILE__, "StoreLoader::load() - %s\n", errorStr.c_str()); + return true; } -plugin = GetStore(); + plugin = GetStore(); -if (!plugin) + if (!plugin) { - errorStr = "Plugin was not created!"; - printfd(__FILE__, "STORE_LOADER::Load() - %s\n"); - return true; + errorStr = "Plugin was not created!"; + printfd(__FILE__, "StoreLoader::Load() - %s\n"); + return true; } -plugin->SetSettings(storeSettings); -if (plugin->ParseSettings()) + plugin->SetSettings(storeSettings); + if (plugin->ParseSettings()) { - errorStr = plugin->GetStrError(); - printfd(__FILE__, "STORE_LOADER::Load() - Failed to parse settings. Plugin reports: '%s'\n", errorStr.c_str()); - return true; + errorStr = plugin->GetStrError(); + printfd(__FILE__, "StoreLoader::Load() - Failed to parse settings. Plugin reports: '%s'\n", errorStr.c_str()); + return true; } -return false; + return false; } -bool STORE_LOADER::Unload() +bool StoreLoader::unload() noexcept { -printfd(__FILE__, "STORE_LOADER::Unload()\n"); -if (!isLoaded) - { - return true; - } + if (!isLoaded) + return true; + + delete plugin; -if (dlclose(handle)) + if (dlclose(handle)) { - errorStr = "Failed to unload plugin '"; - errorStr += pluginFileName + "': "; - errorStr += dlerror(); - printfd(__FILE__, "STORE_LOADER::Unload() - %s\n", errorStr.c_str()); - return true; + errorStr = "Failed to unload plugin '"; + errorStr += pluginFileName + "': "; + errorStr += dlerror(); + printfd(__FILE__, "StoreLoader::Unload() - %s\n", errorStr.c_str()); + return true; } -isLoaded = false; + isLoaded = false; -return false; + return false; }