claw 1.9.0
 
Loading...
Searching...
No Matches
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@stuff-o-matic.com
24*/
30#ifndef __CLAW_DYNAMIC_LIBRARY_TRAITS_UNIX_HPP__
31#define __CLAW_DYNAMIC_LIBRARY_TRAITS_UNIX_HPP__
32
33#include <claw/exception.hpp>
34#include <dlfcn.h>
35#include <string>
36
37namespace claw
38{
44 {
45 public:
47 typedef void* handle;
48
49 public:
55 static handle open(const std::string& name)
56 {
57 handle h = dlopen(name.c_str(), RTLD_LAZY);
58
59 if(!valid_handle(h))
60 throw claw::exception(dlerror());
61
62 return h;
63 }
64
70 static handle auto_open(const std::string& name)
71 {
72 handle h = dlopen(NULL, RTLD_LAZY);
73
74 if(!valid_handle(h))
75 throw claw::exception(dlerror());
76
77 return h;
78 }
79
84 static void close(handle h)
85 {
86 dlclose(h);
87 }
88
94 template <class T>
95 static T get_symbol(handle h, const std::string& name)
96 {
97 /* HACK : ISO standard doesn't allow to cast from a pointer to an object
98 to a pointer to a function. */
99 T result;
100 *(void**)(&result) = dlsym(h, name.c_str());
101
102 return result;
103 }
104
110 static bool have_symbol(handle h, const std::string& name)
111 {
112 return dlsym(h, name.c_str()) != NULL;
113 }
114
119 static bool valid_handle(handle h)
120 {
121 return h != NULL;
122 }
123
124 }; // class dynamic_library_traits_unix
125
128
129}
130
131#endif // __CLAW_DYNAMIC_LIBRARY_TRAITS_UNIX_HPP__
Unix interface for using dynamic libraries.
static T get_symbol(handle h, const std::string &name)
Get a symbol from a library.
static bool have_symbol(handle h, const std::string &name)
Tell if a symbol is in the library.
static handle open(const std::string &name)
Open a library.
static handle auto_open(const std::string &name)
Open the current program.
static void close(handle h)
Close a library.
void * handle
Type of the system handle to the library.
static bool valid_handle(handle h)
Tell if an handle is a valid library handle.
A simple class to use as exception with string message.
Definition exception.hpp:43
A simple class to use as exception with string message.
This is the main namespace.
dynamic_library_traits_unix dynamic_library_traits
The traits to access the dynamic libraries in Unix system.