nndeploy C++ API  0.2.0
nndeploy C++ API
dlopen.h
Go to the documentation of this file.
1 #ifndef _NNDEPLOY_BASE_DLOPEN_H_
2 #define _NNDEPLOY_BASE_DLOPEN_H_
3 
4 #include "nndeploy/base/common.h"
6 #include "nndeploy/base/log.h"
7 #include "nndeploy/base/macro.h"
8 #include "nndeploy/base/object.h"
10 #include "nndeploy/base/param.h"
11 #include "nndeploy/base/status.h"
12 #include "nndeploy/base/string.h"
13 
14 #ifdef WIN32
15 #define NOMINMAX
16 #include <windows.h>
18 #include <libloaderapi.h>
19 #else
20 #include <dlfcn.h>
21 #endif
22 
23 namespace nndeploy {
24 namespace base {
25 
26 #define NNDEPLOY_DEFINE_FUNC_PTR(func) func##Func func = nullptr
27 
28 #ifdef WIN32
29 #define NNDEPLOY_LOAD_FUNCTION_PTR(handle, func_name) \
30  func_name = \
31  reinterpret_cast<func_name##Func>(GetProcAddress(handle, #func_name)); \
32  if (func_name == nullptr) { \
33  NNDEPLOY_LOGE("load func (%s) from (%s) failed!\n", #func_name, \
34  library_path.c_str()); \
35  return false; \
36  }
37 // load function ptr use dlopen and dlsym. if cann't find func_name, that will
38 // be ok.
39 #define NNDEPLOY_TRY_LOAD_FUNCTION_PTR(handle, func_name) \
40  func_name = \
41  reinterpret_cast<func_name##Func>(GetProcAddress(handle, #func_name)); \
42  if (func_name == nullptr) { \
43  NNDEPLOY_LOGE("load func (%s) from (%s) failed!\n", #func_name, \
44  library_path.c_str()); \
45  }
46 #else // WIN32
47 // load function ptr use dlopen and dlsym. if cann't find func_name, will return
48 // false.
49 #define NNDEPLOY_LOAD_FUNCTION_PTR(handle, func_name) \
50  if (is_pixel) { \
51  func_name = \
52  reinterpret_cast<func_name##Func>(loadOpenCLPointer(#func_name)); \
53  } else { \
54  func_name = reinterpret_cast<func_name##Func>(dlsym(handle, #func_name)); \
55  } \
56  if (func_name == nullptr) { \
57  NNDEPLOY_LOGE("load func (%s) from (%s) failed!\n", #func_name, \
58  library_path.c_str()); \
59  return false; \
60  }
61 
62 // load function ptr use dlopen and dlsym. if cann't find func_name, that will
63 // be ok.
64 #define NNDEPLOY_TRY_LOAD_FUNCTION_PTR(handle, func_name) \
65  if (is_pixel) { \
66  func_name = \
67  reinterpret_cast<func_name##Func>(loadOpenCLPointer(#func_name)); \
68  } else { \
69  func_name = reinterpret_cast<func_name##Func>(dlsym(handle, #func_name)); \
70  } \
71  if (func_name == nullptr) { \
72  NNDEPLOY_LOGE("load func (%s) from (%s) failed!\n", #func_name, \
73  library_path.c_str()); \
74  }
75 
76 #endif // end of WIN32
77 
78 struct Handle {
79 #ifdef WIN32
80  HMODULE handle_;
81 #else
82  void *handle_;
83 #endif
84 };
85 
86 extern NNDEPLOY_CC_API bool loadLibraryFromPath(const std::string &path,
87  bool update);
88 extern NNDEPLOY_CC_API bool freeLibrary(const std::string &path);
89 extern NNDEPLOY_CC_API Handle *getLibraryHandle(const std::string &path,
90  bool update);
91 
92 } // namespace base
93 } // namespace nndeploy
94 
95 #endif /* _NNDEPLOY_BASE_DLOPEN_H_ */
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
bool loadLibraryFromPath(const std::string &path, bool update)
bool freeLibrary(const std::string &path)
Handle * getLibraryHandle(const std::string &path, bool update)