]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dynload.h
typo fixes
[wxWidgets.git] / include / wx / dynload.h
CommitLineData
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
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
RL
40
41// Ugh, I'd much rather this was typesafe, but no time
42// to rewrite wxHashTable right now..
43
44typedef wxHashTable wxDLManifest;
45typedef 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
4f89dbc4
RL
54#if defined(__WXPM__) || defined(__EMX__) || defined(__WINDOWS__)
55typedef HMODULE wxDllType;
0b9ab0bd
RL
56#elif defined(HAVE_DLOPEN)
57#include <dlfcn.h>
4f89dbc4 58typedef void *wxDllType;
0b9ab0bd
RL
59#elif defined(HAVE_SHL_LOAD)
60#include <dl.h>
4f89dbc4 61typedef shl_t wxDllType;
0b9ab0bd 62#elif defined(__DARWIN__)
4f89dbc4 63typedef void *wxDllType;
0b9ab0bd 64#elif defined(__WXMAC__)
4f89dbc4 65typedef CFragConnectionID wxDllType;
0b9ab0bd 66#else
4f89dbc4 67#error "Dynamic Loading classes can't be compiled on this platform, sorry."
0b9ab0bd
RL
68#endif
69
70
71// ---------------------------------------------------------------------------
4f89dbc4 72// wxDynamicLibrary
0b9ab0bd
RL
73// ---------------------------------------------------------------------------
74
4f89dbc4
RL
75//FIXME: This class isn't really common at all, it should be moved
76// into platform dependent files.
0b9ab0bd 77
4f89dbc4
RL
78// NOTE: this class is (deliberately) not virtual, do not attempt
79// to use it polymorphically.
80
81enum 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.
d0fcccc4
VZ
90
91 wxDL_NOSHARE = 0x00000010, // load new DLL, don't reuse already loaded
92
93 // FIXME: why? (VZ)
4f89dbc4
RL
94#ifdef __osf__
95 wxDL_DEFAULT = wxDL_LAZY
96#else
97 wxDL_DEFAULT = wxDL_LAZY | wxDL_GLOBAL
98#endif
99};
100
101
102class WXDLLEXPORT wxDynamicLibrary
0b9ab0bd
RL
103{
104public:
105
4f89dbc4
RL
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)
0b9ab0bd 108
4f89dbc4 109 static wxDllType GetProgramHandle();
0b9ab0bd 110
4f89dbc4 111 // return the platform standard DLL extension (with leading dot)
0b9ab0bd 112
d0fcccc4 113 static const wxChar *GetDllExt() { return ms_dllext; }
0b9ab0bd 114
4f89dbc4 115 wxDynamicLibrary() : m_handle(0) {}
23213f18 116 wxDynamicLibrary(wxString libname, int flags = wxDL_DEFAULT)
4f89dbc4
RL
117 : m_handle(0)
118 {
119 Load(libname, flags);
120 }
121 ~wxDynamicLibrary() { Unload(); }
0b9ab0bd 122
4f89dbc4
RL
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
23213f18 130 bool Load(wxString libname, int flags = wxDL_DEFAULT);
4f89dbc4
RL
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; }
0b9ab0bd
RL
139
140 // resolve a symbol in a loaded DLL, such as a variable or function
4f89dbc4 141 // name. 'name' is the (possibly mangled) name of the symbol.
0b9ab0bd
RL
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 //
4f89dbc4
RL
149 // Returns a pointer to the symbol on success, or NULL if an error
150 // occurred or the symbol wasn't found.
0b9ab0bd 151
4f89dbc4 152 void *GetSymbol(const wxString& name, bool *success = 0) const;
0b9ab0bd 153
4f89dbc4
RL
154#if WXWIN_COMPATIBILITY_2_2
155 operator bool() const { return IsLoaded(); }
156#endif
0b9ab0bd 157
4f89dbc4 158protected:
0b9ab0bd 159
4f89dbc4 160 // Platform specific shared lib suffix.
0b9ab0bd 161
d0fcccc4 162 static const wxChar *ms_dllext;
4f89dbc4
RL
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
171DECLARE_NO_COPY_CLASS(wxDynamicLibrary)
0b9ab0bd
RL
172};
173
174
175// ---------------------------------------------------------------------------
4f89dbc4 176// wxPluginLibrary
0b9ab0bd
RL
177// ---------------------------------------------------------------------------
178
4f89dbc4
RL
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
187class WXDLLEXPORT wxPluginLibrary : public wxDynamicLibrary
0b9ab0bd
RL
188{
189public:
190
dabd1377 191 static wxDLImports* ms_classes; // Static hash of all imported classes.
0b9ab0bd 192
23213f18 193 wxPluginLibrary( const wxString &libname, int flags = wxDL_DEFAULT );
4f89dbc4 194 ~wxPluginLibrary();
0b9ab0bd 195
d0fcccc4 196 wxPluginLibrary *RefLib();
4f89dbc4 197 bool UnrefLib();
7c1e2b44
RL
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
4f89dbc4
RL
211 void RefObj() { ++m_objcount; }
212 void UnrefObj()
7c1e2b44
RL
213 {
214 wxASSERT_MSG( m_objcount > 0, _T("Too many objects deleted??") );
215 --m_objcount;
216 }
0b9ab0bd 217
4f89dbc4 218 // Override/hide some base class methods
0b9ab0bd 219
4f89dbc4
RL
220 bool IsLoaded() const { return m_linkcount > 0; }
221 void Unload() { UnrefLib(); }
0b9ab0bd
RL
222
223private:
224
7c1e2b44 225 wxClassInfo *m_before; // sm_first before loading this lib
7c1e2b44 226 wxClassInfo *m_after; // ..and after.
0b9ab0bd 227
7c1e2b44
RL
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.
0b9ab0bd 231
7c1e2b44
RL
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.
0b9ab0bd 236
4f89dbc4 237DECLARE_NO_COPY_CLASS(wxPluginLibrary)
0b9ab0bd
RL
238};
239
240
4f89dbc4 241class WXDLLEXPORT wxPluginManager
0b9ab0bd
RL
242{
243public:
244
245 // Static accessors.
246
4f89dbc4 247 static wxPluginLibrary *LoadLibrary( const wxString &libname,
23213f18 248 int flags = wxDL_DEFAULT );
4f89dbc4
RL
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
0b9ab0bd
RL
257
258 // Instance methods.
259
4f89dbc4 260 wxPluginManager() : m_entry(0) {};
23213f18 261 wxPluginManager(const wxString &libname, int flags = wxDL_DEFAULT)
4f89dbc4
RL
262 {
263 Load(libname, flags);
264 }
265 ~wxPluginManager() { Unload(); }
266
23213f18 267 bool Load(const wxString &libname, int flags = wxDL_DEFAULT);
4f89dbc4 268 void Unload();
0b9ab0bd
RL
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
dabd1377
JS
276 static void CreateManifest() { ms_manifest = new wxDLManifest(wxKEY_STRING); }
277 static void ClearManifest() { delete ms_manifest; ms_manifest = NULL; }
278
0b9ab0bd
RL
279private:
280
dabd1377
JS
281 static wxDLManifest* ms_manifest; // Static hash of loaded libs.
282 wxPluginLibrary* m_entry; // Cache our entry in the manifest.
0b9ab0bd
RL
283
284 // We could allow this class to be copied if we really
285 // wanted to, but not without modification.
286
4f89dbc4 287DECLARE_NO_COPY_CLASS(wxPluginManager)
0b9ab0bd
RL
288};
289
290
4f89dbc4
RL
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
300class WXDLLEXPORT wxDllLoader
301{
302public:
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
d0fcccc4 311 static wxString GetDllExt() { return wxDynamicLibrary::GetDllExt(); }
4f89dbc4
RL
312
313private:
314
315 wxDllLoader(); // forbid construction of objects
316};
317#endif
318
0b9ab0bd
RL
319#endif // wxUSE_DYNAMIC_LOADER
320#endif // _WX_DYNAMICLOADER_H__
321