dso.cpp

00001 
00002 /* Copyright (c) 2009, Cedric Stalder <cedric.stalder@gmail.com> 
00003  *               2009, Stefan Eilemann <eile@equalizergraphics.com> 
00004  *
00005  * This library is free software; you can redistribute it and/or modify it under
00006  * the terms of the GNU Lesser General Public License version 2.1 as published
00007  * by the Free Software Foundation.
00008  *  
00009  * This library is distributed in the hope that it will be useful, but WITHOUT
00010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00012  * details.
00013  * 
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this library; if not, write to the Free Software Foundation, Inc.,
00016  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00017  */
00018 
00019 #include "dso.h"
00020 
00021 #include "debug.h"
00022 #include "log.h"
00023 
00024 #ifdef WIN32
00025 #  define EQ_DL_ERROR getErrorString( GetLastError( ))
00026 #else
00027 #  include <dlfcn.h>
00028 #  define EQ_DL_ERROR dlerror()
00029 #endif
00030 
00031 namespace eq
00032 {
00033 namespace base
00034 {
00035 
00036 bool DSO::open( const std::string& fileName )
00037 {
00038     if( _dso )
00039     {
00040         EQWARN << "DSO already open, close it first" << std::endl;
00041         return false;
00042     }
00043 
00044     if( fileName.empty( ))
00045     {
00046 #ifdef WIN32
00047         _dso = GetModuleHandle( "Equalizer.dll" );
00048         EQASSERT( _dso );
00049 #else
00050         _dso = RTLD_DEFAULT;
00051 #endif
00052     }
00053     else
00054     {
00055 #ifdef WIN32
00056         _dso = LoadLibrary( fileName.c_str() );
00057 #else
00058         _dso = dlopen( fileName.c_str(), RTLD_LAZY | RTLD_LOCAL );
00059 #endif
00060         if( !_dso )
00061         {
00062             EQWARN << "Can't open library: " << EQ_DL_ERROR << std::endl;
00063             return false;
00064         }
00065     }
00066 
00067     return true;
00068 }
00069 
00070 void DSO::close()
00071 {
00072     if( !_dso )
00073         return;
00074 
00075 #ifdef WIN32
00076     if( _dso != GetModuleHandle( 0 ))
00077         FreeLibrary( _dso ) ;
00078 #else
00079     if( _dso != RTLD_DEFAULT )
00080         dlclose ( _dso );
00081 #endif
00082 
00083     _dso = 0;
00084 }
00085 
00086 void* DSO::getFunctionPointer( const std::string& name )
00087 {
00088 #ifdef WIN32
00089     return GetProcAddress( _dso, name.c_str() );
00090 #else
00091     return dlsym( _dso, name.c_str() );
00092 #endif
00093 }
00094 
00095 }
00096 }
Generated on Mon Aug 10 18:58:32 2009 for Equalizer 0.9 by  doxygen 1.5.8