library.hh 1.44 KB
Newer Older
1
2
3
#ifndef RADIX_RADIXDL_LIBRARY_HH_
#define RADIX_RADIXDL_LIBRARY_HH_

4
#if defined(_WIN32)
5
6
7
8
9
10
#include <windows.h>
#else
#include <dlfcn.h>
#endif

#include <string>
11
#include <vector>
12
13

#include "radixdl/function.hh"
14
#include "radixcore/visibility.hh"
15

16
17
18
19
namespace radix
{
class RADIX_PUBLIC DynamicLibrary
{
20
21
22
 public:
  DynamicLibrary();
  virtual ~DynamicLibrary();
23

24
25
  bool is_open() const;
  bool function_exists(const std::string &);
26

27
28
  bool open();
  bool close();
29

30
31
  template <typename return_type = void, typename... ARGS>
  DLFunction<return_type, ARGS...> function(const std::string &);
32

33
34
35
  void set_lib_name(const std::string &);
  void addSearchDir(const std::string &);
  void removeSearchDir(const std::string &);
36

37
38
39
40
41
 private:
  std::vector<std::string> mDirs;
  std::string mFileName;
  static const std::vector<std::string> m_lib_prefixes;
  static const std::vector<std::string> m_lib_suffixes;
42

43
44
#if defined(_WIN32)
  HINSTANCE mHandle;
45
#else
46
  void *mHandle;
47
#endif
48
};  // class DLibrary
49

50
template <typename return_type, typename... ARGS>
51
DLFunction<return_type, ARGS...> DynamicLibrary::function(
52
53
54
55
    const std::string &func)
{
  if (mHandle == NULL)
  {
56
57
58
59
60
    return DLFunction<return_type, ARGS...>();
  }
#if defined(_WIN32)
  return DLFunction<return_type, ARGS...>(
      GetProcAddress(mHandle, func.c_str()));
61
#else
62
  return DLFunction<return_type, ARGS...>(dlsym(mHandle, func.c_str()));
63
64
65
#endif
}

66
}  // namespace radix
67
68

#endif /* RADIX_RADIXDL_LIBRARY_HH_ */