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 
37 namespace Pt {
38 
39 namespace System {
40 
41 class Symbol;
42 
45 class PT_SYSTEM_API SymbolNotFound : public SystemError
46 {
47  public:
50  SymbolNotFound(const std::string& sym);
51 
54  SymbolNotFound(const char* sym);
55 
57  ~SymbolNotFound() throw()
58  {}
59 
61  const std::string& symbol() const
62  { return _symbol; }
63 
64  private:
65  std::string _symbol;
66 };
67 
106 class PT_SYSTEM_API Library
107 {
108  public:
111  Library();
112 
121  explicit Library(const Path& path);
122 
125  Library(const Library& other);
126 
129  Library& operator=(const Library& other);
130 
133  ~Library();
134 
144  Library& open(const Path& path);
145 
147  void close();
148 
152  void* operator[](const char* symbol) const
153  { return this->resolve(symbol); }
154 
158  void* resolve(const char* symbol) const;
159 
162  operator const void*() const;
163 
168  Symbol getSymbol(const char* symbol) const;
169 
172  bool operator!() const;
173 
176  const Path& path() const;
177 
182  static const char* suffix();
183 
188  static const char* prefix();
189 
190  protected:
192  void detach();
193 
194  private:
196  class LibraryImpl* _impl;
197 
199  System::Path _path;
200 };
201 
204 class Symbol
205 {
206  public:
210  : _sym(0)
211  { }
212 
215  Symbol(const Library& lib, void* sym)
216  : _lib(lib), _sym(sym)
217  { }
218 
221  void* sym() const
222  { return _sym; }
223 
226  const Library& library() const
227  { return _lib; }
228 
231  operator void*() const
232  { return _sym; }
233 
234  private:
235  Library _lib;
236  void* _sym;
237 };
238 
239 } // namespace System
240 
241 } // namespace Pt
242 
243 #endif // PT_SYSTEM_LIBRARY_H
Symbol()
Default constructor.
Definition: Library.h:209
Symbol resolved from a shared library.
Definition: Library.h:204
Represents a path in the file-system.
Definition: Path.h:47
const std::string & symbol() const
Returns the symbol, which was not found.
Definition: Library.h:61
Thrown, when a symbol is not found in a library.
Definition: Library.h:45
Exception class indication a system error.
Definition: SystemError.h:42
~SymbolNotFound()
Destructor.
Definition: Library.h:57
Shared library loader.
Definition: Library.h:106
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:152
void * sym() const
Returns the symbol address.
Definition: Library.h:221
Symbol(const Library &lib, void *sym)
Construct with library and symbol address.
Definition: Library.h:215
const Library & library() const
Returns the library where the symbol was resolved.
Definition: Library.h:226