Library.h
1/*
2 * Copyright (C) 2006-2008 Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef PT_SYSTEM_LIBRARY_H
30#define PT_SYSTEM_LIBRARY_H
31
32#include <Pt/System/Api.h>
33#include <Pt/System/SystemError.h>
34#include <Pt/System/Path.h>
35#include <string>
36
37namespace Pt {
38
39namespace System {
40
41class Symbol;
42
47class PT_SYSTEM_API SymbolNotFound : public SystemError
48{
49 public:
52 SymbolNotFound(const std::string& sym);
53
56 SymbolNotFound(const char* sym);
57
60 {}
61
63 const std::string& symbol() const
64 { return _symbol; }
65
66 private:
67 std::string _symbol;
68};
69
122class PT_SYSTEM_API Library
123{
124 public:
128
137 explicit Library(const Path& path);
138
141 Library(const Library& other);
142
145 Library& operator=(const Library& other);
146
150
161
163 void close();
164
168 void* operator[](const char* symbol) const
169 { return this->resolve(symbol); }
170
174 void* resolve(const char* symbol) const;
175
178 operator const void*() const;
179
184 Symbol getSymbol(const char* symbol) const;
185
188 bool operator!() const;
189
192 const Path& path() const;
193
198 static const char* suffix();
199
204 static const char* prefix();
205
206 protected:
208 void detach();
209
210 private:
212 class LibraryImpl* _impl;
213
215 System::Path _path;
216};
217
223{
224 public:
228 : _sym(0)
229 { }
230
233 Symbol(const Library& lib, void* sym)
234 : _lib(lib), _sym(sym)
235 { }
236
239 void* sym() const
240 { return _sym; }
241
244 const Library& library() const
245 { return _lib; }
246
249 operator void*() const
250 { return _sym; }
251
252 private:
253 Library _lib;
254 void* _sym;
255};
256
257} // namespace System
258
259} // namespace Pt
260
261#endif // PT_SYSTEM_LIBRARY_H
Shared library loader.
Definition Library.h:123
Library & open(const Path &path)
Loads a shared library.
Library & operator=(const Library &other)
Assignment operator.
Symbol getSymbol(const char *symbol) const
Resolves the symbol symbol from the shared library.
Library()
Default Constructor which does not load a library.
void * operator[](const char *symbol) const
Resolves the symbol symbol from the shared library Returns the address of the symbol or 0 if it was n...
Definition Library.h:168
const Path & path() const
Returns the path to the shared library image.
~Library()
The destructor unloads the shared library from memory.
void close()
Closes the shared library.
static const char * suffix()
Returns the extension for shared libraries.
bool operator!() const
Returns true if invalid.
void * resolve(const char *symbol) const
Resolves the symbol symbol from the shared library Returns the address of the symbol or 0 if it was n...
Library(const Path &path)
Loads a shared library.
static const char * prefix()
Returns the prefix for shared libraries.
Library(const Library &other)
Copy constructor.
Represents a path in the file-system.
Definition Path.h:48
SymbolNotFound(const std::string &sym)
Construct with symbol.
SymbolNotFound(const char *sym)
Construct with symbol.
~SymbolNotFound()
Destructor.
Definition Library.h:59
const std::string & symbol() const
Returns the symbol, which was not found.
Definition Library.h:63
Symbol resolved from a shared library.
Definition Library.h:223
void * sym() const
Returns the symbol address.
Definition Library.h:239
Symbol()
Default constructor.
Definition Library.h:227
const Library & library() const
Returns the library where the symbol was resolved.
Definition Library.h:244
Symbol(const Library &lib, void *sym)
Construct with library and symbol address.
Definition Library.h:233
SystemError(const std::string &what)
Construct with error message.
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33