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