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