]> git.saurik.com Git - wxWidgets.git/blame - src/common/module.cpp
don't do anything before including the PCH header
[wxWidgets.git] / src / common / module.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
32d4c30a 2// Name: src/common/module.cpp
c801d85f
KB
3// Purpose: Modules initialization/destruction
4// Author: Wolfram Gloger/adapted by Guilhem Lavaux
5// Modified by:
6// Created: 04/11/98
7// RCS-ID: $Id$
8// Copyright: (c) Wolfram Gloger and Guilhem Lavaux
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
c801d85f
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
32d4c30a
WS
16 #pragma hdrstop
17#endif
18
88a7a4e1
WS
19#include "wx/module.h"
20
32d4c30a
WS
21#ifndef WX_PRECOMP
22 #include "wx/hash.h"
88a7a4e1 23 #include "wx/intl.h"
e4db172a 24 #include "wx/log.h"
c801d85f
KB
25#endif
26
642c0eda 27#include "wx/listimpl.cpp"
8aa4edd2 28
3b96fc2f
MB
29#define TRACE_MODULE _T("module")
30
4115960d 31WX_DEFINE_LIST(wxModuleList)
c801d85f
KB
32
33IMPLEMENT_CLASS(wxModule, wxObject)
34
8aa4edd2 35wxModuleList wxModule::m_modules;
c801d85f
KB
36
37void wxModule::RegisterModule(wxModule* module)
38{
af266e5b 39 module->m_state = State_Registered;
8aa4edd2 40 m_modules.Append(module);
c801d85f
KB
41}
42
0b9ab0bd
RL
43void wxModule::UnregisterModule(wxModule* module)
44{
45 m_modules.DeleteObject(module);
df5168c4 46 delete module;
0b9ab0bd
RL
47}
48
c801d85f
KB
49// Collect up all module-derived classes, create an instance of each,
50// and register them.
8aa4edd2 51void wxModule::RegisterModules()
c801d85f 52{
644cb537
MB
53 for (wxClassInfo::const_iterator it = wxClassInfo::begin_classinfo(),
54 end = wxClassInfo::end_classinfo();
55 it != end; ++it)
c801d85f 56 {
644cb537
MB
57 const wxClassInfo* classInfo = *it;
58
8aa4edd2 59 if ( classInfo->IsKindOf(CLASSINFO(wxModule)) &&
644cb537 60 (classInfo != (& (wxModule::ms_classInfo))) )
c801d85f 61 {
3b96fc2f
MB
62 wxLogTrace(TRACE_MODULE, wxT("Registering module %s"),
63 classInfo->GetClassName());
8aa4edd2 64 wxModule* module = (wxModule *)classInfo->CreateObject();
644cb537 65 wxModule::RegisterModule(module);
c801d85f 66 }
c801d85f 67 }
c801d85f
KB
68}
69
af266e5b
VZ
70bool wxModule::DoInitializeModule(wxModule *module,
71 wxModuleList &initializedModules)
c801d85f 72{
af266e5b 73 if ( module->m_state == State_Initializing )
c801d85f 74 {
af266e5b
VZ
75 wxLogError(_("Circular dependency involving module \"%s\" detected."),
76 module->GetClassInfo()->GetClassName());
77 return false;
78 }
79
80 module->m_state = State_Initializing;
81
82 const wxArrayClassInfo& dependencies = module->m_dependencies;
83
84 // satisfy module dependencies by loading them before the current module
85 for ( unsigned int i = 0; i < dependencies.size(); ++i )
86 {
87 wxClassInfo * cinfo = dependencies[i];
88
89 // Check if the module is already initialized
90 wxModuleList::compatibility_iterator node;
91 for ( node = initializedModules.GetFirst(); node; node = node->GetNext() )
8aa4edd2 92 {
af266e5b
VZ
93 if ( node->GetData()->GetClassInfo() == cinfo )
94 break;
95 }
96
97 if ( node )
98 {
99 // this dependency is already initialized, nothing to do
100 continue;
101 }
8c18c674 102
af266e5b
VZ
103 // find the module in the registered modules list
104 for ( node = m_modules.GetFirst(); node; node = node->GetNext() )
105 {
106 wxModule *moduleDep = node->GetData();
107 if ( moduleDep->GetClassInfo() == cinfo )
8aa4edd2 108 {
af266e5b
VZ
109 if ( !DoInitializeModule(moduleDep, initializedModules ) )
110 {
111 // failed to initialize a dependency, so fail this one too
112 return false;
113 }
114
115 break;
8aa4edd2 116 }
af266e5b 117 }
8aa4edd2 118
af266e5b
VZ
119 if ( !node )
120 {
121 wxLogError(_("Dependency \"%s\" of module \"%s\" doesn't exist."),
122 cinfo->GetClassName(),
123 module->GetClassInfo()->GetClassName());
4e32eea1 124 return false;
8aa4edd2 125 }
c801d85f 126 }
8aa4edd2 127
af266e5b
VZ
128 if ( !module->Init() )
129 {
130 wxLogError(_("Module \"%s\" initialization failed"),
131 module->GetClassInfo()->GetClassName());
132 return false;
133 }
134
135 wxLogTrace(TRACE_MODULE, wxT("Module \"%s\" initialized"),
136 module->GetClassInfo()->GetClassName());
137
138 module->m_state = State_Initialized;
139 initializedModules.Append(module);
140
4e32eea1 141 return true;
c801d85f
KB
142}
143
af266e5b
VZ
144// Initialize user-defined modules
145bool wxModule::InitializeModules()
c801d85f 146{
af266e5b
VZ
147 wxModuleList initializedModules;
148
149 for ( wxModuleList::compatibility_iterator node = m_modules.GetFirst();
150 node;
151 node = node->GetNext() )
152 {
153 wxModule *module = node->GetData();
154
155 // the module could have been already initialized as dependency of
156 // another one
157 if ( module->m_state == State_Registered )
158 {
159 if ( !DoInitializeModule( module, initializedModules ) )
160 {
161 // failed to initialize all modules, so clean up the already
162 // initialized ones
163 DoCleanUpModules(initializedModules);
164
165 return false;
166 }
167 }
168 }
169
170 // remember the real initialisation order
171 m_modules = initializedModules;
172
173 return true;
174}
175
176// Clean up all currently initialized modules
177void wxModule::DoCleanUpModules(const wxModuleList& modules)
178{
179 // cleanup user-defined modules in the reverse order compared to their
180 // initialization -- this ensures that dependencies are respected
181 for ( wxModuleList::compatibility_iterator node = modules.GetLast();
182 node;
183 node = node->GetPrevious() )
c801d85f 184 {
3b96fc2f
MB
185 wxLogTrace(TRACE_MODULE, wxT("Cleanup module %s"),
186 node->GetData()->GetClassInfo()->GetClassName());
af266e5b
VZ
187
188 wxModule * module = node->GetData();
189
190 wxASSERT_MSG( module->m_state == State_Initialized,
191 _T("not initialized module being cleaned up") );
192
193 module->Exit();
194 module->m_state = State_Registered;
c801d85f 195 }
8aa4edd2 196
af266e5b 197 // clear all modules, even the non-initialized ones
df5168c4 198 WX_CLEAR_LIST(wxModuleList, m_modules);
c801d85f 199}