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