From 95e8f9247f1cc305b90bd5c59bb0cb08faf92dac 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: Tue, 5 Jun 2018 11:30:42 +0300 Subject: [PATCH] nnc: Implement plugin example (#278) nnc: Implement plugin example This commit show how to create plugin for nnc Signed-off-by: Vitaliy Cherepanov --- contrib/nnc/CMakeLists.txt | 1 + contrib/nnc/examples/CMakeLists.txt | 6 ++ contrib/nnc/examples/plugin_example/CMakeLists.txt | 13 +++ contrib/nnc/examples/plugin_example/main.cpp | 93 ++++++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 contrib/nnc/examples/CMakeLists.txt create mode 100644 contrib/nnc/examples/plugin_example/CMakeLists.txt create mode 100644 contrib/nnc/examples/plugin_example/main.cpp diff --git a/contrib/nnc/CMakeLists.txt b/contrib/nnc/CMakeLists.txt index 167726e..b1e1dcc 100644 --- a/contrib/nnc/CMakeLists.txt +++ b/contrib/nnc/CMakeLists.txt @@ -10,3 +10,4 @@ target_link_libraries(nnc PRIVATE dl) target_include_directories(nnc PUBLIC include) add_subdirectory(libs) +add_subdirectory(examples) diff --git a/contrib/nnc/examples/CMakeLists.txt b/contrib/nnc/examples/CMakeLists.txt new file mode 100644 index 0000000..8a4d01e --- /dev/null +++ b/contrib/nnc/examples/CMakeLists.txt @@ -0,0 +1,6 @@ +file(GLOB CONTRIB_EXAMPLES_CMAKE_FILES "*/CMakeLists.txt") + +foreach(CONTRIB_EXAMPLES_CMAKE_FILE ${CONTRIB_EXAMPLES_CMAKE_FILES}) + get_filename_component(CONTRIB_BASE ${CONTRIB_EXAMPLES_CMAKE_FILE} DIRECTORY) + add_subdirectory(${CONTRIB_BASE}) +endforeach(CONTRIB_EXAMPLES_CMAKE_FILE ${CONTRIB_EXAMPLES_CMAKE_FILES}) diff --git a/contrib/nnc/examples/plugin_example/CMakeLists.txt b/contrib/nnc/examples/plugin_example/CMakeLists.txt new file mode 100644 index 0000000..dc0219a --- /dev/null +++ b/contrib/nnc/examples/plugin_example/CMakeLists.txt @@ -0,0 +1,13 @@ +file(GLOB_RECURSE PL_EXAMPLE_PARSE_SRC *.cpp) +file(GLOB_RECURSE PL_EXAMPLE_PARSE_H) + +add_library(some_parser SHARED ${PL_EXAMPLE_PARSE_SRC} ${PL_EXAMPLE_PARSE_H}) +add_library(some_parser_second SHARED ${PL_EXAMPLE_PARSE_SRC} ${PL_EXAMPLE_PARSE_H}) + +target_link_libraries(some_parser PRIVATE nnc_plugin_core) +target_link_libraries(some_parser PRIVATE nncc_foundation) +target_link_libraries(some_parser PRIVATE nnc_core) + +target_link_libraries(some_parser_second PRIVATE nnc_plugin_core) +target_link_libraries(some_parser_second PRIVATE nncc_foundation) +target_link_libraries(some_parser_second PRIVATE nnc_core) diff --git a/contrib/nnc/examples/plugin_example/main.cpp b/contrib/nnc/examples/plugin_example/main.cpp new file mode 100644 index 0000000..cbca17f --- /dev/null +++ b/contrib/nnc/examples/plugin_example/main.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include "PluginInstance.h" +#include "PluginException.h" +#include "ConfigException.h" +#include "PluginType.h" + +static const std::string pluginName = "Your plugin name here"; +static const std::string pluginVersion = "Your plugin version here"; +static const std::string pluginDesc = "Your plugin description here"; +static const auto pluginType = nncc::contrib::plugin::typeFrontEnd; + +using namespace nncc::contrib::config; +using namespace nncc::contrib::plugin; + +class SamplePluginInstance : public AbstractPluginInstance +{ +public: + static AbstractPluginInstance &getInstance(); + void fillSession() override; + void checkConfig() override; + void *execute(void *data) override; + + void setParam(const std::string &name) override; + void setParam(const std::string &name, const std::string &value) override; + +private: + std::string _filename; +}; + +AbstractPluginInstance &SamplePluginInstance::getInstance() +{ + static SamplePluginInstance instance; + std::cout << std::endl << "!!! plugin (" << pluginName << ") " << __func__ << std::endl; + + return instance; +} + +void SamplePluginInstance::fillSession() +{ + static std::map info = {{"module description", pluginDesc}}; + + static std::vector moduleParams = {{"filename", "[model file name]", false}, + {"someoption", "[Your description here]", true}}; + + AbstractPluginInstance::fillSessionBase(pluginType, pluginVersion, pluginName); + + for (auto &i : info) + getSession()->addInfo(i.first, i.second); + + for (auto &p : moduleParams) + getSession()->registerParam(p); +} + +void SamplePluginInstance::checkConfig() +{ + std::cout << std::endl << "!!! plugin (" << pluginName << ") " << __func__ << std::endl; + + std::string info = "!!! plugin (" + pluginName + ") " + __func__ + " [Bad configuration]"; + + throw nncc::contrib::ConfigException(info); +} + +void *SamplePluginInstance::execute(void *data) +{ + std::cout << std::endl << "!!! plugin (" << pluginName << ") " << __func__ << std::endl; + return data; +} + +void SamplePluginInstance::setParam(const std::string &name) +{ + throw nncc::contrib::ConfigException("bad parameter <" + name + ">"); +} + +void SamplePluginInstance::setParam(const std::string &name, const std::string &value) +{ + if (name == "filename") + { + _filename = value; + } + else + { + throw nncc::contrib::ConfigException("unsupported parameter <" + name + ">"); + } + std::cout << __func__ << " : " << name << " = " << value << std::endl; +} + +extern "C" AbstractPluginInstance *get_instance() +{ + std::cout << std::endl << "!!! plugin (" << pluginName << ") " << __func__ << std::endl; + return &SamplePluginInstance::getInstance(); +} -- 2.7.4