From 55e64c35749b28ac137ceaf956715826ae40c533 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:27:21 +0300 Subject: [PATCH] nnc: Implement AbstractModule class (#268) nnc: implement AbstractModule class This commit implements base module class. Signed-off-by: Vitaliy Cherepanov --- contrib/nnc/include/module/AbstractModule.h | 40 ++++++++++++++ contrib/nnc/src/module/AbstractModule.cpp | 83 +++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 contrib/nnc/include/module/AbstractModule.h create mode 100644 contrib/nnc/src/module/AbstractModule.cpp diff --git a/contrib/nnc/include/module/AbstractModule.h b/contrib/nnc/include/module/AbstractModule.h new file mode 100644 index 0000000..213d29f --- /dev/null +++ b/contrib/nnc/include/module/AbstractModule.h @@ -0,0 +1,40 @@ +#ifndef __ABSTRACT_MODULE_H__ +#define __ABSTRACT_MODULE_H__ + +#include + +#include "module/plugin/PluginData.h" +#include "module/plugin/PluginProxy.h" + +namespace nncc +{ +namespace contrib +{ +namespace module +{ + +class AbstractModule +{ +public: + AbstractModule(); + explicit AbstractModule(contrib::plugin::PluginType moduleType); + virtual ~AbstractModule(); + + void registerPlugin(std::shared_ptr &pl); + virtual void *execute(void *data); + virtual void configure(std::shared_ptr conf); + contrib::plugin::PluginType getModuleType() const; + + friend std::ostream &operator<<(std::ostream &st, const AbstractModule &m); + +private: + std::shared_ptr _activePlugin; + std::vector> _plugins; + contrib::plugin::PluginType _moduleType; +}; + +} // namespace module +} // namespace contrib +} // namespace nncc + +#endif /* __ABSTRACT_MODULE_H__ */ diff --git a/contrib/nnc/src/module/AbstractModule.cpp b/contrib/nnc/src/module/AbstractModule.cpp new file mode 100644 index 0000000..e52cb53 --- /dev/null +++ b/contrib/nnc/src/module/AbstractModule.cpp @@ -0,0 +1,83 @@ +#include +#include +#include + +#include "module/AbstractModule.h" +#include "PluginInstance.h" +#include "module/plugin/PluginSession.h" + +namespace nncc +{ +namespace contrib +{ +namespace module +{ + +AbstractModule::AbstractModule() : _activePlugin(nullptr) {} + +AbstractModule::AbstractModule(contrib::plugin::PluginType moduleType) + : _activePlugin(nullptr), _moduleType(moduleType) { +} + +AbstractModule::~AbstractModule() {} + +void AbstractModule::registerPlugin(std::shared_ptr &pl) { _plugins.push_back(pl); } + +void AbstractModule::configure(std::shared_ptr conf) { + _activePlugin = nullptr; + for (auto &pl : _plugins) { + try { + auto pluginInstance = pl->getPluginInstance(); + auto *session = dynamic_cast(pluginInstance->getSession().get()); + auto resParams = config::DataList::intersection(*conf, session->getSupportedParams()); + + for (auto param : resParams->getElements()) { + if (param.second.hasValue()) + pluginInstance->setParam(param.second.getName(), param.second.getValue()); + else + pluginInstance->setParam(param.second.getName()); + } + + pluginInstance->checkConfig(); + _activePlugin = pl; + break; + } + catch (nncc::contrib::ConfigException &e) { + std::cout << e.what() << std::endl; + } + catch (contrib::config::DataException &e) { + std::cout << e.what() << std::endl; + } + catch (PluginException &e) { + std::cout << e.what() << std::endl; + } + + std::cout << "plugin cannot be configured (" << pl->getPluginName() << ")" << std::endl; + } + + if (_activePlugin == nullptr) + throw ConfigException("Module <" + pluginTypeToStr(_moduleType) + + "> cannot be configured"); +} + +void *AbstractModule::execute(void *data) { + if (_activePlugin == nullptr) + throw ConfigException("Module <" + pluginTypeToStr(_moduleType) + + "> has not been configured!"); + return _activePlugin->getPluginInstance()->execute(data); +} + +contrib::plugin::PluginType AbstractModule::getModuleType() const { return _moduleType; } + +std::ostream &operator<<(std::ostream &st, const AbstractModule &m) { + st << "=== MODULE " << pluginTypeToStr(m._moduleType) << " ===" << std::endl; + st << "plugins {" << std::endl; + for (auto p : m._plugins) + st << " " << p << std::endl; + st << "}" << std::endl; + return st; +} + +} // namespace module +} // namespace contrib +} // namespace nncc -- 2.7.4