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