RTXI  3.0.0
The Real-Time eXperiment Interface Reference Manual
dlplugin.hpp
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah,
4  Will Cornell Medical College
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #ifndef DLLPLUGIN_H
22 #define DLLPLUGIN_H
23 
24 #include <algorithm>
25 #include <mutex>
26 #include <string>
27 #include <vector>
28 
29 #include <dlfcn.h>
30 
32 
36 namespace DLL
37 {
38 
39 typedef struct dll_info
40 {
41  std::string library_name;
42  void* handle;
43  bool operator==(const dll_info& rhs) const
44  {
45  return this->library_name == rhs.library_name;
46  }
47  bool operator!=(const dll_info& rhs) const { return !operator==(rhs); }
49 
50 class Loader
51 {
52 public:
53  Loader() = default;
54  Loader(const Loader&) = delete;
55  Loader(Loader&&) = delete;
56  Loader& operator=(const Loader&) = delete;
57  Loader& operator=(Loader&&) = delete;
58  ~Loader();
59 
68  int load(const char* library);
69 
75  void unload(const char* library);
76 
85  template<typename T>
86  T dlsym(const char* library, const char* symbol) // NOLINT
87  {
88  const dll_info temp = {std::string(library), nullptr};
89  auto handle_loc = std::find(
90  this->loaded_plugins.begin(), this->loaded_plugins.end(), temp);
91  if (handle_loc == this->loaded_plugins.end()) {
92  return nullptr;
93  }
94  return reinterpret_cast<T>(::dlsym(handle_loc->handle, symbol)); // NOLINT
95  }
96 
100  void unloadAll();
101 
102 private:
103  // static const u_int32_t MAGIC_NUMBER = 0xCA24CB3F;
104  // u_int32_t magic_number;
105  // std::string library;
106  std::vector<dll_info> loaded_plugins;
107  std::mutex m_dll_mutex;
108 }; // class Loader
109 
110 } // namespace DLL
111 
112 #endif // PLUGIN_H
Loader(Loader &&)=delete
Loader & operator=(const Loader &)=delete
Loader & operator=(Loader &&)=delete
void unloadAll()
Definition: dlplugin.cpp:65
T dlsym(const char *library, const char *symbol)
Definition: dlplugin.hpp:86
void unload(const char *library)
Definition: dlplugin.cpp:52
Loader()=default
int load(const char *library)
Definition: dlplugin.cpp:30
Loader(const Loader &)=delete
Classes associated with the loading/unloading of binaries at run-time.
Definition: dlplugin.hpp:37
struct DLL::dll_info dll_info
bool operator!=(const dll_info &rhs) const
Definition: dlplugin.hpp:47
void * handle
Definition: dlplugin.hpp:42
std::string library_name
Definition: dlplugin.hpp:41
bool operator==(const dll_info &rhs) const
Definition: dlplugin.hpp:43