lin_shared_object_loader.h
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 POSIX compatible loader for a shared object
7  *
8  * @file lin_shared_object_loader.h
9  */
10 #pragma once
11 
12 #include <dlfcn.h>
13 
14 #include "ie_api.h"
15 #include "details/ie_exception.hpp"
17 
18 namespace InferenceEngine {
19 namespace details {
20 
21 /**
22  * @brief This class provides an OS shared module abstraction
23  */
24 class SharedObjectLoader {
25 private:
26  void* shared_object = nullptr;
27 
28 public:
29  /**
30  * @brief Loads a library with the name specified. The library is loaded according to
31  * the POSIX rules for dlopen
32  * @param pluginName Full or relative path to the library
33  */
34  explicit SharedObjectLoader(const char* pluginName) {
35  shared_object = dlopen(pluginName, RTLD_LAZY);
36 
37  if (shared_object == nullptr)
38  THROW_IE_EXCEPTION << "Cannot load library '" << pluginName << "': " << dlerror();
39  }
40 
41 #ifdef ENABLE_UNICODE_PATH_SUPPORT
42  /**
43  * @brief Loads a library with the name specified. The library is loaded according to
44  * the POSIX rules for dlopen
45  * @param pluginName Full or relative path to the library
46  */
47  explicit SharedObjectLoader(const wchar_t* pluginName) : SharedObjectLoader(wStringtoMBCSstringChar(pluginName).c_str()) {
48  }
49 
50 #endif // ENABLE_UNICODE_PATH_SUPPORT
51 
52  ~SharedObjectLoader() noexcept(false) {
53  if (0 != dlclose(shared_object)) {
54  THROW_IE_EXCEPTION << "dlclose failed: " << dlerror();
55  }
56  }
57 
58  /**
59  * @brief Searches for a function symbol in the loaded module
60  * @param symbolName Name of the function to find
61  * @return A pointer to the function if found
62  * @throws InferenceEngineException if the function is not found
63  */
64  void* get_symbol(const char* symbolName) const {
65  void* procAddr = nullptr;
66 
67  procAddr = dlsym(shared_object, symbolName);
68  if (procAddr == nullptr)
69  THROW_IE_EXCEPTION << "dlSym cannot locate method '" << symbolName << "': " << dlerror();
70  return procAddr;
71  }
72 };
73 
74 } // namespace details
75 } // 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
The macro defines a symbol import/export mechanism essential for Microsoft Windows(R) OS...
This is a header file with functions related to filesystem operations.
A header file for the main Inference Engine exception.