Belle II Software  release-05-01-25
xsi_shared_lib.h
1 /*
2  * xsi_shared_library.h
3  * Copyright (c) 2012, Xilinx, Inc. All Rights Reserved.
4  * Class for loading shared libraries generated by xelab
5  */
6 
7 #ifndef _XSI_SHARED_LIB_H
8 #define _XSI_SHARED_LIB_H
9 
10 #if defined(_WIN32)
11 # include <windows.h>
12 #else
13 # include <dlfcn.h>
14 #endif
15 
16 #include <string>
17 #include <stdexcept>
18 
19 namespace Xsi {
20 
21  class SharedLibrary {
22  public:
23 #if defined(_WIN32)
24  typedef HINSTANCE handle_type;
25 #else
26  typedef void* handle_type;
27 #endif
28  typedef void* symbol_type;
29 
30  SharedLibrary() : _lib(0), _retain(false) { }
31  ~SharedLibrary() { unload(); }
32  operator bool() const { return (_lib != 0); }
33  bool loaded() const { return (_lib != 0); }
34  handle_type handle() const { return _lib; }
35  const std::string& path() const { return _path; }
36  const std::string& error() const { return _err; }
37  bool load(const std::string& path)
38  {
39  unload();
40  // reset the retain flag
41  _retain = false;
42 
43  if (path.empty()) {
44  _err = "Failed to load shared library. "
45  "Path of the shared library is not specified.";
46  return false;
47  }
48  std::string msg;
49  bool ok = load_impl(path, msg);
50  if (ok) {
51  _path = path;
52  _err.clear();
53  } else {
54  _err = "Failed to load shared library \"" + path +
55  "\". " + msg;
56  }
57  return ok;
58  }
59 
60  bool load_impl(const std::string& path, std::string& errmsg)
61  {
62  bool ok = true;
63 #if defined(_WIN32)
64  SetLastError(0);
65  _lib = LoadLibrary(path.c_str());
66  if (_lib == 0) {
67  translate_error_message(GetLastError(), errmsg);
68  ok = false;
69  }
70 #else
71  _lib = dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
72  char* err = dlerror();
73  if (err != NULL) {
74  errmsg = err;
75  ok = false;
76  }
77 #endif
78  return ok;
79  }
80 
81  void unload()
82  {
83  if (_lib) {
84  if (!_retain) {
85 #if defined(_WIN32)
86  FreeLibrary(_lib);
87 #else
88  dlclose(_lib);
89 #endif
90  }
91  _lib = 0;
92  }
93  _err.clear();
94  }
95 
96  void retain()
97  {
98  _retain = true;
99  }
100 
101  bool getsymbol(const std::string& name, symbol_type& sym)
102  {
103  std::string msg;
104  bool ok = true;
105 
106  if (_lib == 0) {
107  msg = "The shared library is not loaded.";
108  ok = false;
109  } else {
110 
111 #if defined(_WIN32)
112  sym = (void*) GetProcAddress(_lib, name.c_str());
113  if (sym == NULL) {
114  translate_error_message(GetLastError(), msg);
115  ok = false;
116  }
117 #else
118  dlerror(); // clear error
119  sym = (void*) dlsym(_lib, name.c_str());
120  char* err = dlerror();
121  if (err != NULL) {
122  msg = err;
123  ok = false;
124  }
125 #endif
126  }
127 
128  if (ok) {
129  _err.clear();
130  } else {
131  _err = "Failed to obtain symbol \"" + name +
132  "\" from shared library. " + msg;
133  }
134 
135  return ok;
136  }
137 
138  symbol_type getfunction(const std::string& name)
139  {
140  symbol_type sym = NULL;
141  return getsymbol(name, sym) ? sym : NULL;
142  }
143 
144  private:
145  // shared library is non-copyable
147  const SharedLibrary& operator=(const SharedLibrary&);
148 
149 #if defined(_WIN32)
150  static void translate_error_message(DWORD errid, std::string& msg)
151  {
152  LPVOID bufptr;
153  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
154  FORMAT_MESSAGE_FROM_SYSTEM |
155  FORMAT_MESSAGE_IGNORE_INSERTS,
156  NULL,
157  errid,
158  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
159  (LPTSTR) &bufptr,
160  0,
161  NULL);
162  msg.clear();
163  if (bufptr) {
164  msg = reinterpret_cast<char*>(bufptr);
165  }
166  LocalFree(bufptr);
167  }
168 
169  static const std::string& library_suffix()
170  {
171  static const std::string s = ".dll";
172  return s;
173  }
174 #else
175  static const std::string& library_suffix()
176  {
177  static const std::string s = ".so";
178  return s;
179  }
180 #endif
181 
182  handle_type _lib;
183  std::string _path;
184  std::string _err;
185  bool _retain;
186  };
187 
188 } // namespace Xsi
189 
190 #endif // _XSI_SHARED_LIB_H
191 
192 // 67d7842dbbe25473c3c32b93c0da8047785f30d78e8a024de1b57352245f9689
Xsi::SharedLibrary
Definition: xsi_shared_lib.h:21