From 2ad44119bfa22d538083ab227ee25e3839feea21 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vitaliy=20Cherepanov/AI=20Tools=20Lab/Engineer/=EC=82=BC?= =?utf8?q?=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 1 Jun 2018 13:14:22 +0300 Subject: [PATCH] nnc: Implement PluginProxy class (#267) nnc: implement PluginProxy class this is base class to load pluggin Signed-off-by: Vitaliy Cherepanov --- contrib/nnc/include/module/plugin/PluginProxy.h | 57 +++++++++++++++++++ contrib/nnc/src/module/plugin/PluginProxy.cpp | 76 +++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 contrib/nnc/include/module/plugin/PluginProxy.h create mode 100644 contrib/nnc/src/module/plugin/PluginProxy.cpp diff --git a/contrib/nnc/include/module/plugin/PluginProxy.h b/contrib/nnc/include/module/plugin/PluginProxy.h new file mode 100644 index 0000000..b836bb0 --- /dev/null +++ b/contrib/nnc/include/module/plugin/PluginProxy.h @@ -0,0 +1,57 @@ +#ifndef __PLUGIN_PROXY_H__ +#define __PLUGIN_PROXY_H__ + +#include +#include +#include + +#include "shared_library.h" +#include "PluginException.h" +#include "PluginInstance.h" + +namespace nncc +{ +namespace contrib +{ +namespace module +{ +namespace plugin +{ + +class PluginProxy +{ +public: + PluginProxy() = delete; + static std::shared_ptr create(const std::string &pluginPath); + virtual ~PluginProxy(); + + const std::string &getPluginPath() const; + const std::string &getPluginName() const; + + std::shared_ptr getPluginInstance(); + +public: + static const std::string getInstanceFuncName; + +private: + explicit PluginProxy(const std::string &pluginPath); + + void init() noexcept(false); + void *getFuncAddr(const std::string &func_name) noexcept(false); + friend std::ostream &operator<<(std::ostream &st, const PluginProxy &pl); + +private: + typedef std::shared_ptr (*get_instance_t)(); + + std::shared_ptr _pluginInstance; + std::shared_ptr _lib; + get_instance_t _getInstance; + std::string _pluginName; +}; + +} // namespace plugin +} // namespace module +} // namespace contrib +} // namespace nncc + +#endif /* __PLUGIN_PROXY_H__ */ diff --git a/contrib/nnc/src/module/plugin/PluginProxy.cpp b/contrib/nnc/src/module/plugin/PluginProxy.cpp new file mode 100644 index 0000000..a5f085f --- /dev/null +++ b/contrib/nnc/src/module/plugin/PluginProxy.cpp @@ -0,0 +1,76 @@ +#include +#include +#include "module/plugin/PluginProxy.h" + +namespace nncc +{ +namespace contrib +{ +namespace module +{ +namespace plugin +{ + +const std::string PluginProxy::getInstanceFuncName = "get_instance"; + +void *PluginProxy::getFuncAddr(const std::string &func_name) { + void *fp; + fp = _lib->findFunc(func_name); + + if (fp == nullptr) + throw PluginException(std::string("function not found: <") + func_name + ">"); + return fp; +} + +PluginProxy::PluginProxy(const std::string &pluginPath) : _getInstance(nullptr), _lib(nullptr), _pluginInstance(nullptr) { + _lib = std::make_shared(pluginPath); + + auto i = pluginPath.find_last_of('/'); + if (i != std::string::npos) + _pluginName = pluginPath.substr(i + 1); + else + _pluginName = pluginPath; +} + +void PluginProxy::init() noexcept(false) { + // Fill instance + _getInstance = (PluginProxy::get_instance_t) getFuncAddr(getInstanceFuncName); + _pluginInstance = _getInstance(); + + if (_pluginInstance == nullptr) + throw PluginException("bad plugin instance"); +} + +std::shared_ptr PluginProxy::create(const std::string &pluginPath) { + std::shared_ptr pl = nullptr; + try { + pl = std::shared_ptr(new PluginProxy(pluginPath)); + pl->init(); + return pl; + } + catch (PluginException &e) { + throw PluginException(e, "cannot create plugin"); + } +} + +PluginProxy::~PluginProxy() {} + +std::ostream &operator<<(std::ostream &st, const PluginProxy &pl) { + st << *(pl._lib); + return st; +} + +const std::string &PluginProxy::getPluginPath() const { return _lib->getPath(); } + +const std::string &PluginProxy::getPluginName() const { return _pluginName; } + +std::shared_ptr PluginProxy::getPluginInstance() { + if (_pluginInstance == nullptr) + throw PluginException(std::string("bad plugin instance <") + getPluginPath() + ">"); + return _pluginInstance; +} + +} // namespace plugins +} // namespace module +} // namespace contrib +} // namespace nncc -- 2.7.4