Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / cpp_interfaces / base / ie_plugin_base.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 /**
6  * \brief inference engine plugin API wrapper, to be used by particular implementors
7  * \file ie_plugin_base.hpp
8  */
9
10 #pragma once
11
12 #include <memory>
13 #include <map>
14 #include <string>
15 #include <ie_plugin.hpp>
16 #include "description_buffer.hpp"
17 #include "cpp_interfaces/exception2status.hpp"
18
19 namespace InferenceEngine {
20
21 /**
22  * @brief cpp interface for plugin, to avoid dll boundaries ,and simplify internal development
23  * @tparam T Minimal CPP implementation of IInferencePlugin (e.g. IInferencePluginInternal)
24  * @details can be used to create external wrapper too
25  */
26 template<class T>
27 class PluginBase : public IInferencePlugin {
28 protected:
29     class VersionStore : public Version {
30         std::string _dsc;
31         std::string _buildNumber;
32     public:
33         explicit VersionStore(const Version &v) {
34             _dsc = v.description;
35             _buildNumber = v.buildNumber;
36             description = _dsc.c_str();
37             buildNumber = _buildNumber.c_str();
38             apiVersion = v.apiVersion;
39         }
40     } _version;
41
42     std::shared_ptr<T> _impl;
43
44 public:
45     /**
46      *
47      * @param actualReported version that are to be reported
48      */
49     PluginBase(const Version &actualReported, std::shared_ptr<T> impl) : _version(actualReported) {
50         if (impl.get() == nullptr) {
51             THROW_IE_EXCEPTION << "implementation not defined";
52         }
53         _impl = impl;
54     }
55
56     /**
57      * @brief return plugin's version information
58      * @param versionInfo pointer to version info, will be set by plugin
59      */
60     void GetVersion(const Version *&versionInfo) noexcept override {
61         versionInfo = &_version;
62     }
63
64     void SetLogCallback(IErrorListener &listener) noexcept override {
65         NO_EXCEPT_CALL_RETURN_VOID(_impl->SetLogCallback(listener));
66     }
67
68     StatusCode LoadNetwork(ICNNNetwork &network, ResponseDesc *resp) noexcept override {
69         TO_STATUS(_impl->LoadNetwork(network));
70     }
71
72     StatusCode LoadNetwork(IExecutableNetwork::Ptr &executableNetwork,
73                            ICNNNetwork &network,
74                            const std::map<std::string, std::string> &config,
75                            ResponseDesc *resp)noexcept override {
76         TO_STATUS(_impl->LoadNetwork(executableNetwork, network, config));
77     }
78
79     StatusCode Infer(const Blob &input, Blob &result, ResponseDesc *resp) noexcept override {
80         TO_STATUS(_impl->Infer(input, result));
81     }
82
83     StatusCode Infer(const BlobMap &input, BlobMap &result, ResponseDesc *resp)noexcept override {
84         TO_STATUS(_impl->Infer(input, result));
85     }
86
87     StatusCode GetPerformanceCounts(std::map<std::string,
88             InferenceEngineProfileInfo> &perfMap, ResponseDesc *resp) const noexcept override {
89         TO_STATUS(_impl->GetPerformanceCounts(perfMap));
90     }
91
92     StatusCode AddExtension(InferenceEngine::IExtensionPtr extension,
93                             InferenceEngine::ResponseDesc *resp) noexcept override {
94         TO_STATUS(_impl->AddExtension(extension));
95     }
96
97     StatusCode SetConfig(const std::map<std::string, std::string> &config, ResponseDesc *resp) noexcept override {
98         TO_STATUS(_impl->SetConfig(config));
99     }
100
101     StatusCode ImportNetwork(IExecutableNetwork::Ptr &ret, const std::string &modelFileName,
102                              const std::map<std::string, std::string> &config, ResponseDesc *resp) noexcept override {
103         TO_STATUS(ret = _impl->ImportNetwork(modelFileName, config));
104     }
105
106     void Release() noexcept override {
107         delete this;
108     }
109
110     /**
111      * @depricated Use the version with config parameter
112      */
113     void QueryNetwork(const ICNNNetwork &network, QueryNetworkResult &res) const noexcept override {
114         QueryNetwork(network, {}, res);
115     }
116
117     void QueryNetwork(const ICNNNetwork &network, const std::map<std::string, std::string>& config, QueryNetworkResult &res) const noexcept override {
118         _impl->QueryNetwork(network, config, res);
119     }
120
121 private:
122     ~PluginBase() = default;
123 };
124
125 template<class T>
126 inline IInferencePlugin *make_ie_compatible_plugin(const Version &reported, std::shared_ptr<T> impl) {
127     return new PluginBase<T>(reported, impl);
128 }
129
130 }  // namespace InferenceEngine