HAL
runtime_library.cpp
Go to the documentation of this file.
2 
4 
5 namespace hal
6 {
8  {
9  m_handle = NULL;
10  }
11 
13  {
14  if (m_handle != NULL)
15  {
16  this->unload_library();
17  }
18  m_file_name.clear();
19  }
20 
21  std::string RuntimeLibrary::get_file_name() const
22  {
23  return m_file_name;
24  }
25 
26  bool RuntimeLibrary::load_library(const std::string& file_name)
27  {
28 #ifdef _WIN32
29  m_handle = LoadLibrary(file_name.c_str());
30 #else
31  m_handle = dlopen((file_name).c_str(), RTLD_GLOBAL | RTLD_LAZY);
32 #endif
33 
34  if (m_handle != NULL)
35  {
36  log_debug("core", "loaded library '{}'", file_name);
37  m_file_name = file_name;
38  return true;
39  }
40 
41  auto error_message = std::string("");
42 #ifdef _WIN32
43  error_message = "to be implemented for windows based operating systems.";
44 #else
45  error_message = dlerror();
46 #endif
47  log_error("core", "cannot load library '{}' (error: {}) -- did you properly link the plugin?", file_name, error_message);
48  return false;
49  }
50 
52  {
53  if (m_handle == NULL)
54  {
55  log_error("core", "library '{}' already unloaded", m_file_name);
56  return true;
57  }
58 #ifdef _WIN32
59  if (FreeLibrary(m_handle) == 0)
60  {
61  log_debug("core", "unloaded library '{}'", m_file_name);
62  m_handle = NULL;
63  return true;
64  }
65 #else
66  if (dlclose(m_handle) == 0)
67  {
68  log_debug("core", "unloaded library '{}'", m_file_name);
69  m_handle = NULL;
70  return true;
71  }
72 #endif
73 
74  log_error("core", "cannot unload library '{}'", m_file_name);
75  return false;
76  }
77 
78  lib_fn_ptr_t RuntimeLibrary::get_function(const std::string& function_name)
79  {
80  lib_fn_ptr_t fptr = NULL;
81 #ifdef _WIN32
82  fptr = GetProcAddress(m_handle, function_name.c_str());
83  if (!fptr)
84  {
85  log_error("core", "cannot load function '{}' from library '{}'", function_name, m_file_name);
86  return NULL;
87  }
88 #else
89  dlerror();
90  fptr = (lib_fn_ptr_t)dlsym(m_handle, function_name.c_str());
91  if (dlerror())
92  {
93  log_debug("core", "cannot load function '{}' from library '{}'", function_name, m_file_name);
94  return NULL;
95  }
96 #endif
97  return fptr;
98  }
99 } // namespace hal
std::string get_file_name() const
bool load_library(const std::string &file_name)
lib_fn_ptr_t get_function(const std::string &function_name)
#define log_error(channel,...)
Definition: log.h:78
#define log_debug(channel,...)
Definition: log.h:74
void(*)() lib_fn_ptr_t