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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef _VPR_LIBRARY_DYLD_H_
00043 #define _VPR_LIBRARY_DYLD_H_
00044
00045 #include <vpr/vprConfig.h>
00046
00047 #include <stdlib.h>
00048 #include <string>
00049
00050 #include <vpr/Util/Assert.h>
00051 #include <vpr/Util/ReturnStatus.h>
00052
00053 #if defined (__GNUC__) && __GNUC__ > 3
00054 #define dl_restrict __restrict
00055 #else
00056 #define dl_restrict
00057 #endif
00058
00059
00060 namespace vpr
00061 {
00062
00073 class LibraryDYLD
00074 {
00075 public:
00085 LibraryDYLD(const std::string& name)
00086 : mName(name)
00087 , mLibrary(NULL)
00088 {
00089 ;
00090 }
00091
00101 LibraryDYLD()
00102 : mName("")
00103 , mLibrary(NULL)
00104 {
00105 ;
00106 }
00107
00111 LibraryDYLD(const LibraryDYLD& lib)
00112 : mName("")
00113 , mLibrary(NULL)
00114 {
00115 copy(lib);
00116 }
00117
00121 ~LibraryDYLD()
00122 {
00123 if ( NULL != mLibrary )
00124 {
00125 unload();
00126 }
00127 }
00128
00132 LibraryDYLD& operator=(const LibraryDYLD& lib)
00133 {
00134 copy(lib);
00135 return *this;
00136 }
00137
00141 const std::string& getName() const
00142 {
00143 return mName;
00144 }
00145
00153 vpr::ReturnStatus load();
00154
00163 vpr::ReturnStatus unload();
00164
00171 bool isLoaded() const
00172 {
00173 return mLibrary != NULL;
00174 }
00175
00177
00195 void* findSymbol(const char* symbolName)
00196 {
00197
00198
00199 if ( NULL == mLibrary )
00200 {
00201 load();
00202 vprASSERT(NULL != mLibrary && "Could not load any library");
00203 }
00204
00205 return internalDlsym(mLibrary, symbolName);
00206 }
00207
00208 void* findSymbol(const std::string& symbolName)
00209 {
00210 return findSymbol(symbolName.c_str());
00211 }
00213
00215
00219 static void* findSymbolAndLibrary(const char* symbolName, LibraryDYLD& lib);
00220
00221 static void* findSymbolAndLibrary(const std::string& symbolName,
00222 LibraryDYLD& lib)
00223 {
00224 return findSymbolAndLibrary(symbolName.c_str(), lib);
00225 }
00227
00228 protected:
00232 void copy(const LibraryDYLD& lib)
00233 {
00234 this->mName = lib.mName;
00235 this->mLibrary = lib.mLibrary;
00236 }
00237
00238 void* internalDlopen(const char* filename, int flag);
00239
00240 const char* internalDlerror();
00241
00242 void* internalDlsym(void* dl_restrict handle,
00243 const char* dl_restrict symbol);
00244
00245 int internalDlclose(void* handle);
00246
00247 private:
00248 std::string mName;
00249 void* mLibrary;
00250 };
00251
00252 }
00253
00254
00255 #endif