PCMDynLoad.cpp
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 #include "../include/PCMDynLoad.h"
00030 
00031 
00032 
00033 
00034 
00035 #include <dlfcn.h>
00036 
00037 namespace ParCompMark
00038 {
00039 
00040   
00041   
00042   
00043 
00044   DynLoad::DynLoad(const std::string & libName)
00045         
00046         
00047         
00048   {
00049         mLibraryName = libName;
00050         load();
00051   }
00052 
00053  
00054 
00055   DynLoad::~DynLoad()
00056   {
00057         if(mHandle)
00058         {
00059          unload();
00060          mHandle = 0;
00061         }
00062   }
00063 
00064  
00065 
00066   
00067   
00068   
00069 
00070   bool DynLoad::hasFunction(const std::string & funcName) const
00071   {
00072         Assert(mHandle, INVALID_OPERATION_ERROR, "DynLoad::hasFunction()");
00073         void *f = dlsym(mHandle, funcName.c_str());
00074 
00075         return f != 0;
00076   }
00077 
00078  
00079 
00080   void *DynLoad::getFunction(const std::string & funcName) const
00081   {
00082         Assert(mHandle, INVALID_OPERATION_ERROR, "DynLoad::getFunction()");
00083 
00084         char *m;
00085 
00086         void *f = dlsym(mHandle, funcName.c_str());
00087 
00088         if(!f)
00089         {
00090          m = dlerror();
00091          if(m)
00092                 Except(INTERNAL_ERROR, "DynLoad::getFunction()", m);
00093         }
00094 
00095         return f;
00096 
00097   }
00098 
00099  
00100 
00101   void DynLoad::load()
00102   {
00103         if((mHandle = dlopen(mLibraryName.c_str(), RTLD_LAZY)) == NULL)
00104         {
00105          Except(INTERNAL_ERROR, "DynLoad::load()", dlerror());
00106         }
00107 
00108         Logger::getInstance()->log(Logger::NOTICE, mLibraryName + " loaded.");
00109   }
00110 
00111  
00112 
00113   void DynLoad::unload()
00114   {
00115 
00116         if(dlclose(mHandle) != 0)
00117         {
00118          Except(INTERNAL_ERROR, "DynLoad::unload()", dlerror());
00119         }
00120 
00121         Logger::getInstance()->log(Logger::NOTICE, mLibraryName + " unloaded.");
00122   }
00123 
00124  
00125 
00126 }