HMSBEAGLE  1.0.0
libhmsbeagle/plugin/UnixSharedLibrary.h
00001 
00008 #ifndef __UNIXSHAREDLIBRARY_H__
00009 #define __UNIXSHAREDLIBRARY_H__
00010 
00011 #ifdef HAVE_CONFIG_H
00012 #include "libhmsbeagle/config.h"
00013 #endif
00014 
00015 #include "libhmsbeagle/plugin/SharedLibrary.h"
00016 
00017 // only use the standard unix library interface if we don't have libtool libraries
00018 #ifndef HAVE_LIBLTDL
00019 
00020 #include <dlfcn.h>
00021 #include <string>
00022 
00023 namespace beagle {
00024 namespace plugin {
00025 
00026 class UnixSharedLibrary : public SharedLibrary
00027 {
00028   public:
00029     UnixSharedLibrary(const char* name);
00030     ~UnixSharedLibrary();
00031 
00032     void* findSymbol(const char* name);
00033 
00034   private:
00035     void* m_handle;
00036 };
00037 
00038 UnixSharedLibrary::UnixSharedLibrary(const char* name)
00039     : m_handle(0)
00040 {
00041     std::string libname = "lib";
00042     libname += name;
00043 #ifdef DLS_MACOS
00044     libname += ".dylib";
00045 #else
00046     libname += ".so";
00047 #endif
00048     m_handle = dlopen(libname.c_str(),RTLD_NOW|RTLD_GLOBAL);
00049     if (m_handle == 0)
00050     {
00051     const char* s = dlerror();
00052     throw SharedLibraryException(s?s:"Exact Error Not Reported");
00053     }
00054 }
00055 UnixSharedLibrary::~UnixSharedLibrary() { dlclose(m_handle); }
00056 
00057 void* UnixSharedLibrary::findSymbol(const char* name)
00058 {
00059     void* sym = dlsym(m_handle,name);
00060     if (sym == 0)
00061     throw SharedLibraryException("Symbol Not Found");
00062     else
00063     return sym;
00064 }
00065 
00066 } // namespace plugin
00067 } // namespace beagle
00068 
00069 #endif  // HAVE_LIBLTDL
00070 
00071 #endif  // __UNIXSHAREDLIBRARY_H__
00072