11165b27ddde6d698571776da214aa19fb20b42e
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / hetero / hetero_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 <ie_ihetero_plugin.hpp>
17 #include "description_buffer.hpp"
18 #include "cpp_interfaces/exception2status.hpp"
19 #include "cpp_interfaces/base/ie_plugin_base.hpp"
20
21 namespace InferenceEngine {
22
23 /**
24  * @brief cpp interface for plugin, to avoid dll boundaries ,and simplify internal development
25  * @tparam T Minimal CPP implementation of IInferencePlugin (e.g. IInferencePluginInternal)
26  * @details can be used to create external wrapper too
27  */
28 IE_SUPPRESS_DEPRECATED_START
29 template<class T>
30 class HeteroPluginBase : public IHeteroInferencePlugin, public IInferencePluginAPI {
31 protected:
32     class VersionStore : public Version {
33         std::string _dsc;
34         std::string _buildNumber;
35     public:
36         explicit VersionStore(const Version &v) {
37             _dsc = v.description;
38             _buildNumber = v.buildNumber;
39             description = _dsc.c_str();
40             buildNumber = _buildNumber.c_str();
41             apiVersion = v.apiVersion;
42         }
43     } _version;
44
45     std::shared_ptr<T> _impl;
46
47 public:
48     /**
49      *
50      * @param actualReported version that are to be reported
51      */
52     HeteroPluginBase(const Version &actualReported, std::shared_ptr<T> impl) : _version(actualReported) {
53         if (impl.get() == nullptr) {
54             THROW_IE_EXCEPTION << "implementation not defined";
55         }
56         _impl = impl;
57     }
58
59     /**
60      * @brief return plugin's version information
61      * @param versionInfo pointer to version info, will be set by plugin
62      */
63     void GetVersion(const Version *&versionInfo) noexcept override {
64         versionInfo = &_version;
65     }
66
67     void SetName(const std::string & pluginName) noexcept override {
68         _impl->SetName(pluginName);
69     }
70
71     std::string GetName() const noexcept override {
72         return _impl->GetName();
73     }
74
75     Parameter GetConfig(const std::string& name, const std::map<std::string, Parameter> & options) const override {
76         return _impl->GetConfig(name, options);
77     }
78
79     Parameter GetMetric(const std::string& name, const std::map<std::string, Parameter> & options) const override {
80         return _impl->GetMetric(name, options);
81     }
82
83     void SetLogCallback(IErrorListener &listener) noexcept override {
84         NO_EXCEPT_CALL_RETURN_VOID(_impl->SetLogCallback(listener));
85     }
86
87     StatusCode LoadNetwork(ICNNNetwork &network, ResponseDesc *resp) noexcept override {
88         TO_STATUS(_impl->LoadNetwork(network));
89     }
90
91     StatusCode LoadNetwork(IExecutableNetwork::Ptr &executableNetwork,
92                            ICNNNetwork &network,
93                            const std::map<std::string, std::string> &config,
94                            ResponseDesc *resp)noexcept override {
95         TO_STATUS(_impl->LoadNetwork(executableNetwork, network, config));
96     }
97
98     StatusCode Infer(const Blob &input, Blob &result, ResponseDesc *resp) noexcept override {
99         TO_STATUS(_impl->Infer(input, result));
100     }
101
102     StatusCode Infer(const BlobMap &input, BlobMap &result, ResponseDesc *resp)noexcept override {
103         TO_STATUS(_impl->Infer(input, result));
104     }
105
106     StatusCode GetPerformanceCounts(std::map<std::string,
107             InferenceEngineProfileInfo> &perfMap, ResponseDesc *resp) const noexcept override {
108         TO_STATUS(_impl->GetPerformanceCounts(perfMap));
109     }
110
111     StatusCode AddExtension(InferenceEngine::IExtensionPtr extension,
112                             InferenceEngine::ResponseDesc *resp) noexcept override {
113         TO_STATUS(_impl->AddExtension(extension));
114     }
115
116     StatusCode SetConfig(const std::map<std::string, std::string> &config, ResponseDesc *resp) noexcept override {
117         TO_STATUS(_impl->SetConfig(config));
118     }
119
120     StatusCode ImportNetwork(IExecutableNetwork::Ptr &ret, const std::string &modelFileName,
121                              const std::map<std::string, std::string> &config, ResponseDesc *resp) noexcept override {
122         TO_STATUS(ret = _impl->ImportNetwork(modelFileName, config));
123     }
124
125     void QueryNetwork(const InferenceEngine::ICNNNetwork &network,
126         const std::map<std::string, std::string>& config, InferenceEngine::QueryNetworkResult &res) const noexcept override {
127         TO_STATUSVAR(_impl->QueryNetwork(network, config, res), res.rc, &res.resp);
128     }
129
130     void Release() noexcept override {
131         delete this;
132     }
133
134     void SetDeviceLoader(const std::string &device, IHeteroDeviceLoader::Ptr loader)noexcept override {
135         _impl->SetDeviceLoader(device, loader);
136     }
137
138     StatusCode SetAffinity(
139         ICNNNetwork& network,
140         const std::map<std::string, std::string> &config,
141         ResponseDesc *resp) noexcept override {
142         TO_STATUS(_impl->SetAffinity(network, config));
143     }
144
145     void SetCore(ICore* core) noexcept override {
146         _impl->SetCore(core);
147     }
148
149     const ICore& GetCore() const override {
150         IE_ASSERT(nullptr != _impl->GetCore());
151         return *_impl->GetCore();
152     }
153
154  private:
155     ~HeteroPluginBase() = default;
156 };
157 IE_SUPPRESS_DEPRECATED_END
158
159 }  // namespace InferenceEngine