34 #include <boost/filesystem.hpp>
36 #include "exceptions/WLibraryFetchFailed.h"
37 #include "exceptions/WLibraryLoadFailed.h"
38 #include "WSharedLib.h"
64 explicit data(
const std::string& path ):
66 m_hDLL( LoadLibrary( path.c_str() ) )
70 throw WLibraryLoadFailed( std::string(
"Could not load library \"" +
m_path +
"\" due to the error: " + errmsg() ) );
79 FreeLibrary( m_hDLL );
96 throw WLibraryFetchFailed( std::string(
"Could not fetch symbol \"" + name +
"\"" +
" due to the error: " + errmsg() ) );
125 return reinterpret_cast< void*
>(
findFunction( name ) );
133 static std::string errmsg()
137 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, GetLastError(), 0,
138 reinterpret_cast< LPTSTR
>( &lpMsgBuf ), 0, 0 );
142 msg.push_back( *p++ );
144 LocalFree( lpMsgBuf );
154 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
159 pthread_mutex_lock( &mutex );
163 pthread_mutex_unlock( &mutex );
188 assert( dlclose(
m_dl ) == 0 );
198 explicit data(
const std::string& path )
205 throw WLibraryLoadFailed( std::string(
"Could not load library \"" +
m_path +
"\" due to the error: " + dlerror() ) );
240 void*
findVariable(
const std::string& name,
bool suppressThrow =
false )
244 void* variable_ptr = dlsym(
m_dl, name.c_str() );
245 const char *err = dlerror();
246 if( !suppressThrow && err )
248 throw WLibraryFetchFailed( std::string(
"Could not fetch symbol \"" + name +
"\"" +
" due to the error: " + err ) );
268 m_data( new
data( lib.string() ) ),
274 m_data( new
data( rhs.m_data->m_path ) ),
275 m_libPath( rhs.m_libPath )
329 std::string relPath(
m_libPath.filename().string() );
Thrown whenever a symbol could not be fetched.
Thrown whenever a library could not been loaded.
This class loads shared libraries and provides function pointers.
static std::string getSystemSuffix()
Returns the suffix for libraries used on the system.
static std::string getSystemPrefix()
Returns the prefix used for libraries on the system.
static std::string getSystemLibPath()
Returns the default path for libraries on the current system.
data * m_data
internal data
boost::filesystem::path m_libPath
path to lib
virtual ~WSharedLib()
Destructor.
void * findVariable(const std::string &name) const
Find the specified symbol in the library.
func_ptr_type findFunction(const std::string &name) const
Find the specified function pointer in the library.
friend void swap(WSharedLib &lhs, WSharedLib &rhs)
Swap to shared libraries.
bool existsFunction(const std::string &name) const
Check whether the function exists.
void(* func_ptr_type)(void)
neutral function pointer type
WSharedLib(boost::filesystem::path lib)
Constructor.
std::string getLibraryName()
Returns the filename of the library without path.
WSharedLib & operator=(const WSharedLib &rhs)
Copy assignment for shared libraries.
Simple class holding an opened library.
void * findVariable(const std::string &name, bool suppressThrow=false)
Searches the lib for the specified symbol and returns it.
void * m_dl
Handle describing the loaded lib.
bool existsFunction(const std::string &name)
Check for existence of a given function pointer symbol.
const std::string m_path
Path of lib.
data(const std::string &path)
Constructor.
func_ptr_type findFunction(const std::string &name)
Searches the lib for the specified function symbol and returns it.