~ubuntu-branches/ubuntu/oneiric/libclaw/oneiric

« back to all changes in this revision

Viewing changes to claw/dynamic_library_traits_win32.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge
  • Date: 2008-05-17 15:36:57 UTC
  • Revision ID: james.westby@ubuntu.com-20080517153657-0b1204j754ykoz48
Tags: upstream-1.5.2b
ImportĀ upstreamĀ versionĀ 1.5.2b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  CLAW - a C++ Library Absolutely Wonderful
 
3
 
 
4
  CLAW is a free library without any particular aim but being useful to 
 
5
  anyone.
 
6
 
 
7
  Copyright (C) 2005-2008 Julien Jorge
 
8
 
 
9
  This library is free software; you can redistribute it and/or
 
10
  modify it under the terms of the GNU Lesser General Public
 
11
  License as published by the Free Software Foundation; either
 
12
  version 2.1 of the License, or (at your option) any later version.
 
13
 
 
14
  This library is distributed in the hope that it will be useful,
 
15
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
  Lesser General Public License for more details.
 
18
 
 
19
  You should have received a copy of the GNU Lesser General Public
 
20
  License along with this library; if not, write to the Free Software
 
21
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
22
 
 
23
  contact: julien_jorge@yahoo.fr
 
24
*/
 
25
/**
 
26
 * \file dynamic_library_traits_win32.hpp
 
27
 * \brief Microsoft Windows interface for using dynamic libraries.
 
28
 * \author Julien Jorge
 
29
 */
 
30
#ifndef __CLAW_DYNAMIC_LIBRARY_TRAITS_WIN32_HPP__
 
31
#define __CLAW_DYNAMIC_LIBRARY_TRAITS_WIN32_HPP__
 
32
 
 
33
#include <string>
 
34
//#include <cstdarg>
 
35
//#include <windef.h>
 
36
//#include <winbase.h>
 
37
#include <windows.h>
 
38
 
 
39
namespace claw
 
40
{
 
41
  /**
 
42
   * \brief Microsoft Windows interface for using dynamic libraries.
 
43
   * \author Julien Jorge
 
44
   */
 
45
  class dynamic_library_traits_win32
 
46
  {
 
47
  public:
 
48
     /** \brief Type of the system handle to the library. */
 
49
    typedef HMODULE handle;
 
50
 
 
51
  public:
 
52
    /*------------------------------------------------------------------------*/
 
53
    /**
 
54
     * \brief Open a library.
 
55
     * \param name The name of the library to open.
 
56
     * \return The handle on the loaded library.
 
57
     */
 
58
    static handle open( const std::string& name )
 
59
    {
 
60
      return LoadLibrary( name.c_str() );
 
61
    } // dynamic_library_traits_win32::open()
 
62
 
 
63
    /*------------------------------------------------------------------------*/
 
64
    /**
 
65
     * \brief Open the current program.
 
66
     * \param name The name of the current program.
 
67
     * \return The handle on the loaded library.
 
68
     */
 
69
    static handle auto_open( const std::string& name )
 
70
    {
 
71
      return LoadLibrary( name.c_str() );
 
72
    } // dynamic_library_traits_win32::auto_open()
 
73
 
 
74
    /*------------------------------------------------------------------------*/
 
75
    /**
 
76
     * \brief Close a library.
 
77
     * \param h The handle of the library to close.
 
78
     */
 
79
    static void close( handle h )
 
80
    {
 
81
      FreeLibrary(h);
 
82
    } // dynamic_library_traits_win32::close()
 
83
 
 
84
    /*------------------------------------------------------------------------*/
 
85
    /**
 
86
     * \brief Get a symbol from a library.
 
87
     * \param h Handle of the library.
 
88
     * \param name The name of the symbol to load.
 
89
     */
 
90
    template<class T>
 
91
    static T get_symbol( handle h, const std::string& name )
 
92
    {
 
93
      /* HACK : ISO standard doesn't allow to cast from a pointer to an object
 
94
         to a pointer to a function. */
 
95
      T result;
 
96
      *(FARPROC*)(&result) = GetProcAddress( h, name.c_str() );
 
97
 
 
98
      return result;
 
99
    } // dynamic_library_traits_win32::get_symbol()
 
100
 
 
101
    /*------------------------------------------------------------------------*/
 
102
    /**
 
103
     * \brief Tell if a symbol is in the library.
 
104
     * \param h Handle of the library.
 
105
     * \param name The name of the symbol to find.
 
106
     */
 
107
    static bool have_symbol( handle h, const std::string& name )
 
108
    {
 
109
      return GetProcAddress( h, name.c_str() ) != NULL;
 
110
    } // dynamic_library_traits_win32::have_symbol()
 
111
 
 
112
    /*------------------------------------------------------------------------*/
 
113
    /**
 
114
     * \brief Tell if an handle is a valid library handle.
 
115
     * \param h The handle to test.
 
116
     */
 
117
    static bool valid_handle( handle h )
 
118
    {
 
119
      return h != NULL;
 
120
    } // dynamic_library_traits_win32::valid_handle()
 
121
 
 
122
  }; // class dynamic_library_traits_win32
 
123
 
 
124
  typedef dynamic_library_traits_win32 dynamic_library_traits;
 
125
} // namespace claw
 
126
 
 
127
#endif // __CLAW_DYNAMIC_LIBRARY_TRAITS_WIN32_HPP__