1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/module.cpp
3 // Purpose: Modules initialization/destruction
4 // Author: Wolfram Gloger/adapted by Guilhem Lavaux
8 // Copyright: (c) Wolfram Gloger and Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/module.h"
27 #include "wx/listimpl.cpp"
29 #define TRACE_MODULE _T("module")
31 WX_DEFINE_LIST(wxModuleList
)
33 IMPLEMENT_CLASS(wxModule
, wxObject
)
35 wxModuleList
wxModule::m_modules
;
37 void wxModule::RegisterModule(wxModule
* module)
39 module->m_state
= State_Registered
;
40 m_modules
.Append(module);
43 void wxModule::UnregisterModule(wxModule
* module)
45 m_modules
.DeleteObject(module);
49 // Collect up all module-derived classes, create an instance of each,
51 void wxModule::RegisterModules()
53 for (wxClassInfo::const_iterator it
= wxClassInfo::begin_classinfo(),
54 end
= wxClassInfo::end_classinfo();
57 const wxClassInfo
* classInfo
= *it
;
59 if ( classInfo
->IsKindOf(CLASSINFO(wxModule
)) &&
60 (classInfo
!= (& (wxModule::ms_classInfo
))) )
62 wxLogTrace(TRACE_MODULE
, wxT("Registering module %s"),
63 classInfo
->GetClassName());
64 wxModule
* module = (wxModule
*)classInfo
->CreateObject();
65 wxModule::RegisterModule(module);
70 bool wxModule::DoInitializeModule(wxModule
*module,
71 wxModuleList
&initializedModules
)
73 if ( module->m_state
== State_Initializing
)
75 wxLogError(_("Circular dependency involving module \"%s\" detected."),
76 module->GetClassInfo()->GetClassName());
80 module->m_state
= State_Initializing
;
82 // translate named dependencies to the normal ones first
83 if ( !module->ResolveNamedDependencies() )
86 const wxArrayClassInfo
& dependencies
= module->m_dependencies
;
88 // satisfy module dependencies by loading them before the current module
89 for ( unsigned int i
= 0; i
< dependencies
.size(); ++i
)
91 wxClassInfo
* cinfo
= dependencies
[i
];
93 // Check if the module is already initialized
94 wxModuleList::compatibility_iterator node
;
95 for ( node
= initializedModules
.GetFirst(); node
; node
= node
->GetNext() )
97 if ( node
->GetData()->GetClassInfo() == cinfo
)
103 // this dependency is already initialized, nothing to do
107 // find the module in the registered modules list
108 for ( node
= m_modules
.GetFirst(); node
; node
= node
->GetNext() )
110 wxModule
*moduleDep
= node
->GetData();
111 if ( moduleDep
->GetClassInfo() == cinfo
)
113 if ( !DoInitializeModule(moduleDep
, initializedModules
) )
115 // failed to initialize a dependency, so fail this one too
125 wxLogError(_("Dependency \"%s\" of module \"%s\" doesn't exist."),
126 cinfo
->GetClassName(),
127 module->GetClassInfo()->GetClassName());
132 if ( !module->Init() )
134 wxLogError(_("Module \"%s\" initialization failed"),
135 module->GetClassInfo()->GetClassName());
139 wxLogTrace(TRACE_MODULE
, wxT("Module \"%s\" initialized"),
140 module->GetClassInfo()->GetClassName());
142 module->m_state
= State_Initialized
;
143 initializedModules
.Append(module);
148 // Initialize user-defined modules
149 bool wxModule::InitializeModules()
151 wxModuleList initializedModules
;
153 for ( wxModuleList::compatibility_iterator node
= m_modules
.GetFirst();
155 node
= node
->GetNext() )
157 wxModule
*module = node
->GetData();
159 // the module could have been already initialized as dependency of
161 if ( module->m_state
== State_Registered
)
163 if ( !DoInitializeModule( module, initializedModules
) )
165 // failed to initialize all modules, so clean up the already
167 DoCleanUpModules(initializedModules
);
174 // remember the real initialisation order
175 m_modules
= initializedModules
;
180 // Clean up all currently initialized modules
181 void wxModule::DoCleanUpModules(const wxModuleList
& modules
)
183 // cleanup user-defined modules in the reverse order compared to their
184 // initialization -- this ensures that dependencies are respected
185 for ( wxModuleList::compatibility_iterator node
= modules
.GetLast();
187 node
= node
->GetPrevious() )
189 wxLogTrace(TRACE_MODULE
, wxT("Cleanup module %s"),
190 node
->GetData()->GetClassInfo()->GetClassName());
192 wxModule
* module = node
->GetData();
194 wxASSERT_MSG( module->m_state
== State_Initialized
,
195 _T("not initialized module being cleaned up") );
198 module->m_state
= State_Registered
;
201 // clear all modules, even the non-initialized ones
202 WX_CLEAR_LIST(wxModuleList
, m_modules
);
205 bool wxModule::ResolveNamedDependencies()
207 // first resolve required dependencies
208 for ( size_t i
= 0; i
< m_namedDependencies
.size(); ++i
)
210 wxClassInfo
*info
= wxClassInfo::FindClass(m_namedDependencies
[i
]);
214 // required dependency not found
218 // add it even if it is not derived from wxModule because
219 // DoInitializeModule() will make sure a module with the same class
220 // info exists and fail if it doesn't
221 m_dependencies
.Add(info
);