]>
git.saurik.com Git - wxWidgets.git/blob - src/common/dynload.cpp
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
);
362 #endif // wxUSE_DYNAMIC_LOADER