Claw  1.7.3
dynamic_library_traits_unix.hpp
Go to the documentation of this file.
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-2011 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@gamned.org
24 */
30 #ifndef __CLAW_DYNAMIC_LIBRARY_TRAITS_UNIX_HPP__
31 #define __CLAW_DYNAMIC_LIBRARY_TRAITS_UNIX_HPP__
32 
33 #include <string>
34 #include <dlfcn.h>
35 #include <claw/exception.hpp>
36 
37 namespace claw
38 {
44  {
45  public:
47  typedef void* handle;
48 
49  public:
50  /*------------------------------------------------------------------------*/
56  static handle open( const std::string& name )
57  {
58  handle h = dlopen( name.c_str(), RTLD_LAZY );
59 
60  if ( !valid_handle(h) )
61  throw claw::exception( dlerror() );
62 
63  return h;
64  } // dynamic_library_traits_unix::open()
65 
66  /*------------------------------------------------------------------------*/
72  static handle auto_open( const std::string& name )
73  {
74  handle h = dlopen( NULL, RTLD_LAZY );
75 
76  if ( !valid_handle(h) )
77  throw claw::exception( dlerror() );
78 
79  return h;
80  } // dynamic_library_traits_unix::auto_open()
81 
82  /*------------------------------------------------------------------------*/
87  static void close( handle h )
88  {
89  dlclose(h);
90  } // dynamic_library_traits_unix::close()
91 
92  /*------------------------------------------------------------------------*/
98  template<class T>
99  static T get_symbol( handle h, const std::string& name )
100  {
101  /* HACK : ISO standard doesn't allow to cast from a pointer to an object
102  to a pointer to a function. */
103  T result;
104  *(void**)(&result) = dlsym( h, name.c_str() );
105 
106  return result;
107  } // dynamic_library_traits_unix::get_symbol()
108 
109  /*------------------------------------------------------------------------*/
115  static bool have_symbol( handle h, const std::string& name )
116  {
117  return dlsym( h, name.c_str() ) != NULL;
118  } // dynamic_library_traits_unix::have_symbol()
119 
120  /*------------------------------------------------------------------------*/
125  static bool valid_handle( handle h )
126  {
127  return h != NULL;
128  } // dynamic_library_traits_unix::valid_handle()
129 
130  }; // class dynamic_library_traits_unix
131 
134 
135 } // namespace claw
136 
137 #endif // __CLAW_DYNAMIC_LIBRARY_TRAITS_UNIX_HPP__