]>
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 | ||
af49c4b8 | 16 | #if defined(__GNUG__) && !defined(__APPLE__) |
0b9ab0bd RL |
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 | ||
2b5f62a0 | 28 | #include "wx/hashmap.h" |
0b9ab0bd RL |
29 | #include "wx/module.h" |
30 | ||
4f89dbc4 RL |
31 | // FIXME: can this go in private.h or something too?? |
32 | #if defined(__WXPM__) || defined(__EMX__) | |
33 | #define INCL_DOS | |
34 | #include <os2.h> | |
35 | #endif | |
36 | ||
37 | #ifdef __WXMSW__ | |
38 | #include "wx/msw/private.h" | |
39 | #endif | |
0b9ab0bd | 40 | |
2b5f62a0 | 41 | class WXDLLEXPORT wxPluginLibrary; |
0b9ab0bd | 42 | |
2b5f62a0 VZ |
43 | WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxPluginLibrary *, wxDLManifest); |
44 | typedef wxDLManifest wxDLImports; | |
0b9ab0bd RL |
45 | |
46 | // ---------------------------------------------------------------------------- | |
47 | // conditional compilation | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | // Note: WXPM/EMX has to be tested first, since we want to use | |
51 | // native version, even if configure detected presence of DLOPEN. | |
52 | ||
4f89dbc4 RL |
53 | #if defined(__WXPM__) || defined(__EMX__) || defined(__WINDOWS__) |
54 | typedef HMODULE wxDllType; | |
0b9ab0bd RL |
55 | #elif defined(HAVE_DLOPEN) |
56 | #include <dlfcn.h> | |
4f89dbc4 | 57 | typedef void *wxDllType; |
0b9ab0bd RL |
58 | #elif defined(HAVE_SHL_LOAD) |
59 | #include <dl.h> | |
4f89dbc4 | 60 | typedef shl_t wxDllType; |
0b9ab0bd | 61 | #elif defined(__DARWIN__) |
4f89dbc4 | 62 | typedef void *wxDllType; |
0b9ab0bd | 63 | #elif defined(__WXMAC__) |
4f89dbc4 | 64 | typedef CFragConnectionID wxDllType; |
0b9ab0bd | 65 | #else |
4f89dbc4 | 66 | #error "Dynamic Loading classes can't be compiled on this platform, sorry." |
0b9ab0bd RL |
67 | #endif |
68 | ||
69 | ||
70 | // --------------------------------------------------------------------------- | |
4f89dbc4 | 71 | // wxDynamicLibrary |
0b9ab0bd RL |
72 | // --------------------------------------------------------------------------- |
73 | ||
4f89dbc4 RL |
74 | //FIXME: This class isn't really common at all, it should be moved |
75 | // into platform dependent files. | |
0b9ab0bd | 76 | |
4f89dbc4 RL |
77 | // NOTE: this class is (deliberately) not virtual, do not attempt |
78 | // to use it polymorphically. | |
79 | ||
80 | enum wxDLFlags | |
81 | { | |
82 | wxDL_LAZY = 0x00000001, // resolve undefined symbols at first use | |
83 | wxDL_NOW = 0x00000002, // resolve undefined symbols on load | |
84 | wxDL_GLOBAL = 0x00000004, // export extern symbols to subsequently | |
85 | // loaded libs. | |
86 | wxDL_VERBATIM = 0x00000008, // Attempt to load the supplied library | |
87 | // name without appending the usual dll | |
88 | // filename extension. | |
d0fcccc4 VZ |
89 | |
90 | wxDL_NOSHARE = 0x00000010, // load new DLL, don't reuse already loaded | |
91 | ||
92 | // FIXME: why? (VZ) | |
4f89dbc4 RL |
93 | #ifdef __osf__ |
94 | wxDL_DEFAULT = wxDL_LAZY | |
95 | #else | |
96 | wxDL_DEFAULT = wxDL_LAZY | wxDL_GLOBAL | |
97 | #endif | |
98 | }; | |
99 | ||
100 | ||
101 | class WXDLLEXPORT wxDynamicLibrary | |
0b9ab0bd RL |
102 | { |
103 | public: | |
104 | ||
4f89dbc4 RL |
105 | // return a valid handle for the main program itself or NULL if |
106 | // back linking is not supported by the current platform (e.g. Win32) | |
0b9ab0bd | 107 | |
4f89dbc4 | 108 | static wxDllType GetProgramHandle(); |
0b9ab0bd | 109 | |
4f89dbc4 | 110 | // return the platform standard DLL extension (with leading dot) |
0b9ab0bd | 111 | |
d0fcccc4 | 112 | static const wxChar *GetDllExt() { return ms_dllext; } |
0b9ab0bd | 113 | |
4f89dbc4 | 114 | wxDynamicLibrary() : m_handle(0) {} |
23213f18 | 115 | wxDynamicLibrary(wxString libname, int flags = wxDL_DEFAULT) |
4f89dbc4 RL |
116 | : m_handle(0) |
117 | { | |
118 | Load(libname, flags); | |
119 | } | |
120 | ~wxDynamicLibrary() { Unload(); } | |
0b9ab0bd | 121 | |
4f89dbc4 RL |
122 | // return TRUE if the library was loaded successfully |
123 | ||
124 | bool IsLoaded() const { return m_handle != 0; } | |
125 | ||
126 | // load the library with the given name | |
127 | // (full or not), return TRUE on success | |
128 | ||
23213f18 | 129 | bool Load(wxString libname, int flags = wxDL_DEFAULT); |
4f89dbc4 RL |
130 | |
131 | // unload the library, also done automatically in dtor | |
132 | ||
133 | void Unload(); | |
134 | ||
135 | // Return the raw handle from dlopen and friends. | |
136 | ||
137 | wxDllType GetLibHandle() const { return m_handle; } | |
0b9ab0bd RL |
138 | |
139 | // resolve a symbol in a loaded DLL, such as a variable or function | |
4f89dbc4 | 140 | // name. 'name' is the (possibly mangled) name of the symbol. |
0b9ab0bd RL |
141 | // (use extern "C" to export unmangled names) |
142 | // | |
143 | // Since it is perfectly valid for the returned symbol to actually be | |
144 | // NULL, that is not always indication of an error. Pass and test the | |
145 | // parameter 'success' for a true indication of success or failure to | |
146 | // load the symbol. | |
147 | // | |
4f89dbc4 RL |
148 | // Returns a pointer to the symbol on success, or NULL if an error |
149 | // occurred or the symbol wasn't found. | |
0b9ab0bd | 150 | |
4f89dbc4 | 151 | void *GetSymbol(const wxString& name, bool *success = 0) const; |
0b9ab0bd | 152 | |
4f89dbc4 RL |
153 | #if WXWIN_COMPATIBILITY_2_2 |
154 | operator bool() const { return IsLoaded(); } | |
155 | #endif | |
0b9ab0bd | 156 | |
4f89dbc4 | 157 | protected: |
0b9ab0bd | 158 | |
4f89dbc4 | 159 | // Platform specific shared lib suffix. |
0b9ab0bd | 160 | |
d0fcccc4 | 161 | static const wxChar *ms_dllext; |
4f89dbc4 RL |
162 | |
163 | // the handle to DLL or NULL | |
164 | ||
165 | wxDllType m_handle; | |
166 | ||
167 | // no copy ctor/assignment operators | |
168 | // or we'd try to unload the library twice | |
169 | ||
2b5f62a0 | 170 | DECLARE_NO_COPY_CLASS(wxDynamicLibrary) |
0b9ab0bd RL |
171 | }; |
172 | ||
173 | ||
174 | // --------------------------------------------------------------------------- | |
4f89dbc4 | 175 | // wxPluginLibrary |
0b9ab0bd RL |
176 | // --------------------------------------------------------------------------- |
177 | ||
4f89dbc4 RL |
178 | // NOTE: Do not attempt to use a base class pointer to this class. |
179 | // wxDL is not virtual and we deliberately hide some of it's | |
180 | // methods here. | |
181 | // | |
182 | // Unless you know exacty why you need to, you probably shouldn't | |
183 | // instantiate this class directly anyway, use wxPluginManager | |
184 | // instead. | |
185 | ||
186 | class WXDLLEXPORT wxPluginLibrary : public wxDynamicLibrary | |
0b9ab0bd RL |
187 | { |
188 | public: | |
189 | ||
dabd1377 | 190 | static wxDLImports* ms_classes; // Static hash of all imported classes. |
0b9ab0bd | 191 | |
23213f18 | 192 | wxPluginLibrary( const wxString &libname, int flags = wxDL_DEFAULT ); |
4f89dbc4 | 193 | ~wxPluginLibrary(); |
0b9ab0bd | 194 | |
d0fcccc4 | 195 | wxPluginLibrary *RefLib(); |
4f89dbc4 | 196 | bool UnrefLib(); |
7c1e2b44 RL |
197 | |
198 | // These two are called by the PluginSentinel on (PLUGGABLE) object | |
199 | // creation/destruction. There is usually no reason for the user to | |
200 | // call them directly. We have to separate this from the link count, | |
201 | // since the two are not interchangeable. | |
202 | ||
203 | // FIXME: for even better debugging PluginSentinel should register | |
204 | // the name of the class created too, then we can state | |
205 | // exactly which object was not destroyed which may be | |
206 | // difficult to find otherwise. Also this code should | |
207 | // probably only be active in DEBUG mode, but let's just | |
208 | // get it right first. | |
209 | ||
4f89dbc4 RL |
210 | void RefObj() { ++m_objcount; } |
211 | void UnrefObj() | |
7c1e2b44 RL |
212 | { |
213 | wxASSERT_MSG( m_objcount > 0, _T("Too many objects deleted??") ); | |
214 | --m_objcount; | |
215 | } | |
0b9ab0bd | 216 | |
4f89dbc4 | 217 | // Override/hide some base class methods |
0b9ab0bd | 218 | |
4f89dbc4 RL |
219 | bool IsLoaded() const { return m_linkcount > 0; } |
220 | void Unload() { UnrefLib(); } | |
0b9ab0bd RL |
221 | |
222 | private: | |
223 | ||
7c1e2b44 | 224 | wxClassInfo *m_before; // sm_first before loading this lib |
7c1e2b44 | 225 | wxClassInfo *m_after; // ..and after. |
0b9ab0bd | 226 | |
7c1e2b44 RL |
227 | size_t m_linkcount; // Ref count of library link calls |
228 | size_t m_objcount; // ..and (pluggable) object instantiations. | |
229 | wxModuleList m_wxmodules; // any wxModules that we initialised. | |
0b9ab0bd | 230 | |
7c1e2b44 RL |
231 | void UpdateClassInfo(); // Update the wxClassInfo table |
232 | void RestoreClassInfo(); // Restore the original wxClassInfo state. | |
233 | void RegisterModules(); // Init any wxModules in the lib. | |
234 | void UnregisterModules(); // Cleanup any wxModules we installed. | |
0b9ab0bd | 235 | |
2b5f62a0 | 236 | DECLARE_NO_COPY_CLASS(wxPluginLibrary) |
0b9ab0bd RL |
237 | }; |
238 | ||
239 | ||
4f89dbc4 | 240 | class WXDLLEXPORT wxPluginManager |
0b9ab0bd RL |
241 | { |
242 | public: | |
243 | ||
244 | // Static accessors. | |
245 | ||
4f89dbc4 | 246 | static wxPluginLibrary *LoadLibrary( const wxString &libname, |
23213f18 | 247 | int flags = wxDL_DEFAULT ); |
4f89dbc4 RL |
248 | static bool UnloadLibrary(const wxString &libname); |
249 | ||
250 | // This is used by wxDllLoader. It's wrapped in the compatibility | |
251 | // macro because it's of arguable use outside of that. | |
252 | ||
253 | #if WXWIN_COMPATIBILITY_2_2 | |
254 | static wxPluginLibrary *GetObjectFromHandle(wxDllType handle); | |
255 | #endif | |
0b9ab0bd RL |
256 | |
257 | // Instance methods. | |
258 | ||
2b5f62a0 | 259 | wxPluginManager() : m_entry(NULL) {}; |
23213f18 | 260 | wxPluginManager(const wxString &libname, int flags = wxDL_DEFAULT) |
4f89dbc4 RL |
261 | { |
262 | Load(libname, flags); | |
263 | } | |
264 | ~wxPluginManager() { Unload(); } | |
265 | ||
23213f18 | 266 | bool Load(const wxString &libname, int flags = wxDL_DEFAULT); |
4f89dbc4 | 267 | void Unload(); |
0b9ab0bd RL |
268 | |
269 | bool IsLoaded() const { return m_entry && m_entry->IsLoaded(); } | |
270 | void *GetSymbol(const wxString &symbol, bool *success = 0) | |
271 | { | |
272 | return m_entry->GetSymbol( symbol, success ); | |
273 | } | |
274 | ||
dabd1377 JS |
275 | static void CreateManifest() { ms_manifest = new wxDLManifest(wxKEY_STRING); } |
276 | static void ClearManifest() { delete ms_manifest; ms_manifest = NULL; } | |
277 | ||
0b9ab0bd | 278 | private: |
2b5f62a0 VZ |
279 | // return the pointer to the entry for the library with given name in |
280 | // ms_manifest or NULL if none | |
281 | static wxPluginLibrary *FindByName(const wxString& name) | |
282 | { | |
283 | const wxDLManifest::iterator i = ms_manifest->find(name); | |
284 | ||
285 | return i == ms_manifest->end() ? NULL : i->second; | |
286 | } | |
0b9ab0bd | 287 | |
dabd1377 JS |
288 | static wxDLManifest* ms_manifest; // Static hash of loaded libs. |
289 | wxPluginLibrary* m_entry; // Cache our entry in the manifest. | |
0b9ab0bd RL |
290 | |
291 | // We could allow this class to be copied if we really | |
292 | // wanted to, but not without modification. | |
2b5f62a0 | 293 | DECLARE_NO_COPY_CLASS(wxPluginManager) |
0b9ab0bd RL |
294 | }; |
295 | ||
296 | ||
4f89dbc4 RL |
297 | // --------------------------------------------------------------------------- |
298 | // wxDllLoader | |
299 | // --------------------------------------------------------------------------- | |
300 | ||
301 | // Cross platform wrapper for dlopen and friends. | |
302 | // There are no instances of this class, it simply | |
303 | // serves as a namespace for its static member functions. | |
304 | ||
305 | #if WXWIN_COMPATIBILITY_2_2 | |
306 | class WXDLLEXPORT wxDllLoader | |
307 | { | |
308 | public: | |
309 | ||
3d7fa4e6 | 310 | static wxDllType LoadLibrary(const wxString& name, bool *success = NULL); |
4f89dbc4 RL |
311 | static void UnloadLibrary(wxDllType dll); |
312 | ||
313 | static wxDllType GetProgramHandle() { return wxDynamicLibrary::GetProgramHandle(); } | |
314 | ||
315 | static void *GetSymbol(wxDllType dllHandle, const wxString &name, bool *success = 0); | |
316 | ||
d0fcccc4 | 317 | static wxString GetDllExt() { return wxDynamicLibrary::GetDllExt(); } |
4f89dbc4 RL |
318 | |
319 | private: | |
320 | ||
321 | wxDllLoader(); // forbid construction of objects | |
322 | }; | |
323 | #endif | |
324 | ||
0b9ab0bd RL |
325 | #endif // wxUSE_DYNAMIC_LOADER |
326 | #endif // _WX_DYNAMICLOADER_H__ | |
327 |