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