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"
19 #include "ie_common.h"
20 #include "ie_plugin.hpp"
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 
43  /**
44  * @brief Calls a function from the library that creates an object and returns StatusCode
45  * @param name Name of function to load object with
46  * @return If StatusCode provided by function is OK then returns the loaded object. Throws an exception otherwise
47  */
48  template <class T>
49  T* instantiateSymbol(const std::string& name) const {
50  IE_SUPPRESS_DEPRECATED_START
51  T* instance = nullptr;
52  ResponseDesc desc;
53  StatusCode sts = bind_function<StatusCode(T*&, ResponseDesc*)>(name)(instance, &desc);
54  if (sts != OK) {
55  THROW_IE_EXCEPTION << desc.msg;
56  }
57  return instance;
58  IE_SUPPRESS_DEPRECATED_END
59  }
60 
61  /**
62  * @brief Loads function from the library and returns a pointer to it
63  * @param functionName Name of function to load
64  * @return The loaded function
65  */
66  template <class T>
67  std::function<T> bind_function(const std::string& functionName) const {
68  std::function<T> ptr(reinterpret_cast<T*>(_so_loader->get_symbol(functionName.c_str())));
69  return ptr;
70  }
71 
72  IE_SUPPRESS_DEPRECATED_END
73 };
74 
75 /**
76  * @brief This class is a trait class that provides a creator with a function name corresponding to the templated class
77  * parameter
78  */
79 template <class T>
80 class SOCreatorTrait {};
81 
82 /**
83  * @brief This class instantiate object using shared library
84  */
85 template <class T, class Loader = SharedObjectLoader>
86 class SOPointer {
87  IE_SUPPRESS_DEPRECATED_START
88  template <class U, class W>
89  friend class SOPointer;
90 
91 public:
92  /**
93  * @brief Default constructor
94  */
95  SOPointer() = default;
96 
97  /**
98  * @brief The main constructor
99  * @param name Name of a shared library file
100  */
101  template <typename C,
102  typename = enableIfSupportedChar<C>>
103  explicit SOPointer(const std::basic_string<C> & name)
104  : _so_loader(new Loader(name.c_str())),
105  _pointedObj(details::shared_from_irelease(
106  SymbolLoader<Loader>(_so_loader).template instantiateSymbol<T>(SOCreatorTrait<T>::name))) {}
107 
108  explicit SOPointer(const char * name)
109  : _so_loader(new Loader(name)),
110  _pointedObj(details::shared_from_irelease(
111  SymbolLoader<Loader>(_so_loader).template instantiateSymbol<T>(SOCreatorTrait<T>::name))) {}
112 
113  /**
114  * @brief Constructs an object with existing reference
115  * @param _pointedObj_ Existing reference to wrap
116  */
117  explicit SOPointer(T* _pointedObj_): _so_loader(), _pointedObj(_pointedObj_) {
118  if (_pointedObj == nullptr) {
119  THROW_IE_EXCEPTION << "Cannot create SOPointer<T, Loader> from nullptr";
120  }
121  }
122 
123  /**
124  * @brief The copy-like constructor, can create So Pointer that dereferenced into child type if T is derived of U
125  * @param that copied SOPointer object
126  */
127  template <class U, class W>
128  SOPointer(const SOPointer<U, W>& that)
129  : _so_loader(std::dynamic_pointer_cast<Loader>(that._so_loader)),
130  _pointedObj(std::dynamic_pointer_cast<T>(that._pointedObj)) {
131  if (_pointedObj == nullptr) {
132  THROW_IE_EXCEPTION << "Cannot create object from SOPointer<U, W> reference";
133  }
134  }
135 
136  /**
137  * @brief Standard pointer operator
138  * @return underlined interface with disabled Release method
139  */
140  details::NoReleaseOn<T>* operator->() const noexcept {
141  return reinterpret_cast<details::NoReleaseOn<T>*>(_pointedObj.get());
142  }
143 
144  /**
145  * @brief Standard dereference operator
146  * @return underlined interface with disabled Release method
147  */
148  details::NoReleaseOn<T>* operator*() const noexcept {
149  return this->operator->();
150  }
151 
152  explicit operator bool() const noexcept {
153  return (nullptr != _so_loader) && (nullptr != _pointedObj);
154  }
155 
156  friend bool operator==(std::nullptr_t, const SOPointer& ptr) noexcept {
157  return !ptr;
158  }
159  friend bool operator==(const SOPointer& ptr, std::nullptr_t) noexcept {
160  return !ptr;
161  }
162  friend bool operator!=(std::nullptr_t, const SOPointer& ptr) noexcept {
163  return static_cast<bool>(ptr);
164  }
165  friend bool operator!=(const SOPointer& ptr, std::nullptr_t) noexcept {
166  return static_cast<bool>(ptr);
167  }
168 
169  SOPointer& operator=(const SOPointer& pointer) noexcept {
170  _pointedObj = pointer._pointedObj;
171  _so_loader = pointer._so_loader;
172  return *this;
173  }
174 
175 protected:
176  /**
177  * @brief Gets a smart pointer to the DLL
178  */
179  std::shared_ptr<Loader> _so_loader;
180  /**
181  * @brief Gets a smart pointer to the custom object
182  */
183  std::shared_ptr<T> _pointedObj;
184  IE_SUPPRESS_DEPRECATED_END
185 };
186 
187 } // namespace details
188 
189 /**
190  * @brief Creates a special shared_pointer wrapper for the given type from a specific shared module
191  * @param name Name of the shared library file
192  */
193 template <class T>
194 inline std::shared_ptr<T> make_so_pointer(const file_name_t& name) = delete;
195 
196 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
Inference Engine API.
Definition: ie_argmax_layer.hpp:15
A header file for Main Inference Engine API.
StatusCode
This enum contains codes for all possible return values of the interface functions.
Definition: ie_common.h:216
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.
This is a header file with common inference engine definitions.
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:207
A header file for the main Inference Engine exception.