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