Dynamic Library abstraction. This makes the abstraction of a single dynamic
[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 was developed by Reid Spencer and is distributed under the 
6 // University of Illinois Open Source 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   /// @since 1.4
30   /// @brief Portable dynamic library abstraction.
31   class DynamicLibrary {
32     /// @name Constructors
33     /// @{
34     public:
35       /// This is the constructor for DynamicLibrary instances. It will open
36       /// the dynamic library specified by the \filename Path.
37       /// @throws std::string indicating why the library couldn't be opened.
38       /// @brief DynamicLibrary constructor
39       DynamicLibrary(const char* filename);
40
41       /// After destruction, the symbols of the library will no longer be
42       /// available to the program. It is important to make sure the lifespan
43       /// of a DynamicLibrary exceeds the lifetime of the pointers returned 
44       /// by the GetAddressOfSymbol otherwise the program may walk off into 
45       /// uncharted territory.
46       /// @see GetAddressOfSymbol.
47       /// @brief Closes the DynamicLibrary
48       ~DynamicLibrary();
49
50     /// @}
51     /// @name Accessors
52     /// @{
53     public:
54       /// Looks up a \p symbolName in the DynamicLibrary and returns its address
55       /// if it exists. If the symbol does not exist, returns (void*)0.
56       /// @returns the address of the symbol or 0.
57       /// @brief Get the address of a symbol in the DynamicLibrary.
58       void* GetAddressOfSymbol(const char* symbolName);
59
60       /// @brief Convenience function for C++ophiles.
61       void* GetAddressOfSymbol(const std::string& symbolName) {
62         return GetAddressOfSymbol(symbolName.c_str());
63       }
64
65     /// @}
66     /// @name Implementation
67     /// @{
68     protected:
69       void* handle;  // Opaque handle for information about the library
70
71       DynamicLibrary();  ///< Do not implement
72       DynamicLibrary(const DynamicLibrary&); ///< Do not implement
73       DynamicLibrary& operator=(const DynamicLibrary&); ///< Do not implement
74     /// @}
75   };
76
77 } // End sys namespace
78 } // End llvm namespace
79
80 #endif // LLVM_SYSTEM_DYNAMIC_LIBRARY_H