00001 #ifndef __DYNAMIC_LIB_LOADER_CPP__
00002 #define __DYNAMIC_LIB_LOADER_CPP__
00003
00004 #include <string>
00005
00006 #include <loaders/dynamic_lib_loader.h>
00007 #include <utilities/exception.h>
00008 #include <utilities/nwindows.h>
00009
00010 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00011 #include <dlfcn.h>
00012 #endif
00013
00014 namespace nitro
00015 {
00016
00017 DynamicLibLoader::DynamicLibLoader( void )
00018 {
00019 try
00020 {
00021 Handle = NULL;
00022 }
00023 catch( nitro::exception e )
00024 {
00025 throw( nitro::exception( std::string( "DynamicLibLoader::DynamicLibLoader( void )::" ) + e.what() , e.code() ) );
00026 }
00027 catch( ... )
00028 {
00029 throw( nitro::exception( std::string( "DynamicLibLoader::DynamicLibLoader( void )::An error occured" ) , 1 ) );
00030 }
00031 }
00032
00033 DynamicLibLoader::DynamicLibLoader( const char * LibraryPath )
00034 {
00035 try
00036 {
00037 Handle = NULL;
00038
00039 LoadLibrary( LibraryPath );
00040 }
00041 catch( nitro::exception e )
00042 {
00043 throw( nitro::exception( std::string( "DynamicLibLoader::DynamicLibLoader( const char * LibraryPath )::" ) + e.what() , e.code() ) );
00044 }
00045 catch( ... )
00046 {
00047 throw( nitro::exception( std::string( "DynamicLibLoader::DynamicLibLoader( const char * LibraryPath )::An error occured" ) , 0) );
00048 }
00049 }
00050
00051 DynamicLibLoader::DynamicLibLoader( const std::string & LibraryPath )
00052 {
00053 try
00054 {
00055 Handle = NULL;
00056
00057 LoadLibrary( LibraryPath );
00058 }
00059 catch( nitro::exception e )
00060 {
00061 throw( nitro::exception( std::string( "DynamicLibLoader::DynamicLibLoader( const std::string & LibraryPath )::" ) + e.what() , e.code() ) );
00062 }
00063 catch( ... )
00064 {
00065 throw( nitro::exception( std::string( "DynamicLibLoader::DynamicLibLoader( const std::string & LibraryPath )::An error occured" ) , 0) );
00066 }
00067 }
00068
00069 void DynamicLibLoader::LoadLibrary( const char * LibraryPath )
00070 {
00071 try
00072 {
00073 Release();
00074
00075 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00076 Handle = ( void * ) new HMODULE;
00077
00078 *( ( HMODULE * )Handle ) = LoadLibraryA( LibraryPath );
00079
00080 if( *( ( HMODULE * )Handle ) == NULL )
00081 {
00082 throw( nitro::exception( std::string( "An error occured while loading library " ) + LibraryPath , 0 ) );
00083 }
00084 #endif
00085
00086 #if defined( NIX_PLARFORM ) || defined( CYGWIN_PLATFORM )
00087 Handle = dlopen( LibraryPath , RTLD_NOW | RTLD_GLOBAL );
00088
00089 if( Handle == NULL )
00090 {
00091 throw( nitro::exception( std::string( "An error occured while loading library " ) + LibraryPath , 0 ) );
00092 }
00093 #endif
00094 }
00095 catch( nitro::exception e )
00096 {
00097 throw( nitro::exception( std::string( "DynamicLibLoader::LoadLibrary( const char * LibraryPath )::" ) + e.what() , e.code() ) );
00098 }
00099 catch( ... )
00100 {
00101 throw( nitro::exception( std::string( "DynamicLibLoader::LoadLibrary( const char * LibraryPath )::An error occured" ) , 1 ) );
00102 }
00103 }
00104
00105 void DynamicLibLoader::LoadLibrary( const std::string & LibraryPath )
00106 {
00107 try
00108 {
00109 LoadLibrary( LibraryPath.c_str() );
00110 }
00111 catch( nitro::exception e )
00112 {
00113 throw( nitro::exception( std::string( "DynamicLibLoader::LoadLibrary( const std::string & LibraryPath )::" ) + e.what() , e.code() ) );
00114 }
00115 catch( ... )
00116 {
00117 throw( nitro::exception( std::string( "DynamicLibLoader::LoadLibrary( const std::string & LibraryPath )::An error occured" ) , 1 ) );
00118 }
00119 }
00120
00121 void * DynamicLibLoader::GetResource( const char * ResourceName )
00122 {
00123 try
00124 {
00125 if( Handle )
00126 {
00127 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00128 FARPROC Address( NULL );
00129
00130 Address = GetProcAddress( *( ( HMODULE * )Handle ) , ResourceName );
00131 #endif
00132
00133 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00134 void * Address( NULL );
00135
00136 Address = dlsym( Handle , ResourceName );
00137 #endif
00138
00139 if( Address == NULL )
00140 {
00141 throw( nitro::exception( std::string( "An error occured while getting library resource " ) + ResourceName , 1 ) );
00142 }
00143
00144 return( ( void * )Address );
00145 }
00146 else
00147 {
00148 throw( nitro::exception( std::string( "Library was not loaded" ) , 1 ) );
00149 }
00150 }
00151 catch( nitro::exception e )
00152 {
00153 throw( nitro::exception( std::string( "DynamicLibLoader::GetResource( const char * ResourceName )::" ) + e.what() , e.code() ) );
00154 }
00155 catch( ... )
00156 {
00157 throw( nitro::exception( std::string( "DynamicLibLoader::GetResource( const char * ResourceName )::An error occured" ) , 1 ) );
00158 }
00159 }
00160
00161 void * DynamicLibLoader::GetResource( const std::string & ResourceName )
00162 {
00163 try
00164 {
00165 return( GetResource( ResourceName.c_str() ) );
00166 }
00167 catch( nitro::exception e )
00168 {
00169 throw( nitro::exception( std::string( "DynamicLibLoader::GetResource( const std::string & ResourceName )::" ) + e.what() , e.code() ) );
00170 }
00171 catch( ... )
00172 {
00173 throw( nitro::exception( std::string( "DynamicLibLoader::GetResource( const std::string & ResourceName )::An error occured" ) , 1 ) );
00174 }
00175 }
00176
00177 bool DynamicLibLoader::ResourceExists( const char * ResourceName )
00178 {
00179 try
00180 {
00181 if( Handle )
00182 {
00183 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00184 FARPROC Address( NULL );
00185
00186 Address = GetProcAddress( *( ( HMODULE * )Handle ) , ResourceName );
00187 #endif
00188
00189 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00190 void * Address( NULL );
00191
00192 Address = dlsym( Handle , ResourceName );
00193 #endif
00194
00195 if( Address == NULL )
00196 {
00197 return( false );
00198 }
00199
00200 return( true );
00201 }
00202 else
00203 {
00204 throw( nitro::exception( std::string( "Library was not loaded" ) , 1 ) );
00205 }
00206 }
00207 catch( nitro::exception e )
00208 {
00209 throw( nitro::exception( std::string( "DynamicLibLoader::ResourceExists( const char * ResourceName )::" ) + e.what() , e.code() ) );
00210 }
00211 catch( ... )
00212 {
00213 throw( nitro::exception( std::string( "DynamicLibLoader::ResourceExists( const char * ResourceName )::An error occured" ) , 1 ) );
00214 }
00215 }
00216
00217 bool DynamicLibLoader::ResourceExists( const std::string & ResourceName )
00218 {
00219 try
00220 {
00221 return( ResourceExists( ResourceName.c_str() ) );
00222 }
00223 catch( nitro::exception e )
00224 {
00225 throw( nitro::exception( std::string( "DynamicLibLoader::ResourceExists( const std::string & ResourceName )::" ) + e.what() , e.code() ) );
00226 }
00227 catch( ... )
00228 {
00229 throw( nitro::exception( std::string( "DynamicLibLoader::ResourceExists( const std::string & ResourceName )::An error occured" ) , 1 ) );
00230 }
00231 }
00232
00233 void DynamicLibLoader::Release( void )
00234 {
00235 try
00236 {
00237 if( Handle )
00238 {
00239 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00240 FreeLibrary( * ( ( HMODULE * )Handle ) );
00241
00242 delete ( HMODULE * )Handle;
00243 #endif
00244
00245 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00246 dlclose( Handle );
00247 #endif
00248
00249 Handle = NULL;
00250 }
00251 }
00252 catch( nitro::exception e )
00253 {
00254 throw( nitro::exception( std::string( "DynamicLibLoader::Release( void )::" ) + e.what() , e.code() ) );
00255 }
00256 catch( ... )
00257 {
00258 throw( nitro::exception( std::string( "DynamicLibLoader::Release( void )::An error occured" ) , 1 ) );
00259 }
00260 }
00261
00262 DynamicLibLoader::~DynamicLibLoader()
00263 {
00264 try
00265 {
00266 Release();
00267 }
00268 catch( nitro::exception e )
00269 {
00270 }
00271 catch( ... )
00272 {
00273 }
00274 }
00275
00278
00279 BEGIN_TESTING_SECTION()
00280
00281 ENABLE_CLASS_TESTING( DynamicLibLoader )
00282
00283 CLASS_MEMBER_FUNCTION_TESTING_1( DynamicLibLoader , aliasLoadLibrary , tstLoadLibrary , const char * , void , NO_RET )
00284 CLASS_MEMBER_FUNCTION_TESTING_0( DynamicLibLoader , Release , tstRelease , void , NO_RET )
00285 CLASS_MEMBER_FUNCTION_TESTING_1( DynamicLibLoader , aliasGetResource , tstGetResource , const char * , void * , void * )
00286 CLASS_MEMBER_FUNCTION_TESTING_1( DynamicLibLoader , aliasResourceExists , tstResourceExists , const char * , bool , bool )
00287
00288 #ifdef ENABLE_AUTOTESTING
00289 NITRO_EXPORTING int tstFunc( void )
00290 {
00291 return( 12345 );
00292 }
00293 NITRO_EXPORTING int tstTestCase1( const char * ObjectName )
00294 {
00295 int ( * FuncPtr )( void );
00296
00297 FuncPtr = ( int ( * )( void ) )tstGetResource( ObjectName , "tstFunc" );
00298
00299 return( FuncPtr() );
00300 }
00301 #endif
00302
00303 END_TESTING_SECTION()
00304
00305 }
00306
00307 #endif