HMSBEAGLE  1.0.0
UnixSharedLibrary.h
1 
8 #ifndef __UNIXSHAREDLIBRARY_H__
9 #define __UNIXSHAREDLIBRARY_H__
10 
11 #ifdef HAVE_CONFIG_H
12 #include "libhmsbeagle/config.h"
13 #endif
14 
15 #include "libhmsbeagle/plugin/SharedLibrary.h"
16 
17 // only use the standard unix library interface if we don't have libtool libraries
18 #ifndef HAVE_LIBLTDL
19 
20 #include <dlfcn.h>
21 #include <string>
22 
23 namespace beagle {
24 namespace plugin {
25 
27 {
28  public:
29  UnixSharedLibrary(const char* name);
31 
32  void* findSymbol(const char* name);
33 
34  private:
35  void* m_handle;
36 };
37 
38 UnixSharedLibrary::UnixSharedLibrary(const char* name)
39  : m_handle(0)
40 {
41  std::string libname = "lib";
42  libname += name;
43 #ifdef DLS_MACOS
44  libname += ".dylib";
45 #else
46  libname += ".so";
47 #endif
48  m_handle = dlopen(libname.c_str(),RTLD_NOW|RTLD_GLOBAL);
49  if (m_handle == 0)
50  {
51  const char* s = dlerror();
52  throw SharedLibraryException(s?s:"Exact Error Not Reported");
53  }
54 }
55 UnixSharedLibrary::~UnixSharedLibrary() { dlclose(m_handle); }
56 
57 void* UnixSharedLibrary::findSymbol(const char* name)
58 {
59  void* sym = dlsym(m_handle,name);
60  if (sym == 0)
61  throw SharedLibraryException("Symbol Not Found");
62  else
63  return sym;
64 }
65 
66 } // namespace plugin
67 } // namespace beagle
68 
69 #endif // HAVE_LIBLTDL
70 
71 #endif // __UNIXSHAREDLIBRARY_H__
72