Publishing 2019 R1.1 content and Myriad plugin sources (#162)
[platform/upstream/dldt.git] / inference-engine / src / hetero_plugin / 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
20 namespace InferenceEngine {
21
22 /**
23  * @brief cpp interface for plugin, to avoid dll boundaries ,and simplify internal development
24  * @tparam T Minimal CPP implementation of IInferencePlugin (e.g. IInferencePluginInternal)
25  * @details can be used to create external wrapper too
26  */
27 template<class T>
28 class HeteroPluginBase : public IHeteroInferencePlugin {
29 protected:
30     class VersionStore : public Version {
31         std::string _dsc;
32         std::string _buildNumber;
33     public:
34         explicit VersionStore(const Version &v) {
35             _dsc = v.description;
36             _buildNumber = v.buildNumber;
37             description = _dsc.c_str();
38             buildNumber = _buildNumber.c_str();
39             apiVersion = v.apiVersion;
40         }
41     } _version;
42
43     std::shared_ptr<T> _impl;
44
45 public:
46     /**
47      *
48      * @param actualReported version that are to be reported
49      */
50     HeteroPluginBase(const Version &actualReported, std::shared_ptr<T> impl) : _version(actualReported) {
51         if (impl.get() == nullptr) {
52             THROW_IE_EXCEPTION << "implementation not defined";
53         }
54         _impl = impl;
55     }
56
57     /**
58      * @brief return plugin's version information
59      * @param versionInfo pointer to version info, will be set by plugin
60      */
61     void GetVersion(const Version *&versionInfo) noexcept override {
62         versionInfo = &_version;
63     }
64
65     void SetLogCallback(IErrorListener &listener) noexcept override {
66         NO_EXCEPT_CALL_RETURN_VOID(_impl->SetLogCallback(listener));
67     }
68
69     StatusCode LoadNetwork(ICNNNetwork &network, ResponseDesc *resp) noexcept override {
70         TO_STATUS(_impl->LoadNetwork(network));
71     }
72
73     StatusCode LoadNetwork(IExecutableNetwork::Ptr &executableNetwork,
74                            ICNNNetwork &network,
75                            const std::map<std::string, std::string> &config,
76                            ResponseDesc *resp)noexcept override {
77         TO_STATUS(_impl->LoadNetwork(executableNetwork, network, config));
78     }
79
80     StatusCode Infer(const Blob &input, Blob &result, ResponseDesc *resp) noexcept override {
81         TO_STATUS(_impl->Infer(input, result));
82     }
83
84     StatusCode Infer(const BlobMap &input, BlobMap &result, ResponseDesc *resp)noexcept override {
85         TO_STATUS(_impl->Infer(input, result));
86     }
87
88     StatusCode GetPerformanceCounts(std::map<std::string,
89             InferenceEngineProfileInfo> &perfMap, ResponseDesc *resp) const noexcept override {
90         TO_STATUS(_impl->GetPerformanceCounts(perfMap));
91     }
92
93     StatusCode AddExtension(InferenceEngine::IExtensionPtr extension,
94                             InferenceEngine::ResponseDesc *resp) noexcept override {
95         TO_STATUS(_impl->AddExtension(extension));
96     }
97
98     StatusCode SetConfig(const std::map<std::string, std::string> &config, ResponseDesc *resp) noexcept override {
99         TO_STATUS(_impl->SetConfig(config));
100     }
101
102     StatusCode ImportNetwork(IExecutableNetwork::Ptr &ret, const std::string &modelFileName,
103                              const std::map<std::string, std::string> &config, ResponseDesc *resp) noexcept override {
104         TO_STATUS(ret = _impl->ImportNetwork(modelFileName, config));
105     }
106
107     void Release() noexcept override {
108         delete this;
109     }
110
111     void SetDeviceLoader(const std::string &device, IHeteroDeviceLoader::Ptr loader)noexcept override {
112         _impl->SetDeviceLoader(device, loader);
113     }
114
115     StatusCode SetAffinity(
116         ICNNNetwork& network,
117         const std::map<std::string, std::string> &config,
118         ResponseDesc *resp) noexcept override {
119         TO_STATUS(_impl->SetAffinity(network, config));
120     }
121
122
123  private:
124     ~HeteroPluginBase() = default;
125 };
126
127 }  // namespace InferenceEngine