Publishing 2020.1 content
[platform/upstream/dldt.git] / inference-engine / include / ie_extension.h
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 /**
6  * @brief A header file that defines a wrapper class for handling extension instantiation and releasing resources
7  *
8  * @file ie_extension.h
9  */
10 #pragma once
11
12 #include <map>
13 #include <memory>
14 #include <string>
15
16 #include "details/ie_so_pointer.hpp"
17 #include "ie_iextension.h"
18
19 namespace InferenceEngine {
20 namespace details {
21
22 /**
23  * @brief The SOCreatorTrait class specialization for IExtension case, defines the name of the fabric method for
24  * creating IExtension object in DLL
25  */
26 template <>
27 class SOCreatorTrait<IExtension> {
28 public:
29     /**
30      * @brief A name of the fabric method for creating an IExtension object in DLL
31      */
32     static constexpr auto name = "CreateExtension";
33 };
34
35 /**
36  * @brief The SOCreatorTrait class specialization for IExtension case, defines the name of the fabric method for
37  * creating IExtension object in DLL
38  */
39 template <>
40 class SOCreatorTrait<IShapeInferExtension> {
41 public:
42     /**
43      * @brief A name of the fabric method for creating an IShapeInferExtension object in DLL
44      */
45     static constexpr auto name = "CreateShapeInferExtension";
46 };
47
48 }  // namespace details
49
50 /**
51  * @brief This class is a C++ helper to work with objects created using extensions.
52  */
53 class Extension : public IExtension {
54 public:
55     /**
56      * @brief Loads extension from a shared library
57      *
58      * @param name Full or relative path to extension library
59      */
60     explicit Extension(const file_name_t& name): actual(name) {}
61
62     /**
63      * @brief Gets the extension version information
64      *
65      * @param versionInfo A pointer to version info, set by the plugin
66      */
67     void GetVersion(const InferenceEngine::Version*& versionInfo) const noexcept override {
68         actual->GetVersion(versionInfo);
69     }
70
71     /**
72      * @brief Sets a log callback that is used to track what is going on inside
73      *
74      * @param listener Logging listener
75      */
76     void SetLogCallback(InferenceEngine::IErrorListener& listener) noexcept override {
77         actual->SetLogCallback(listener);
78     }
79
80     /**
81      * @brief Cleans the resources up
82      */
83     void Unload() noexcept override {
84         actual->Unload();
85     }
86
87     /**
88      * @brief Does nothing since destruction is done via the regular mechanism
89      */
90     void Release() noexcept override {}
91
92     /**
93      * @brief Gets the array with types of layers which are included in the extension
94      *
95      * @param types Types array
96      * @param size Size of the types array
97      * @param resp Response descriptor
98      * @return Status code
99      */
100     StatusCode getPrimitiveTypes(char**& types, unsigned int& size, ResponseDesc* resp) noexcept override {
101         return actual->getPrimitiveTypes(types, size, resp);
102     }
103
104     /**
105      * @brief Gets the factory with implementations for a given layer
106      *
107      * @param factory Factory with implementations
108      * @param cnnLayer A layer to get the factory for
109      * @param resp Response descriptor
110      * @return Status code
111      */
112     StatusCode getFactoryFor(ILayerImplFactory*& factory, const CNNLayer* cnnLayer,
113                              ResponseDesc* resp) noexcept override {
114         return actual->getFactoryFor(factory, cnnLayer, resp);
115     }
116
117     StatusCode getShapeInferImpl(IShapeInferImpl::Ptr& impl, const char* type, ResponseDesc* resp) noexcept override {
118         return actual->getShapeInferImpl(impl, type, resp);
119     }
120
121 protected:
122     /**
123      * @brief A SOPointer instance to the loaded templated object
124      */
125     InferenceEngine::details::SOPointer<IExtension> actual;
126 };
127
128 /**
129  * @brief This class is a C++ helper to work with objects created using extensions.
130  */
131 class ShapeInferExtension : public IShapeInferExtension {
132 public:
133     /**
134      * @brief Loads extension from a shared library
135      *
136      * @param name Full or relative path to extension library
137      */
138     explicit ShapeInferExtension(const file_name_t& name): actual(name) {}
139
140     /**
141      * @brief Gets the extension version information
142      *
143      * @param versionInfo A pointer to version info, set by the plugin
144      */
145     void GetVersion(const InferenceEngine::Version*& versionInfo) const noexcept override {
146         actual->GetVersion(versionInfo);
147     }
148
149     /**
150      * @brief Sets a log callback that is used to track what is going on inside
151      *
152      * @param listener Logging listener
153      */
154     void SetLogCallback(InferenceEngine::IErrorListener& listener) noexcept override {
155         actual->SetLogCallback(listener);
156     }
157
158     /**
159      * @brief Cleans the resources up
160      */
161     void Unload() noexcept override {
162         actual->Unload();
163     }
164
165     /**
166      * @brief Does nothing since destruction is done via the regular mechanism
167      */
168     void Release() noexcept override {}
169
170     /**
171      * @brief Gets the array with types of layers which are included in the extension
172      *
173      * @param types Types array
174      * @param size Size of the types array
175      * @param resp Response descriptor
176      * @return Status code
177      */
178     StatusCode getShapeInferTypes(char**& types, unsigned int& size, ResponseDesc* resp) noexcept override {
179         return actual->getShapeInferTypes(types, size, resp);
180     }
181
182     /**
183      * @brief Gets shape propagation implementation for the given string-type of cnn Layer
184      *
185      * @param impl the vector with implementations which is ordered by priority
186      * @param resp response descriptor
187      * @return status code
188      */
189     StatusCode getShapeInferImpl(IShapeInferImpl::Ptr& impl, const char* type, ResponseDesc* resp) noexcept override {
190         return actual->getShapeInferImpl(impl, type, resp);
191     }
192
193 protected:
194     /**
195      * @brief A SOPointer instance to the loaded templated object
196      */
197     InferenceEngine::details::SOPointer<IShapeInferExtension> actual;
198 };
199
200 /**
201  * @brief Creates a special shared_pointer wrapper for the given type from a specific shared module
202  *
203  * @param name Name of the shared library file
204  * @return shared_pointer A wrapper for the given type from a specific shared module
205  */
206 template <>
207 inline std::shared_ptr<IShapeInferExtension> make_so_pointer(const file_name_t& name) {
208     return std::make_shared<ShapeInferExtension>(name);
209 }
210
211 /**
212  * @brief Creates a special shared_pointer wrapper for the given type from a specific shared module
213  *
214  * @param name Name of the shared library file
215  * @return shared_pointer A wrapper for the given type from a specific shared module
216  */
217 template <>
218 inline std::shared_ptr<IExtension> make_so_pointer(const file_name_t& name) {
219     return std::make_shared<Extension>(name);
220 }
221
222 }  // namespace InferenceEngine