]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynload.cpp
wxClassInfo (un)registration now happens automatically, no need to do it in wxPluginL...
[wxWidgets.git] / src / common / dynload.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dynload.cpp
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 licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "dynload.h"
15 #endif
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_DYNAMIC_LOADER
28
29 #ifdef __WINDOWS__
30 #include "wx/msw/private.h"
31 #endif
32
33 #ifndef WX_PRECOMP
34 #include "wx/log.h"
35 #include "wx/intl.h"
36 #include "wx/hash.h"
37 #include "wx/utils.h"
38 #endif
39
40 #include "wx/strconv.h"
41
42 #include "wx/dynload.h"
43 #include "wx/module.h"
44
45
46 // ---------------------------------------------------------------------------
47 // wxPluginLibrary
48 // ---------------------------------------------------------------------------
49
50
51 wxDLImports* wxPluginLibrary::ms_classes = NULL;
52
53 class wxPluginLibraryModule : public wxModule
54 {
55 public:
56 wxPluginLibraryModule() { }
57
58 // TODO: create ms_classes on demand, why always preallocate it?
59 virtual bool OnInit()
60 {
61 wxPluginLibrary::ms_classes = new wxDLImports;
62 wxPluginManager::CreateManifest();
63 return TRUE;
64 }
65
66 virtual void OnExit()
67 {
68 delete wxPluginLibrary::ms_classes;
69 wxPluginLibrary::ms_classes = NULL;
70 wxPluginManager::ClearManifest();
71 }
72
73 private:
74 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule )
75 };
76
77 IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule, wxModule)
78
79
80 wxPluginLibrary::wxPluginLibrary(const wxString &libname, int flags)
81 : m_linkcount(1)
82 , m_objcount(0)
83 {
84 m_before = wxClassInfo::sm_first;
85 Load( libname, flags );
86 m_after = wxClassInfo::sm_first;
87
88 if( m_handle != 0 )
89 {
90 RegisterModules();
91 }
92 else
93 {
94 // Flag us for deletion
95 --m_linkcount;
96 }
97 }
98
99 wxPluginLibrary::~wxPluginLibrary()
100 {
101 if( m_handle != 0 )
102 {
103 UnregisterModules();
104 }
105 }
106
107 wxPluginLibrary *wxPluginLibrary::RefLib()
108 {
109 wxCHECK_MSG( m_linkcount > 0, NULL,
110 _T("Library had been already deleted!") );
111
112 ++m_linkcount;
113 return this;
114 }
115
116 bool wxPluginLibrary::UnrefLib()
117 {
118 wxASSERT_MSG( m_objcount == 0,
119 _T("Library unloaded before all objects were destroyed") );
120
121 if ( --m_linkcount == 0 )
122 {
123 delete this;
124 return TRUE;
125 }
126
127 return FALSE;
128 }
129
130 // ------------------------
131 // Private methods
132 // ------------------------
133
134 void wxPluginLibrary::RegisterModules()
135 {
136 // Plugin libraries might have wxModules, Register and initialise them if
137 // they do.
138 //
139 // Note that these classes are NOT included in the reference counting since
140 // it's implicit that they will be unloaded if and when the last handle to
141 // the library is. We do have to keep a copy of the module's pointer
142 // though, as there is currently no way to Unregister it without it.
143
144 wxASSERT_MSG( m_linkcount == 1,
145 _T("RegisterModules should only be called for the first load") );
146
147 for ( wxClassInfo *info = m_after; info != m_before; info = info->m_next)
148 {
149 if( info->IsKindOf(CLASSINFO(wxModule)) )
150 {
151 wxModule *m = wxDynamicCast(info->CreateObject(), wxModule);
152
153 wxASSERT_MSG( m, _T("wxDynamicCast of wxModule failed") );
154
155 m_wxmodules.push_back(m);
156 wxModule::RegisterModule(m);
157 }
158 }
159
160 // FIXME: Likewise this is (well was) very similar to InitializeModules()
161
162 for ( wxModuleList::iterator it = m_wxmodules.begin();
163 it != m_wxmodules.end();
164 ++it)
165 {
166 if( !(*it)->Init() )
167 {
168 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
169
170 // XXX: Watch this, a different hash implementation might break it,
171 // a good hash implementation would let us fix it though.
172
173 // The name of the game is to remove any uninitialised modules and
174 // let the dtor Exit the rest on shutdown, (which we'll initiate
175 // shortly).
176
177 wxModuleList::iterator oldNode = m_wxmodules.end();
178 do {
179 ++it;
180 if( oldNode != m_wxmodules.end() )
181 m_wxmodules.erase(oldNode);
182 wxModule::UnregisterModule( *it );
183 oldNode = it;
184 } while( it != m_wxmodules.end() );
185
186 --m_linkcount; // Flag us for deletion
187 break;
188 }
189 }
190 }
191
192 void wxPluginLibrary::UnregisterModules()
193 {
194 wxModuleList::iterator it;
195
196 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
197 (*it)->Exit();
198
199 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
200 wxModule::UnregisterModule( *it );
201
202 WX_CLEAR_LIST(wxModuleList, m_wxmodules);
203 }
204
205
206 // ---------------------------------------------------------------------------
207 // wxPluginManager
208 // ---------------------------------------------------------------------------
209
210 wxDLManifest* wxPluginManager::ms_manifest = NULL;
211
212 // ------------------------
213 // Static accessors
214 // ------------------------
215
216 wxPluginLibrary *
217 wxPluginManager::LoadLibrary(const wxString &libname, int flags)
218 {
219 wxString realname(libname);
220
221 if( !(flags & wxDL_VERBATIM) )
222 realname += wxDynamicLibrary::GetDllExt();
223
224 wxPluginLibrary *entry;
225
226 if ( flags & wxDL_NOSHARE )
227 {
228 entry = NULL;
229 }
230 else
231 {
232 entry = FindByName(realname);
233 }
234
235 if ( entry )
236 {
237 wxLogTrace(_T("dll"),
238 _T("LoadLibrary(%s): already loaded."), realname.c_str());
239
240 entry->RefLib();
241 }
242 else
243 {
244 entry = new wxPluginLibrary( libname, flags );
245
246 if ( entry->IsLoaded() )
247 {
248 (*ms_manifest)[realname] = entry;
249
250 wxLogTrace(_T("dll"),
251 _T("LoadLibrary(%s): loaded ok."), realname.c_str());
252
253 }
254 else
255 {
256 wxLogTrace(_T("dll"),
257 _T("LoadLibrary(%s): failed to load."), realname.c_str());
258
259 // we have created entry just above
260 if ( !entry->UnrefLib() )
261 {
262 // ... so UnrefLib() is supposed to delete it
263 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
264 }
265
266 entry = NULL;
267 }
268 }
269
270 return entry;
271 }
272
273 bool wxPluginManager::UnloadLibrary(const wxString& libname)
274 {
275 wxString realname = libname;
276
277 wxPluginLibrary *entry = FindByName(realname);
278
279 if ( !entry )
280 {
281 realname += wxDynamicLibrary::GetDllExt();
282
283 entry = FindByName(realname);
284 }
285
286 if ( !entry )
287 {
288 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
289 libname.c_str());
290
291 return FALSE;
292 }
293
294 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname.c_str());
295
296 if ( !entry->UnrefLib() )
297 {
298 // not really unloaded yet
299 return FALSE;
300 }
301
302 ms_manifest->erase(ms_manifest->find(realname));
303
304 return TRUE;
305 }
306
307 // ------------------------
308 // Class implementation
309 // ------------------------
310
311 bool wxPluginManager::Load(const wxString &libname, int flags)
312 {
313 m_entry = wxPluginManager::LoadLibrary(libname, flags);
314
315 return IsLoaded();
316 }
317
318 void wxPluginManager::Unload()
319 {
320 wxCHECK_RET( m_entry, _T("unloading an invalid wxPluginManager?") );
321
322 for ( wxDLManifest::iterator i = ms_manifest->begin();
323 i != ms_manifest->end();
324 ++i )
325 {
326 if ( i->second == m_entry )
327 {
328 ms_manifest->erase(i);
329 break;
330 }
331 }
332
333 m_entry->UnrefLib();
334
335 m_entry = NULL;
336 }
337
338
339
340 #if WXWIN_COMPATIBILITY_2_2
341
342 wxPluginLibrary *wxPluginManager::GetObjectFromHandle(wxDllType handle)
343 {
344 for ( wxDLManifest::iterator i = ms_manifest->begin();
345 i != ms_manifest->end();
346 ++i )
347 {
348 wxPluginLibrary * const lib = i->second;
349
350 if ( lib->GetLibHandle() == handle )
351 return lib;
352 }
353
354 return NULL;
355 }
356
357 // ---------------------------------------------------------------------------
358 // wxDllLoader (all these methods are static)
359 // ---------------------------------------------------------------------------
360
361
362 wxDllType wxDllLoader::LoadLibrary(const wxString &name, bool *success)
363 {
364 wxPluginLibrary *p = wxPluginManager::LoadLibrary
365 (
366 name,
367 wxDL_DEFAULT | wxDL_VERBATIM | wxDL_NOSHARE
368 );
369
370 if ( success )
371 *success = p != NULL;
372
373 return p ? p->GetLibHandle() : 0;
374 }
375
376 void wxDllLoader::UnloadLibrary(wxDllType handle)
377 {
378 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(handle);
379
380 wxCHECK_RET( p, _T("Unloading a library not loaded with wxDllLoader?") );
381
382 p->UnrefLib();
383 }
384
385 void *
386 wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name, bool *success)
387 {
388 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(dllHandle);
389
390 if ( !p )
391 {
392 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
393
394 if ( success )
395 *success = FALSE;
396
397 return NULL;
398 }
399
400 return p->GetSymbol(name, success);
401 }
402
403
404 // ---------------------------------------------------------------------------
405 // Global variables
406 // ---------------------------------------------------------------------------
407
408 wxLibraries wxTheLibraries;
409
410 // ============================================================================
411 // implementation
412 // ============================================================================
413
414 // construct the full name from the base shared object name: adds a .dll
415 // suffix under Windows or .so under Unix
416 static wxString ConstructLibraryName(const wxString& basename)
417 {
418 wxString fullname;
419 fullname << basename << wxDllLoader::GetDllExt();
420
421 return fullname;
422 }
423
424 // ---------------------------------------------------------------------------
425 // wxLibrary (one instance per dynamic library)
426 // ---------------------------------------------------------------------------
427
428 wxLibrary::wxLibrary(wxDllType handle)
429 {
430 typedef wxClassInfo *(*t_get_first)(void);
431 t_get_first get_first;
432
433 m_handle = handle;
434
435 // Some system may use a local heap for library.
436 get_first = (t_get_first)GetSymbol(_T("wxGetClassFirst"));
437 // It is a wxWindows DLL.
438 if (get_first)
439 PrepareClasses(get_first());
440 }
441
442 wxLibrary::~wxLibrary()
443 {
444 if ( m_handle )
445 {
446 wxDllLoader::UnloadLibrary(m_handle);
447 }
448 }
449
450 wxObject *wxLibrary::CreateObject(const wxString& name)
451 {
452 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
453
454 if (!info)
455 return NULL;
456
457 return info->CreateObject();
458 }
459
460 void wxLibrary::PrepareClasses(wxClassInfo *first)
461 {
462 // Index all class infos by their class name
463 wxClassInfo *info = first;
464 while (info)
465 {
466 if (info->m_className)
467 classTable.Put(info->m_className, (wxObject *)info);
468 info = info->m_next;
469 }
470
471 #if !wxUSE_EXTENDED_RTTI
472 // Set base pointers for each wxClassInfo
473 info = first;
474 while (info)
475 {
476 if (info->GetBaseClassName1())
477 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
478 if (info->GetBaseClassName2())
479 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
480 info = info->m_next;
481 }
482 #endif
483 }
484
485 void *wxLibrary::GetSymbol(const wxString& symbname)
486 {
487 return wxDllLoader::GetSymbol(m_handle, symbname);
488 }
489
490
491 // ---------------------------------------------------------------------------
492 // wxLibraries (only one instance should normally exist)
493 // ---------------------------------------------------------------------------
494
495 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING)
496 {
497 }
498
499 wxLibraries::~wxLibraries()
500 {
501 wxNode *node = m_loaded.First();
502
503 while (node) {
504 wxLibrary *lib = (wxLibrary *)node->Data();
505 delete lib;
506
507 node = node->Next();
508 }
509 }
510
511 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
512 {
513 wxLibrary *lib;
514 wxClassInfo *old_sm_first;
515 wxNode *node = m_loaded.Find(name.GetData());
516
517 if (node != NULL)
518 return ((wxLibrary *)node->Data());
519
520 // If DLL shares data, this is necessary.
521 old_sm_first = wxClassInfo::sm_first;
522 wxClassInfo::sm_first = NULL;
523
524 wxString libname = ConstructLibraryName(name);
525
526 bool success = FALSE;
527 wxDllType handle = wxDllLoader::LoadLibrary(libname, &success);
528 if(success)
529 {
530 lib = new wxLibrary(handle);
531 wxClassInfo::sm_first = old_sm_first;
532
533 m_loaded.Append(name.GetData(), lib);
534 }
535 else
536 lib = NULL;
537 return lib;
538 }
539
540 wxObject *wxLibraries::CreateObject(const wxString& path)
541 {
542 wxNode *node = m_loaded.First();
543 wxObject *obj;
544
545 while (node) {
546 obj = ((wxLibrary *)node->Data())->CreateObject(path);
547 if (obj)
548 return obj;
549
550 node = node->Next();
551 }
552 return NULL;
553 }
554
555 #endif // WXWIN_COMPATIBILITY_2_2
556
557
558 #endif // wxUSE_DYNAMIC_LOADER
559