Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / include / details / os / lin_shared_object_loader.h
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)
36             THROW_IE_EXCEPTION << "Cannot load library '" << pluginName << "': " << dlerror();
37     }
38     ~SharedObjectLoader() noexcept(false) {
39         if (0 != dlclose(shared_object)) {
40             THROW_IE_EXCEPTION << "dlclose failed: " << dlerror();
41         }
42     }
43
44     /**
45      * @brief Searches for a function symbol in the loaded module
46      * @param symbolName Name of the function to find
47      * @return A pointer to the function if found
48      * @throws InferenceEngineException if the function is not found
49      */
50     void *get_symbol(const char* symbolName) const {
51         void * procAddr = nullptr;
52
53         procAddr = dlsym(shared_object, symbolName);
54         if (procAddr == nullptr)
55             THROW_IE_EXCEPTION << "dlSym cannot locate method '" << symbolName << "': " << dlerror();
56         return procAddr;
57     }
58 };
59
60 }  // namespace details
61 }  // namespace InferenceEngine