dynamic_library_traits_unix.hpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#ifndef __CLAW_DYNAMIC_LIBRARY_TRAITS_UNIX_HPP__
#define __CLAW_DYNAMIC_LIBRARY_TRAITS_UNIX_HPP__
#include <string>
#include <dlfcn.h>
#include <claw/exception.hpp>
namespace claw
{
class dynamic_library_traits_unix
{
public:
typedef void* handle;
public:
/*------------------------------------------------------------------------*/
static handle open( const std::string& name )
{
handle h = dlopen( name.c_str(), RTLD_LAZY );
if ( !valid_handle(h) )
throw claw::exception( dlerror() );
return h;
} // dynamic_library_traits_unix::open()
/*------------------------------------------------------------------------*/
static handle auto_open( const std::string& name )
{
handle h = dlopen( NULL, RTLD_LAZY );
if ( !valid_handle(h) )
throw claw::exception( dlerror() );
return h;
} // dynamic_library_traits_unix::auto_open()
/*------------------------------------------------------------------------*/
static void close( handle h )
{
dlclose(h);
} // dynamic_library_traits_unix::close()
/*------------------------------------------------------------------------*/
template<class T>
static T get_symbol( handle h, const std::string& name )
{
/* HACK : ISO standard doesn't allow to cast from a pointer to an object
to a pointer to a function. */
T result;
*(void**)(&result) = dlsym( h, name.c_str() );
return result;
} // dynamic_library_traits_unix::get_symbol()
/*------------------------------------------------------------------------*/
static bool have_symbol( handle h, const std::string& name )
{
return dlsym( h, name.c_str() ) != NULL;
} // dynamic_library_traits_unix::have_symbol()
/*------------------------------------------------------------------------*/
static bool valid_handle( handle h )
{
return h != NULL;
} // dynamic_library_traits_unix::valid_handle()
}; // class dynamic_library_traits_unix
typedef dynamic_library_traits_unix dynamic_library_traits;
} // namespace claw
#endif // __CLAW_DYNAMIC_LIBRARY_TRAITS_UNIX_HPP__
claw::dynamic_library_traits_unix::valid_handle
static bool valid_handle(handle h)
Tell if an handle is a valid library handle.
Definition: dynamic_library_traits_unix.hpp:125
claw::dynamic_library_traits_unix::auto_open
static handle auto_open(const std::string &name)
Open the current program.
Definition: dynamic_library_traits_unix.hpp:72
claw::dynamic_library_traits
dynamic_library_traits_unix dynamic_library_traits
The traits to access the dynamic libraries in Unix system.
Definition: dynamic_library_traits_unix.hpp:133
claw::dynamic_library_traits_unix::close
static void close(handle h)
Close a library.
Definition: dynamic_library_traits_unix.hpp:87
claw::exception
A simple class to use as exception with string message.
Definition: exception.hpp:42
claw::dynamic_library_traits_unix::handle
void * handle
Type of the system handle to the library.
Definition: dynamic_library_traits_unix.hpp:47
exception.hpp
A simple class to use as exception with string message.
claw::dynamic_library_traits_unix::have_symbol
static bool have_symbol(handle h, const std::string &name)
Tell if a symbol is in the library.
Definition: dynamic_library_traits_unix.hpp:115
claw::dynamic_library_traits_unix::get_symbol
static T get_symbol(handle h, const std::string &name)
Get a symbol from a library.
Definition: dynamic_library_traits_unix.hpp:99
claw
This is the main namespace.
Definition: algorithm.hpp:33
claw::dynamic_library_traits_unix
Unix interface for using dynamic libraries.
Definition: dynamic_library_traits_unix.hpp:43
claw::dynamic_library_traits_unix::open
static handle open(const std::string &name)
Open a library.
Definition: dynamic_library_traits_unix.hpp:56