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 
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  * @tparam T An type of object SOPointer can hold
85  * @tparam Loader A loader used to load a library
86  */
87 template <class T, class Loader = SharedObjectLoader>
88 class SOPointer {
89  IE_SUPPRESS_DEPRECATED_START
90  template <class U, class W>
91  friend class SOPointer;
92 
93 public:
94  /**
95  * @brief Default constructor
96  */
97  SOPointer() = default;
98 
99  /**
100  * @brief The main constructor
101  * @param name Name of a shared library file
102  */
103  template <typename C,
104  typename = enableIfSupportedChar<C>>
105  explicit SOPointer(const std::basic_string<C> & name)
106  : _so_loader(new Loader(name.c_str())),
107  _pointedObj(details::shared_from_irelease(
108  SymbolLoader<Loader>(_so_loader).template instantiateSymbol<T>(SOCreatorTrait<T>::name))) {}
109 
110  /**
111  * @brief The main constructor
112  * @param name Name of a shared library file
113  */
114  explicit SOPointer(const char * name)
115  : _so_loader(new Loader(name)),
116  _pointedObj(details::shared_from_irelease(
117  SymbolLoader<Loader>(_so_loader).template instantiateSymbol<T>(SOCreatorTrait<T>::name))) {}
118 
119  /**
120  * @brief Constructs an object with existing reference
121  * @param pointedObj Existing reference to wrap
122  */
123  explicit SOPointer(T* pointedObj): _so_loader(), _pointedObj(pointedObj) {
124  if (_pointedObj == nullptr) {
125  THROW_IE_EXCEPTION << "Cannot create SOPointer<T, Loader> from nullptr";
126  }
127  }
128 
129  /**
130  * @brief The copy-like constructor, can create So Pointer that dereferenced into child type if T is derived of U
131  * @param that copied SOPointer object
132  */
133  template <class U, class W>
134  SOPointer(const SOPointer<U, W>& that)
135  : _so_loader(std::dynamic_pointer_cast<Loader>(that._so_loader)),
136  _pointedObj(std::dynamic_pointer_cast<T>(that._pointedObj)) {
137  if (_pointedObj == nullptr) {
138  THROW_IE_EXCEPTION << "Cannot create object from SOPointer<U, W> reference";
139  }
140  }
141 
142  /**
143  * @brief Standard pointer operator
144  * @return underlined interface with disabled Release method
145  */
146  details::NoReleaseOn<T>* operator->() const noexcept {
147  return reinterpret_cast<details::NoReleaseOn<T>*>(_pointedObj.get());
148  }
149 
150  /**
151  * @brief Standard dereference operator
152  * @return underlined interface with disabled Release method
153  */
154  details::NoReleaseOn<T>* operator*() const noexcept {
155  return this->operator->();
156  }
157 
158  explicit operator bool() const noexcept {
159  return (nullptr != _so_loader) && (nullptr != _pointedObj);
160  }
161 
162  friend bool operator==(std::nullptr_t, const SOPointer& ptr) noexcept {
163  return !ptr;
164  }
165  friend bool operator==(const SOPointer& ptr, std::nullptr_t) noexcept {
166  return !ptr;
167  }
168  friend bool operator!=(std::nullptr_t, const SOPointer& ptr) noexcept {
169  return static_cast<bool>(ptr);
170  }
171  friend bool operator!=(const SOPointer& ptr, std::nullptr_t) noexcept {
172  return static_cast<bool>(ptr);
173  }
174 
175  SOPointer& operator=(const SOPointer& pointer) noexcept {
176  _pointedObj = pointer._pointedObj;
177  _so_loader = pointer._so_loader;
178  return *this;
179  }
180 
181 protected:
182  /**
183  * @brief Gets a smart pointer to the DLL
184  */
185  std::shared_ptr<Loader> _so_loader;
186  /**
187  * @brief Gets a smart pointer to the custom object
188  */
189  std::shared_ptr<T> _pointedObj;
190  IE_SUPPRESS_DEPRECATED_END
191 };
192 
193 } // namespace details
194 
195 /**
196  * @brief Creates a special shared_pointer wrapper for the given type from a specific shared module
197  * @tparam T An type of object SOPointer can hold
198  * @param name Name of the shared library file
199  * @return A created object
200  */
201 template <class T>
202 inline std::shared_ptr<T> make_so_pointer(const file_name_t& name) = delete;
203 
204 } // 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
std::string name
Layer name.
Definition: ie_layers.h:42
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
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.
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:274
A header file for the main Inference Engine exception.