]>
Commit | Line | Data |
---|---|---|
0b9ab0bd RL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dynload.h | |
3 | // Purpose: Dynamic loading framework | |
4 | // Author: Ron Lee, David Falkinder, Vadim Zeitlin and a cast of 1000's | |
5 | // (derived in part from dynlib.cpp (c) 1998 Guilhem Lavaux) | |
6 | // Modified by: | |
7 | // Created: 03/12/01 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 2001 Ron Lee <ron@debian.org> | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | #ifndef _WX_DYNAMICLOADER_H__ | |
14 | #define _WX_DYNAMICLOADER_H__ | |
15 | ||
16 | #ifdef __GNUG__ | |
17 | #pragma interface "dynload.h" | |
18 | #endif | |
19 | ||
20 | // ---------------------------------------------------------------------------- | |
21 | // headers | |
22 | // ---------------------------------------------------------------------------- | |
23 | ||
24 | #include "wx/defs.h" | |
25 | ||
26 | #if wxUSE_DYNAMIC_LOADER | |
27 | ||
28 | #include "wx/hash.h" | |
29 | #include "wx/module.h" | |
30 | ||
31 | ||
32 | // Ugh, I'd much rather this was typesafe, but no time | |
33 | // to rewrite wxHashTable right now.. | |
34 | ||
35 | typedef wxHashTable wxDLManifest; | |
36 | typedef wxHashTable wxDLImports; | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // conditional compilation | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | // Note: WXPM/EMX has to be tested first, since we want to use | |
43 | // native version, even if configure detected presence of DLOPEN. | |
44 | ||
45 | #if defined(__WXPM__) || defined(__EMX__) | |
46 | #define INCL_DOS | |
47 | #include <os2.h> | |
48 | typedef HMODULE wxDllType; | |
49 | #elif defined(HAVE_DLOPEN) | |
50 | #include <dlfcn.h> | |
51 | typedef void *wxDllType; | |
52 | #elif defined(HAVE_SHL_LOAD) | |
53 | #include <dl.h> | |
54 | typedef shl_t wxDllType; | |
55 | #elif defined(__WINDOWS__) | |
56 | #include <windows.h> // needed to get HMODULE | |
57 | typedef HMODULE wxDllType; | |
58 | #elif defined(__DARWIN__) | |
59 | typedef void *wxDllType; | |
60 | #elif defined(__WXMAC__) | |
61 | typedef CFragConnectionID wxDllType; | |
62 | #else | |
63 | #error "wxLibrary can't be compiled on this platform, sorry." | |
64 | #endif | |
65 | ||
66 | // LoadLibrary is defined in windows.h as LoadLibraryA, but wxDllLoader | |
67 | // method should be called LoadLibrary, not LoadLibraryA or LoadLibraryW! | |
68 | ||
69 | #if defined(__WIN32__) && defined(LoadLibrary) | |
70 | # include "wx/msw/winundef.h" | |
71 | #endif | |
72 | ||
73 | ||
74 | // --------------------------------------------------------------------------- | |
75 | // wxDllLoader | |
76 | // --------------------------------------------------------------------------- | |
77 | ||
78 | // Cross platform wrapper for dlopen and friends. | |
79 | // There are no instances of this class, it simply | |
80 | // serves as a namespace for its static member functions. | |
81 | ||
82 | class WXDLLEXPORT wxDllLoader | |
83 | { | |
84 | public: | |
85 | ||
86 | // libname may be either the full path to the file or just the filename | |
87 | // in which case the library is searched for in all standard locations. | |
88 | // The platform specific library extension is automatically appended. | |
89 | ||
90 | static wxDllType Load(const wxString& name); | |
91 | ||
92 | // The same as Load, except 'name' is searched for without modification. | |
93 | ||
94 | static wxDllType LoadLibrary(const wxString& name); | |
95 | static void UnloadLibrary(wxDllType dll); | |
96 | ||
97 | // return a valid handle for the main program itself or NULL if | |
98 | // back linking is not supported by the current platform (e.g. Win32) | |
99 | ||
100 | static wxDllType GetProgramHandle(); | |
101 | ||
102 | // resolve a symbol in a loaded DLL, such as a variable or function | |
103 | // name. dllHandle is a handle previously returned by LoadLibrary(), | |
104 | // symbol is the (possibly mangled) name of the symbol. | |
105 | // (use extern "C" to export unmangled names) | |
106 | // | |
107 | // Since it is perfectly valid for the returned symbol to actually be | |
108 | // NULL, that is not always indication of an error. Pass and test the | |
109 | // parameter 'success' for a true indication of success or failure to | |
110 | // load the symbol. | |
111 | // | |
112 | // Returns a pointer to the symbol on success. | |
113 | ||
114 | static void *GetSymbol(wxDllType dllHandle, const wxString &name, bool *success = 0); | |
115 | ||
116 | // return the platform standard DLL extension (with leading dot) | |
117 | ||
118 | static const wxString &GetDllExt() { return ms_dllext; } | |
119 | ||
120 | private: | |
121 | ||
122 | wxDllLoader(); // forbid construction of objects | |
123 | static const wxString ms_dllext; // Platform specific shared lib suffix. | |
124 | }; | |
125 | ||
126 | ||
127 | // --------------------------------------------------------------------------- | |
128 | // wxDynamicLibrary | |
129 | // --------------------------------------------------------------------------- | |
130 | ||
131 | class wxDLManifestEntry | |
132 | { | |
133 | public: | |
134 | ||
135 | static wxDLImports ms_classes; // Static hash of all imported classes. | |
136 | ||
137 | wxDLManifestEntry( const wxString &libname ); | |
138 | ~wxDLManifestEntry(); | |
139 | ||
140 | wxDLManifestEntry *Ref() { ++m_count; return this; } | |
141 | bool Unref() { return (m_count-- < 2) ? (delete this, TRUE) : FALSE; } | |
142 | ||
143 | bool IsLoaded() const { return m_count > 0; } | |
144 | ||
145 | wxDllType GetLinkHandle() const { return m_handle; } | |
146 | wxDllType GetProgramHandle() const { return wxDllLoader::GetProgramHandle(); } | |
147 | void *GetSymbol(const wxString &symbol, bool *success = 0) | |
148 | { | |
149 | return wxDllLoader::GetSymbol( m_handle, symbol, success ); | |
150 | } | |
151 | ||
152 | private: | |
153 | ||
154 | // Order of these three *is* important, do not change it | |
155 | ||
156 | wxClassInfo *m_before; // sm_first before loading this lib | |
157 | wxDllType m_handle; // Handle from dlopen. | |
158 | wxClassInfo *m_after; // ..and after. | |
159 | ||
160 | size_t m_count; // Ref count of Link and Create calls. | |
161 | wxModuleList m_wxmodules; // any wxModules that we initialised. | |
162 | ||
163 | void UpdateClassInfo(); // Update the wxClassInfo table | |
164 | void RestoreClassInfo(); // Restore the original wxClassInfo state. | |
165 | void RegisterModules(); // Init any wxModules in the lib. | |
166 | void UnregisterModules(); // Cleanup any wxModules we installed. | |
167 | ||
168 | DECLARE_NO_COPY_CLASS(wxDLManifestEntry) | |
169 | }; | |
170 | ||
171 | ||
172 | class WXDLLEXPORT wxDynamicLibrary | |
173 | { | |
174 | public: | |
175 | ||
176 | // Static accessors. | |
177 | ||
178 | static wxDLManifestEntry *Link(const wxString &libname); | |
179 | static bool Unlink(const wxString &libname); | |
180 | ||
181 | // Instance methods. | |
182 | ||
183 | wxDynamicLibrary(const wxString &libname); | |
184 | ~wxDynamicLibrary(); | |
185 | ||
186 | bool IsLoaded() const { return m_entry && m_entry->IsLoaded(); } | |
187 | void *GetSymbol(const wxString &symbol, bool *success = 0) | |
188 | { | |
189 | return m_entry->GetSymbol( symbol, success ); | |
190 | } | |
191 | ||
192 | private: | |
193 | ||
194 | static wxDLManifest ms_manifest; // Static hash of loaded libs. | |
195 | wxDLManifestEntry *m_entry; // Cache our entry in the manifest. | |
196 | ||
197 | // We could allow this class to be copied if we really | |
198 | // wanted to, but not without modification. | |
199 | ||
200 | DECLARE_NO_COPY_CLASS(wxDynamicLibrary) | |
201 | }; | |
202 | ||
203 | ||
204 | #endif // wxUSE_DYNAMIC_LOADER | |
205 | #endif // _WX_DYNAMICLOADER_H__ | |
206 | ||
207 | // vi:sts=4:sw=4:et |