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