ie_so_pointer.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief This is a wrapper class for handling plugin instantiation and releasing resources
7  * @file ie_so_pointer.hpp
8  */
9 #pragma once
10 
11 #include <cassert>
12 #include <memory>
13 #include <string>
14 #include <type_traits>
15 
16 #include "details/ie_exception.hpp"
18 #include "details/ie_irelease.hpp"
20 #include "ie_common.h"
21 #include "ie_so_loader.h"
22 
23 namespace InferenceEngine {
24 namespace details {
25 
26 /**
27  * @brief This class is a C++ helper to load a symbol from a library and create its instance
28  */
29 template <class Loader>
30 class SymbolLoader {
31 private:
32  std::shared_ptr<Loader> _so_loader;
33 
34 public:
35  IE_SUPPRESS_DEPRECATED_START
36 
37  /**
38  * @brief The main constructor
39  * @param loader Library to load from
40  */
41  explicit SymbolLoader(std::shared_ptr<Loader> loader): _so_loader(loader) {
42  if (_so_loader == nullptr) {
43  THROW_IE_EXCEPTION << "SymbolLoader cannot be created with nullptr";
44  }
45  }
46 
47  /**
48  * @brief Calls a function from the library that creates an object and returns StatusCode
49  * @param name Name of function to load object with
50  * @return If StatusCode provided by function is OK then returns the loaded object. Throws an exception otherwise
51  */
52  template <class T>
53  T* instantiateSymbol(const std::string& name) const {
54  IE_SUPPRESS_DEPRECATED_START
55  T* instance = nullptr;
56  ResponseDesc desc;
57  StatusCode sts = bind_function<StatusCode(T*&, ResponseDesc*)>(name)(instance, &desc);
58  if (sts != OK) {
59  THROW_IE_EXCEPTION << desc.msg;
60  }
61  return instance;
62  IE_SUPPRESS_DEPRECATED_END
63  }
64 
65 private:
66  /**
67  * @brief Loads function from the library and returns a pointer to it
68  * @param functionName Name of function to load
69  * @return The loaded function
70  */
71  template <class T>
72  std::function<T> bind_function(const std::string& functionName) const {
73  std::function<T> ptr(reinterpret_cast<T*>(_so_loader->get_symbol(functionName.c_str())));
74  return ptr;
75  }
76 
77  IE_SUPPRESS_DEPRECATED_END
78 };
79 
80 /**
81  * @brief This class is a trait class that provides a creator with a function name corresponding to the templated class
82  * parameter
83  */
84 template <class T>
85 class SOCreatorTrait {};
86 
87 /**
88  * @brief This class instantiate object using shared library
89  * @tparam T An type of object SOPointer can hold
90  * @tparam Loader A loader used to load a library
91  */
92 template <class T, class Loader = SharedObjectLoader>
93 class SOPointer {
94  IE_SUPPRESS_DEPRECATED_START
95  template <class U, class W>
96  friend class SOPointer;
97 
98 public:
99  /**
100  * @brief Default constructor
101  */
102  SOPointer() = default;
103 
104  /**
105  * @brief The main constructor
106  * @param name Name of a shared library file
107  */
108  template <typename C,
109  typename = enableIfSupportedChar<C>>
110  explicit SOPointer(const std::basic_string<C> & name)
111  : _so_loader(new Loader(name.c_str())),
112  _pointedObj(details::shared_from_irelease(
113  SymbolLoader<Loader>(_so_loader).template instantiateSymbol<T>(SOCreatorTrait<T>::name))) {}
114 
115  /**
116  * @brief The main constructor
117  * @param name Name of a shared library file
118  */
119  explicit SOPointer(const char * name)
120  : _so_loader(new Loader(name)),
121  _pointedObj(details::shared_from_irelease(
122  SymbolLoader<Loader>(_so_loader).template instantiateSymbol<T>(SOCreatorTrait<T>::name))) {}
123 
124  /**
125  * @brief Constructs an object with existing reference
126  * @param pointedObj Existing reference to wrap
127  */
128  explicit SOPointer(T* pointedObj): _so_loader(), _pointedObj(pointedObj) {
129  if (_pointedObj == nullptr) {
130  THROW_IE_EXCEPTION << "Cannot create SOPointer<T, Loader> from nullptr";
131  }
132  }
133 
134  /**
135  * @brief Constructs an object with existing loader
136  * @param so_loader Existing pointer to a library loader
137  */
138  explicit SOPointer(std::shared_ptr<Loader> so_loader)
139  : _so_loader(so_loader),
140  _pointedObj(details::shared_from_irelease(
141  SymbolLoader<Loader>(_so_loader).template instantiateSymbol<T>(SOCreatorTrait<T>::name))) {}
142 
143  /**
144  * @brief The copy-like constructor, can create So Pointer that dereferenced into child type if T is derived of U
145  * @param that copied SOPointer object
146  */
147  template <class U, class W>
148  SOPointer(const SOPointer<U, W>& that)
149  : _so_loader(std::dynamic_pointer_cast<Loader>(that._so_loader)),
150  _pointedObj(std::dynamic_pointer_cast<T>(that._pointedObj)) {
151  if (_pointedObj == nullptr) {
152  THROW_IE_EXCEPTION << "Cannot create object from SOPointer<U, W> reference";
153  }
154  }
155 
156  /**
157  * @brief Standard pointer operator
158  * @return underlined interface with disabled Release method
159  */
160  details::NoReleaseOn<T>* operator->() const noexcept {
161  return reinterpret_cast<details::NoReleaseOn<T>*>(_pointedObj.get());
162  }
163 
164  /**
165  * @brief Standard dereference operator
166  * @return underlined interface with disabled Release method
167  */
168  details::NoReleaseOn<T>* operator*() const noexcept {
169  return this->operator->();
170  }
171 
172  explicit operator bool() const noexcept {
173  return (nullptr != _so_loader) && (nullptr != _pointedObj);
174  }
175 
176  friend bool operator==(std::nullptr_t, const SOPointer& ptr) noexcept {
177  return !ptr;
178  }
179  friend bool operator==(const SOPointer& ptr, std::nullptr_t) noexcept {
180  return !ptr;
181  }
182  friend bool operator!=(std::nullptr_t, const SOPointer& ptr) noexcept {
183  return static_cast<bool>(ptr);
184  }
185  friend bool operator!=(const SOPointer& ptr, std::nullptr_t) noexcept {
186  return static_cast<bool>(ptr);
187  }
188 
189  SOPointer& operator=(const SOPointer& pointer) noexcept {
190  _pointedObj = pointer._pointedObj;
191  _so_loader = pointer._so_loader;
192  return *this;
193  }
194 
195  operator std::shared_ptr<Loader>() const noexcept {
196  return _so_loader;
197  }
198 
199 protected:
200  /**
201  * @brief Gets a smart pointer to the DLL
202  */
203  std::shared_ptr<Loader> _so_loader;
204 
205  /**
206  * @brief Gets a smart pointer to the custom object
207  */
208  std::shared_ptr<T> _pointedObj;
209  IE_SUPPRESS_DEPRECATED_END
210 };
211 
212 } // namespace details
213 
214 /**
215  * @brief Creates a special shared_pointer wrapper for the given type from a specific shared module
216  * @tparam T An type of object SOPointer can hold
217  * @param name Name of the shared library file
218  * @return A created object
219  */
220 template <class T>
221 inline std::shared_ptr<T> make_so_pointer(const file_name_t& name) = delete;
222 
223 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
Definition: cldnn_config.hpp:16
StatusCode
This enum contains codes for all possible return values of the interface functions.
Definition: ie_common.h:224
A bias layout for opearation.
Definition: ie_common.h:98
std::shared_ptr< T > make_so_pointer(const file_name_t &name)=delete
Creates a special shared_pointer wrapper for the given type from a specific shared module...
Definition: ie_extension.h:285
Utility header file. Provides no release base class.
This is a header file with functions related to filesystem operations.
A header file for definition of abstraction over platform specific shared objects.
A header file for the Inference Engine plugins destruction mechanism.
This is a header file with common inference engine definitions.
A header file for the main Inference Engine exception.