]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dynload.h
reimplemented sanity checks that were lost/broken in the regrettably
[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
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 *RefLib() { ++m_linkcount; return this; }
141 bool UnrefLib();
142
143 // These two are called by the PluginSentinel on (PLUGGABLE) object
144 // creation/destruction. There is usually no reason for the user to
145 // call them directly. We have to separate this from the link count,
146 // since the two are not interchangeable.
147
148 // FIXME: for even better debugging PluginSentinel should register
149 // the name of the class created too, then we can state
150 // exactly which object was not destroyed which may be
151 // difficult to find otherwise. Also this code should
152 // probably only be active in DEBUG mode, but let's just
153 // get it right first.
154
155 void RefObj() { ++m_objcount; }
156 void UnrefObj()
157 {
158 wxASSERT_MSG( m_objcount > 0, _T("Too many objects deleted??") );
159 --m_objcount;
160 }
161
162 bool IsLoaded() const { return m_linkcount > 0; }
163
164 wxDllType GetLinkHandle() const { return m_handle; }
165 wxDllType GetProgramHandle() const { return wxDllLoader::GetProgramHandle(); }
166 void *GetSymbol(const wxString &symbol, bool *success = 0)
167 {
168 return wxDllLoader::GetSymbol( m_handle, symbol, success );
169 }
170
171 private:
172
173 // Order of these three *is* important, do not change it
174
175 wxClassInfo *m_before; // sm_first before loading this lib
176 wxDllType m_handle; // Handle from dlopen.
177 wxClassInfo *m_after; // ..and after.
178
179 size_t m_linkcount; // Ref count of library link calls
180 size_t m_objcount; // ..and (pluggable) object instantiations.
181 wxModuleList m_wxmodules; // any wxModules that we initialised.
182
183 void UpdateClassInfo(); // Update the wxClassInfo table
184 void RestoreClassInfo(); // Restore the original wxClassInfo state.
185 void RegisterModules(); // Init any wxModules in the lib.
186 void UnregisterModules(); // Cleanup any wxModules we installed.
187
188 DECLARE_NO_COPY_CLASS(wxDLManifestEntry)
189 };
190
191
192 class WXDLLEXPORT wxDynamicLibrary
193 {
194 public:
195
196 // Static accessors.
197
198 static wxDLManifestEntry *Link(const wxString &libname);
199 static bool Unlink(const wxString &libname);
200
201 // Instance methods.
202
203 wxDynamicLibrary(const wxString &libname);
204 ~wxDynamicLibrary();
205
206 bool IsLoaded() const { return m_entry && m_entry->IsLoaded(); }
207 void *GetSymbol(const wxString &symbol, bool *success = 0)
208 {
209 return m_entry->GetSymbol( symbol, success );
210 }
211
212 private:
213
214 static wxDLManifest ms_manifest; // Static hash of loaded libs.
215 wxDLManifestEntry *m_entry; // Cache our entry in the manifest.
216
217 // We could allow this class to be copied if we really
218 // wanted to, but not without modification.
219
220 DECLARE_NO_COPY_CLASS(wxDynamicLibrary)
221 };
222
223
224 #endif // wxUSE_DYNAMIC_LOADER
225 #endif // _WX_DYNAMICLOADER_H__
226
227 // vi:sts=4:sw=4:et