]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynload.cpp
fix for always terminating intermediate UniChar String for 4 bytes wchar_t
[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>
55d99c7a 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
VZ
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 }
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
123 if ( --m_linkcount == 0 )
7c1e2b44
RL
124 {
125 delete this;
126 return TRUE;
127 }
d0fcccc4 128
7c1e2b44
RL
129 return FALSE;
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{
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
4f89dbc4 157void wxPluginLibrary::RegisterModules()
0b9ab0bd
RL
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
7c1e2b44 167 wxASSERT_MSG( m_linkcount == 1,
0b9ab0bd
RL
168 _T("RegisterModules should only be called for the first load") );
169
6eccc0c4 170 for ( wxClassInfo *info = m_after; info != m_before; info = info->m_next)
0b9ab0bd
RL
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
df5168c4 178 m_wxmodules.push_back(m);
0b9ab0bd
RL
179 wxModule::RegisterModule(m);
180 }
181 }
182
183 // FIXME: Likewise this is (well was) very similar to InitializeModules()
184
df5168c4
MB
185 for ( wxModuleList::iterator it = m_wxmodules.begin();
186 it != m_wxmodules.end();
187 ++it)
0b9ab0bd 188 {
df5168c4 189 if( !(*it)->Init() )
0b9ab0bd 190 {
4f89dbc4 191 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
0b9ab0bd
RL
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
df5168c4 200 wxModuleList::iterator oldNode = m_wxmodules.end();
0b9ab0bd 201 do {
df5168c4
MB
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() );
0b9ab0bd 208
7c1e2b44 209 --m_linkcount; // Flag us for deletion
0b9ab0bd
RL
210 break;
211 }
212 }
213}
214
4f89dbc4 215void wxPluginLibrary::UnregisterModules()
0b9ab0bd 216{
df5168c4 217 wxModuleList::iterator it;
0b9ab0bd 218
df5168c4
MB
219 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
220 (*it)->Exit();
0b9ab0bd 221
df5168c4
MB
222 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
223 wxModule::UnregisterModule( *it );
0b9ab0bd 224
df5168c4 225 WX_CLEAR_LIST(wxModuleList, m_wxmodules);
0b9ab0bd
RL
226}
227
228
229// ---------------------------------------------------------------------------
d0fcccc4 230// wxPluginManager
0b9ab0bd
RL
231// ---------------------------------------------------------------------------
232
dabd1377 233wxDLManifest* wxPluginManager::ms_manifest = NULL;
0b9ab0bd
RL
234
235// ------------------------
236// Static accessors
237// ------------------------
238
d0fcccc4
VZ
239wxPluginLibrary *
240wxPluginManager::LoadLibrary(const wxString &libname, int flags)
0b9ab0bd 241{
4f89dbc4 242 wxString realname(libname);
0b9ab0bd 243
4f89dbc4
RL
244 if( !(flags & wxDL_VERBATIM) )
245 realname += wxDynamicLibrary::GetDllExt();
246
d0fcccc4
VZ
247 wxPluginLibrary *entry;
248
249 if ( flags & wxDL_NOSHARE )
250 {
251 entry = NULL;
252 }
253 else
254 {
2b5f62a0 255 entry = FindByName(realname);
d0fcccc4 256 }
4f89dbc4 257
d0fcccc4 258 if ( entry )
0b9ab0bd 259 {
d0fcccc4
VZ
260 wxLogTrace(_T("dll"),
261 _T("LoadLibrary(%s): already loaded."), realname.c_str());
262
7c1e2b44 263 entry->RefLib();
0b9ab0bd
RL
264 }
265 else
266 {
4f89dbc4 267 entry = new wxPluginLibrary( libname, flags );
0b9ab0bd 268
d0fcccc4 269 if ( entry->IsLoaded() )
0b9ab0bd 270 {
2b5f62a0 271 (*ms_manifest)[realname] = entry;
d0fcccc4
VZ
272
273 wxLogTrace(_T("dll"),
274 _T("LoadLibrary(%s): loaded ok."), realname.c_str());
275
0b9ab0bd
RL
276 }
277 else
278 {
d0fcccc4
VZ
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;
0b9ab0bd
RL
290 }
291 }
d0fcccc4 292
0b9ab0bd
RL
293 return entry;
294}
295
d0fcccc4 296bool wxPluginManager::UnloadLibrary(const wxString& libname)
0b9ab0bd 297{
d0fcccc4 298 wxString realname = libname;
4f89dbc4 299
2b5f62a0 300 wxPluginLibrary *entry = FindByName(realname);
0b9ab0bd 301
d0fcccc4
VZ
302 if ( !entry )
303 {
304 realname += wxDynamicLibrary::GetDllExt();
0b9ab0bd 305
2b5f62a0 306 entry = FindByName(realname);
d0fcccc4
VZ
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
2b5f62a0 325 ms_manifest->erase(ms_manifest->find(realname));
d0fcccc4
VZ
326
327 return TRUE;
4f89dbc4
RL
328}
329
0b9ab0bd
RL
330// ------------------------
331// Class implementation
332// ------------------------
333
23213f18 334bool wxPluginManager::Load(const wxString &libname, int flags)
0b9ab0bd 335{
4f89dbc4 336 m_entry = wxPluginManager::LoadLibrary(libname, flags);
2b5f62a0 337
4f89dbc4 338 return IsLoaded();
0b9ab0bd
RL
339}
340
4f89dbc4 341void wxPluginManager::Unload()
0b9ab0bd 342{
2b5f62a0 343 wxCHECK_RET( m_entry, _T("unloading an invalid wxPluginManager?") );
0b9ab0bd 344
2b5f62a0
VZ
345 for ( wxDLManifest::iterator i = ms_manifest->begin();
346 i != ms_manifest->end();
347 ++i )
4f89dbc4 348 {
2b5f62a0
VZ
349 if ( i->second == m_entry )
350 {
351 ms_manifest->erase(i);
46113053 352 break;
2b5f62a0 353 }
4f89dbc4 354 }
2b5f62a0
VZ
355
356 m_entry->UnrefLib();
357
358 m_entry = NULL;
0b9ab0bd
RL
359}
360
1948bb32
VS
361
362
363#if WXWIN_COMPATIBILITY_2_2
364
365wxPluginLibrary *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
4f89dbc4
RL
380// ---------------------------------------------------------------------------
381// wxDllLoader (all these methods are static)
382// ---------------------------------------------------------------------------
383
4f89dbc4 384
3d7fa4e6 385wxDllType wxDllLoader::LoadLibrary(const wxString &name, bool *success)
4f89dbc4 386{
d0fcccc4
VZ
387 wxPluginLibrary *p = wxPluginManager::LoadLibrary
388 (
389 name,
390 wxDL_DEFAULT | wxDL_VERBATIM | wxDL_NOSHARE
391 );
392
3d7fa4e6
VZ
393 if ( success )
394 *success = p != NULL;
395
d0fcccc4 396 return p ? p->GetLibHandle() : 0;
0b9ab0bd
RL
397}
398
4f89dbc4 399void wxDllLoader::UnloadLibrary(wxDllType handle)
0b9ab0bd 400{
4f89dbc4 401 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(handle);
d0fcccc4
VZ
402
403 wxCHECK_RET( p, _T("Unloading a library not loaded with wxDllLoader?") );
404
4f89dbc4 405 p->UnrefLib();
0b9ab0bd
RL
406}
407
d0fcccc4
VZ
408void *
409wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name, bool *success)
0b9ab0bd 410{
4f89dbc4 411 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(dllHandle);
d0fcccc4
VZ
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
4f89dbc4 423 return p->GetSymbol(name, success);
0b9ab0bd
RL
424}
425
1948bb32
VS
426
427// ---------------------------------------------------------------------------
428// Global variables
429// ---------------------------------------------------------------------------
430
431wxLibraries 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
439static 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
451wxLibrary::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
465wxLibrary::~wxLibrary()
466{
467 if ( m_handle )
468 {
469 wxDllLoader::UnloadLibrary(m_handle);
470 }
471}
472
473wxObject *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
483void 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
d1d738f1 494#if !wxUSE_EXTENDED_RTTI
1948bb32
VS
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 }
1bc7510a 505#endif
1948bb32
VS
506}
507
508void *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
518wxLibraries::wxLibraries():m_loaded(wxKEY_STRING)
519{
520}
521
522wxLibraries::~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
534wxLibrary *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
563wxObject *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
4f89dbc4
RL
578#endif // WXWIN_COMPATIBILITY_2_2
579
1948bb32 580
0b9ab0bd
RL
581#endif // wxUSE_DYNAMIC_LOADER
582