1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
6 * @brief This is a wrapper class for handling plugin instantiation and releasing resources
7 * @file ie_so_pointer.hpp
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"
20 namespace InferenceEngine {
24 * @brief This class is a C++ helper to load a symbol from a library and create its instance
26 template<class Loader>
29 std::shared_ptr<Loader> _so_loader;
33 * @brief The main constructor
34 * @param loader Library to load from
36 explicit SymbolLoader(std::shared_ptr<Loader> loader) : _so_loader(loader) {}
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
44 T* instantiateSymbol(const std::string& name) const {
45 T* instance = nullptr;
47 StatusCode sts = bind_function<StatusCode(T*&, ResponseDesc*)>(name)(instance, &desc);
49 THROW_IE_EXCEPTION << desc.msg;
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
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())));
67 * @brief This class is a trait class that provides a creator with a function name corresponding to the templated class parameter
70 class SOCreatorTrait {};
73 * @brief This class instantiate object using shared library
75 template <class T, class Loader = SharedObjectLoader>
77 template <class U, class W> friend class SOPointer;
80 * @brief Default constructor
82 SOPointer() = default;
85 * @brief The main constructor
86 * @param name Name of a shared library file
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))) {
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
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)) {
105 * @brief Standard pointer operator
106 * @return underlined interface with disabled Release method
108 details::NoReleaseOn<T>* operator->() const noexcept {
109 return reinterpret_cast<details::NoReleaseOn<T>*>(_pointedObj.get());
113 * @brief Standard dereference operator
114 * @return underlined interface with disabled Release method
116 details::NoReleaseOn<T>* operator*() const noexcept {
117 return this->operator->();
120 explicit operator bool() const noexcept {
121 return (nullptr != _so_loader) && (nullptr != _pointedObj);
124 friend bool operator == (std::nullptr_t, const SOPointer& ptr) noexcept {
127 friend bool operator == (const SOPointer& ptr, std::nullptr_t) noexcept {
130 friend bool operator != (std::nullptr_t, const SOPointer& ptr) noexcept {
131 return static_cast<bool>(ptr);
133 friend bool operator != (const SOPointer& ptr, std::nullptr_t) noexcept {
134 return static_cast<bool>(ptr);
137 SOPointer& operator=(const SOPointer & pointer) noexcept {
138 _pointedObj = pointer._pointedObj;
139 _so_loader = pointer._so_loader;
145 * @brief Gets a smart pointer to the DLL
147 std::shared_ptr<Loader> _so_loader;
149 * @brief Gets a smart pointer to the custom object
151 std::shared_ptr<T> _pointedObj;
154 } // namespace details
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
163 inline std::shared_ptr<T> make_so_pointer(const file_name_t & name) = delete;
165 } // namespace InferenceEngine