Claw 1.7.0
dynamic_library_traits_win32.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_WIN32_HPP__
00031 #define __CLAW_DYNAMIC_LIBRARY_TRAITS_WIN32_HPP__
00032 
00033 #include <string>
00034 //#include <cstdarg>
00035 //#include <windef.h>
00036 //#include <winbase.h>
00037 #include <windows.h>
00038 
00039 namespace claw
00040 {
00045   class dynamic_library_traits_win32
00046   {
00047   public:
00049     typedef HMODULE handle;
00050 
00051   public:
00052     /*------------------------------------------------------------------------*/
00058     static handle open( const std::string& name )
00059     {
00060       return LoadLibrary( name.c_str() );
00061     } // dynamic_library_traits_win32::open()
00062 
00063     /*------------------------------------------------------------------------*/
00069     static handle auto_open( const std::string& name )
00070     {
00071       return LoadLibrary( name.c_str() );
00072     } // dynamic_library_traits_win32::auto_open()
00073 
00074     /*------------------------------------------------------------------------*/
00079     static void close( handle h )
00080     {
00081       FreeLibrary(h);
00082     } // dynamic_library_traits_win32::close()
00083 
00084     /*------------------------------------------------------------------------*/
00090     template<class T>
00091     static T get_symbol( handle h, const std::string& name )
00092     {
00093       /* HACK : ISO standard doesn't allow to cast from a pointer to an object
00094          to a pointer to a function. */
00095       T result;
00096       *(FARPROC*)(&result) = GetProcAddress( h, name.c_str() );
00097 
00098       return result;
00099     } // dynamic_library_traits_win32::get_symbol()
00100 
00101     /*------------------------------------------------------------------------*/
00107     static bool have_symbol( handle h, const std::string& name )
00108     {
00109       return GetProcAddress( h, name.c_str() ) != NULL;
00110     } // dynamic_library_traits_win32::have_symbol()
00111 
00112     /*------------------------------------------------------------------------*/
00117     static bool valid_handle( handle h )
00118     {
00119       return h != NULL;
00120     } // dynamic_library_traits_win32::valid_handle()
00121 
00122   }; // class dynamic_library_traits_win32
00123 
00124   typedef dynamic_library_traits_win32 dynamic_library_traits;
00125 } // namespace claw
00126 
00127 #endif // __CLAW_DYNAMIC_LIBRARY_TRAITS_WIN32_HPP__