Rename some GC classes so that their roll will hopefully be clearer.
[oota-llvm.git] / include / llvm / System / DynamicLibrary.h
1 //===-- llvm/System/DynamicLibrary.h - Portable Dynamic Library -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the sys::DynamicLibrary class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SYSTEM_DYNAMIC_LIBRARY_H
15 #define LLVM_SYSTEM_DYNAMIC_LIBRARY_H
16
17 #include "llvm/System/Path.h"
18 #include <string>
19
20 namespace llvm {
21 namespace sys {
22
23   /// This class provides a portable interface to dynamic libraries which also
24   /// might be known as shared libraries, shared objects, dynamic shared
25   /// objects, or dynamic link libraries. Regardless of the terminology or the
26   /// operating system interface, this class provides a portable interface that
27   /// allows dynamic libraries to be loaded and and searched for externally
28   /// defined symbols. This is typically used to provide "plug-in" support.
29   /// It also allows for symbols to be defined which don't live in any library,
30   /// but rather the main program itself, useful on Windows where the main
31   /// executable cannot be searched.
32   class DynamicLibrary {
33     /// @name Constructors
34     /// @{
35     public:
36       /// Construct a DynamicLibrary that represents the currently executing
37       /// program. The program must have been linked with -export-dynamic or
38       /// -dlopen self for this to work. 
39       /// @throws std::string indicating why the program couldn't be opened.
40       /// @brief Open program as dynamic library.
41       DynamicLibrary();
42
43       /// After destruction, the symbols of the library will no longer be
44       /// available to the program. 
45       /// @brief Closes the DynamicLibrary
46       ~DynamicLibrary();
47
48     /// @}
49     /// @name Functions
50     /// @{
51     public:
52       /// This function allows a library to be loaded without instantiating a
53       /// DynamicLibrary object. Consequently, it is marked as being permanent
54       /// and will only be unloaded when the program terminates.  This returns
55       /// false on success or returns true and fills in *ErrMsg on failure.
56       /// @brief Open a dynamic library permanently.
57       static bool LoadLibraryPermanently(const char* filename,
58                                          std::string *ErrMsg = 0);
59
60       /// This function will search through all previously loaded dynamic
61       /// libraries for the symbol \p symbolName. If it is found, the addressof
62       /// that symbol is returned. If not, null is returned. Note that this will
63       /// search permanently loaded libraries (LoadLibraryPermanently) as well
64       /// as ephemerally loaded libraries (constructors).
65       /// @throws std::string on error.
66       /// @brief Search through libraries for address of a symbol
67       static void* SearchForAddressOfSymbol(const char* symbolName);
68
69       /// @brief Convenience function for C++ophiles.
70       static void* SearchForAddressOfSymbol(const std::string& symbolName) {
71         return SearchForAddressOfSymbol(symbolName.c_str());
72       }
73
74       /// This functions permanently adds the symbol \p symbolName with the
75       /// value \p symbolValue.  These symbols are searched before any
76       /// libraries.
77       /// @brief Add searchable symbol/value pair.
78       static void AddSymbol(const char* symbolName, void *symbolValue);
79
80       /// @brief Convenience function for C++ophiles.
81       static void AddSymbol(const std::string& symbolName, void *symbolValue) {
82         AddSymbol(symbolName.c_str(), symbolValue);
83       }
84
85     /// @}
86     /// @name Implementation
87     /// @{
88     protected:
89       void* handle;  // Opaque handle for information about the library
90       DynamicLibrary(const DynamicLibrary&); ///< Do not implement
91       DynamicLibrary& operator=(const DynamicLibrary&); ///< Do not implement
92     /// @}
93   };
94
95 } // End sys namespace
96 } // End llvm namespace
97
98 #endif // LLVM_SYSTEM_DYNAMIC_LIBRARY_H