Claw  1.7.3
dynamic_library_traits_unix.hpp
Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2011 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien.jorge@gamned.org
00024 */
00030 #ifndef __CLAW_DYNAMIC_LIBRARY_TRAITS_UNIX_HPP__
00031 #define __CLAW_DYNAMIC_LIBRARY_TRAITS_UNIX_HPP__
00032 
00033 #include <string>
00034 #include <dlfcn.h>
00035 #include <claw/exception.hpp>
00036 
00037 namespace claw
00038 {
00043   class dynamic_library_traits_unix
00044   {
00045   public:
00047     typedef void* handle;
00048 
00049   public:
00050     /*------------------------------------------------------------------------*/
00056     static handle open( const std::string& name )
00057     {
00058       handle h = dlopen( name.c_str(), RTLD_LAZY );
00059 
00060       if ( !valid_handle(h) )
00061         throw claw::exception( dlerror() );
00062 
00063       return h;
00064     } // dynamic_library_traits_unix::open()
00065 
00066     /*------------------------------------------------------------------------*/
00072     static handle auto_open( const std::string& name )
00073     {
00074       handle h = dlopen( NULL, RTLD_LAZY );
00075 
00076       if ( !valid_handle(h) )
00077         throw claw::exception( dlerror() );
00078 
00079       return h;
00080     } // dynamic_library_traits_unix::auto_open()
00081 
00082     /*------------------------------------------------------------------------*/
00087     static void close( handle h )
00088     {
00089       dlclose(h);
00090     } // dynamic_library_traits_unix::close()
00091 
00092     /*------------------------------------------------------------------------*/
00098     template<class T>
00099     static T get_symbol( handle h, const std::string& name )
00100     {
00101       /* HACK : ISO standard doesn't allow to cast from a pointer to an object
00102          to a pointer to a function. */
00103       T result;
00104       *(void**)(&result) = dlsym( h, name.c_str() );
00105 
00106       return result;
00107     } // dynamic_library_traits_unix::get_symbol()
00108 
00109     /*------------------------------------------------------------------------*/
00115     static bool have_symbol( handle h, const std::string& name )
00116     {
00117       return dlsym( h, name.c_str() ) != NULL;
00118     } // dynamic_library_traits_unix::have_symbol()
00119 
00120     /*------------------------------------------------------------------------*/
00125     static bool valid_handle( handle h )
00126     {
00127       return h != NULL;
00128     } // dynamic_library_traits_unix::valid_handle()
00129 
00130   }; // class dynamic_library_traits_unix
00131 
00133   typedef dynamic_library_traits_unix dynamic_library_traits;
00134 
00135 } // namespace claw
00136 
00137 #endif // __CLAW_DYNAMIC_LIBRARY_TRAITS_UNIX_HPP__