1 /////////////////////////////////////////////////////////////////////////////
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)
9 // Copyright: (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 #include "wx/wxprec.h"
23 #if wxUSE_DYNAMIC_LOADER
26 #include "wx/msw/private.h"
36 #include "wx/strconv.h"
38 #include "wx/dynload.h"
39 #include "wx/module.h"
42 // ---------------------------------------------------------------------------
44 // ---------------------------------------------------------------------------
47 wxDLImports
* wxPluginLibrary::ms_classes
= NULL
;
49 class wxPluginLibraryModule
: public wxModule
52 wxPluginLibraryModule() { }
54 // TODO: create ms_classes on demand, why always preallocate it?
57 wxPluginLibrary::ms_classes
= new wxDLImports
;
58 wxPluginManager::CreateManifest();
64 delete wxPluginLibrary::ms_classes
;
65 wxPluginLibrary::ms_classes
= NULL
;
66 wxPluginManager::ClearManifest();
70 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule
)
73 IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule
, wxModule
)
76 wxPluginLibrary::wxPluginLibrary(const wxString
&libname
, int flags
)
80 m_before
= wxClassInfo::sm_first
;
81 Load( libname
, flags
);
82 m_after
= wxClassInfo::sm_first
;
91 // Flag us for deletion
96 wxPluginLibrary::~wxPluginLibrary()
105 wxPluginLibrary
*wxPluginLibrary::RefLib()
107 wxCHECK_MSG( m_linkcount
> 0, NULL
,
108 _T("Library had been already deleted!") );
114 bool wxPluginLibrary::UnrefLib()
116 wxASSERT_MSG( m_objcount
== 0,
117 _T("Library unloaded before all objects were destroyed") );
119 if ( m_linkcount
== 0 || --m_linkcount
== 0 )
128 // ------------------------
130 // ------------------------
132 void wxPluginLibrary::UpdateClasses()
134 for (wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->m_next
)
136 if( info
->GetClassName() )
138 // Hash all the class names into a local table too so
139 // we can quickly find the entry they correspond to.
140 (*ms_classes
)[info
->GetClassName()] = this;
145 void wxPluginLibrary::RestoreClasses()
147 // Check if there is a need to restore classes.
151 for(wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->m_next
)
153 ms_classes
->erase(ms_classes
->find(info
->GetClassName()));
157 void wxPluginLibrary::RegisterModules()
159 // Plugin libraries might have wxModules, Register and initialise them if
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.
167 wxASSERT_MSG( m_linkcount
== 1,
168 _T("RegisterModules should only be called for the first load") );
170 for ( wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->m_next
)
172 if( info
->IsKindOf(CLASSINFO(wxModule
)) )
174 wxModule
*m
= wxDynamicCast(info
->CreateObject(), wxModule
);
176 wxASSERT_MSG( m
, _T("wxDynamicCast of wxModule failed") );
178 m_wxmodules
.push_back(m
);
179 wxModule::RegisterModule(m
);
183 // FIXME: Likewise this is (well was) very similar to InitializeModules()
185 for ( wxModuleList::iterator it
= m_wxmodules
.begin();
186 it
!= m_wxmodules
.end();
191 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
193 // XXX: Watch this, a different hash implementation might break it,
194 // a good hash implementation would let us fix it though.
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
200 wxModuleList::iterator oldNode
= m_wxmodules
.end();
203 if( oldNode
!= m_wxmodules
.end() )
204 m_wxmodules
.erase(oldNode
);
205 wxModule::UnregisterModule( *it
);
207 } while( it
!= m_wxmodules
.end() );
209 --m_linkcount
; // Flag us for deletion
215 void wxPluginLibrary::UnregisterModules()
217 wxModuleList::iterator it
;
219 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
222 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
223 wxModule::UnregisterModule( *it
);
225 // NB: content of the list was deleted by UnregisterModule calls above:
230 // ---------------------------------------------------------------------------
232 // ---------------------------------------------------------------------------
234 wxDLManifest
* wxPluginManager::ms_manifest
= NULL
;
236 // ------------------------
238 // ------------------------
241 wxPluginManager::LoadLibrary(const wxString
&libname
, int flags
)
243 wxString
realname(libname
);
245 if( !(flags
& wxDL_VERBATIM
) )
246 realname
+= wxDynamicLibrary::GetDllExt();
248 wxPluginLibrary
*entry
;
250 if ( flags
& wxDL_NOSHARE
)
256 entry
= FindByName(realname
);
261 wxLogTrace(_T("dll"),
262 _T("LoadLibrary(%s): already loaded."), realname
.c_str());
268 entry
= new wxPluginLibrary( libname
, flags
);
270 if ( entry
->IsLoaded() )
272 (*ms_manifest
)[realname
] = entry
;
274 wxLogTrace(_T("dll"),
275 _T("LoadLibrary(%s): loaded ok."), realname
.c_str());
280 wxLogTrace(_T("dll"),
281 _T("LoadLibrary(%s): failed to load."), realname
.c_str());
283 // we have created entry just above
284 if ( !entry
->UnrefLib() )
286 // ... so UnrefLib() is supposed to delete it
287 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
297 bool wxPluginManager::UnloadLibrary(const wxString
& libname
)
299 wxString realname
= libname
;
301 wxPluginLibrary
*entry
= FindByName(realname
);
305 realname
+= wxDynamicLibrary::GetDllExt();
307 entry
= FindByName(realname
);
312 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
318 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname
.c_str());
320 if ( !entry
->UnrefLib() )
322 // not really unloaded yet
326 ms_manifest
->erase(ms_manifest
->find(realname
));
331 // ------------------------
332 // Class implementation
333 // ------------------------
335 bool wxPluginManager::Load(const wxString
&libname
, int flags
)
337 m_entry
= wxPluginManager::LoadLibrary(libname
, flags
);
342 void wxPluginManager::Unload()
344 wxCHECK_RET( m_entry
, _T("unloading an invalid wxPluginManager?") );
346 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
347 i
!= ms_manifest
->end();
350 if ( i
->second
== m_entry
)
352 ms_manifest
->erase(i
);
364 #if WXWIN_COMPATIBILITY_2_2
366 wxPluginLibrary
*wxPluginManager::GetObjectFromHandle(wxDllType handle
)
368 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
369 i
!= ms_manifest
->end();
372 wxPluginLibrary
* const lib
= i
->second
;
374 if ( lib
->GetLibHandle() == handle
)
381 // ---------------------------------------------------------------------------
382 // wxDllLoader (all these methods are static)
383 // ---------------------------------------------------------------------------
386 wxDllType
wxDllLoader::LoadLibrary(const wxString
&name
, bool *success
)
388 wxPluginLibrary
*p
= wxPluginManager::LoadLibrary
391 wxDL_DEFAULT
| wxDL_VERBATIM
| wxDL_NOSHARE
395 *success
= p
!= NULL
;
397 return p
? p
->GetLibHandle() : 0;
400 void wxDllLoader::UnloadLibrary(wxDllType handle
)
402 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(handle
);
404 wxCHECK_RET( p
, _T("Unloading a library not loaded with wxDllLoader?") );
410 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
)
412 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(dllHandle
);
416 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
424 return p
->GetSymbol(name
, success
);
428 // ---------------------------------------------------------------------------
430 // ---------------------------------------------------------------------------
432 wxLibraries wxTheLibraries
;
434 // ============================================================================
436 // ============================================================================
438 // construct the full name from the base shared object name: adds a .dll
439 // suffix under Windows or .so under Unix
440 static wxString
ConstructLibraryName(const wxString
& basename
)
443 fullname
<< basename
<< wxDllLoader::GetDllExt();
448 // ---------------------------------------------------------------------------
449 // wxLibrary (one instance per dynamic library)
450 // ---------------------------------------------------------------------------
452 wxLibrary::wxLibrary(wxDllType handle
)
454 typedef wxClassInfo
*(*t_get_first
)(void);
455 t_get_first get_first
;
459 // Some system may use a local heap for library.
460 get_first
= (t_get_first
)GetSymbol(_T("wxGetClassFirst"));
461 // It is a wxWidgets DLL.
463 PrepareClasses(get_first());
466 wxLibrary::~wxLibrary()
470 wxDllLoader::UnloadLibrary(m_handle
);
474 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
476 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
481 return info
->CreateObject();
484 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
486 // Index all class infos by their class name
487 wxClassInfo
*info
= first
;
490 if (info
->m_className
)
491 classTable
.Put(info
->m_className
, (wxObject
*)info
);
495 #if !wxUSE_EXTENDED_RTTI
496 // Set base pointers for each wxClassInfo
500 if (info
->GetBaseClassName1())
501 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
502 if (info
->GetBaseClassName2())
503 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
509 void *wxLibrary::GetSymbol(const wxString
& symbname
)
511 return wxDllLoader::GetSymbol(m_handle
, symbname
);
515 // ---------------------------------------------------------------------------
516 // wxLibraries (only one instance should normally exist)
517 // ---------------------------------------------------------------------------
519 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING
)
523 wxLibraries::~wxLibraries()
525 wxNode
*node
= m_loaded
.GetFirst();
528 wxLibrary
*lib
= (wxLibrary
*)node
->GetData();
531 node
= node
->GetNext();
535 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
538 wxClassInfo
*old_sm_first
;
539 wxNode
*node
= m_loaded
.Find(name
.GetData());
542 return ((wxLibrary
*)node
->GetData());
544 // If DLL shares data, this is necessary.
545 old_sm_first
= wxClassInfo::sm_first
;
546 wxClassInfo::sm_first
= NULL
;
548 wxString libname
= ConstructLibraryName(name
);
550 bool success
= false;
551 wxDllType handle
= wxDllLoader::LoadLibrary(libname
, &success
);
554 lib
= new wxLibrary(handle
);
555 wxClassInfo::sm_first
= old_sm_first
;
557 m_loaded
.Append(name
.GetData(), lib
);
564 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
566 wxNode
*node
= m_loaded
.GetFirst();
570 obj
= ((wxLibrary
*)node
->GetData())->CreateObject(path
);
574 node
= node
->GetNext();
579 #endif // WXWIN_COMPATIBILITY_2_2
582 #endif // wxUSE_DYNAMIC_LOADER