Get rid of wxList and wxArray in wxModule code

Simply use wxVector instead, this shouldn't be less efficient (we rarely
remove the modules from the list and iterating over a vector should
actually be faster, as well as consuming less memory), but it avoids
ugly macros, is simpler to use and to debug and will be trivial to
replace with std::vector<> in the future.

No real changes, this is just pure cleanup.
This commit is contained in:
Vadim Zeitlin
2021-03-07 20:25:49 +01:00
parent 628514bcd3
commit e10e721120
2 changed files with 47 additions and 38 deletions

View File

@@ -12,18 +12,11 @@
#define _WX_MODULE_H_ #define _WX_MODULE_H_
#include "wx/object.h" #include "wx/object.h"
#include "wx/list.h" #include "wx/vector.h"
#include "wx/arrstr.h"
#include "wx/dynarray.h"
// declare a linked list of modules class wxModule;
class WXDLLIMPEXP_FWD_BASE wxModule;
WX_DECLARE_USER_EXPORTED_LIST(wxModule, wxModuleList, WXDLLIMPEXP_BASE);
// and an array of class info objects
WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxClassInfo *, wxArrayClassInfo,
class WXDLLIMPEXP_BASE);
typedef wxVector<wxModule*> wxModuleList;
// declaring a class derived from wxModule will automatically create an // declaring a class derived from wxModule will automatically create an
// instance of this class on program startup, call its OnInit() method and call // instance of this class on program startup, call its OnInit() method and call
@@ -70,14 +63,14 @@ protected:
{ {
wxCHECK_RET( dep, wxT("NULL module dependency") ); wxCHECK_RET( dep, wxT("NULL module dependency") );
m_dependencies.Add(dep); m_dependencies.push_back(dep);
} }
// same as the version above except it will look up wxClassInfo by name on // same as the version above except it will look up wxClassInfo by name on
// its own. Note that className must be ASCII // its own. Note that className must be ASCII
void AddDependency(const char *className) void AddDependency(const char *className)
{ {
m_namedDependencies.Add(wxASCII_STR(className)); m_namedDependencies.push_back(wxASCII_STR(className));
} }
@@ -98,11 +91,12 @@ private:
// module dependencies: contains wxClassInfo pointers for all modules which // module dependencies: contains wxClassInfo pointers for all modules which
// must be initialized before this one // must be initialized before this one
typedef wxVector<wxClassInfo*> wxArrayClassInfo;
wxArrayClassInfo m_dependencies; wxArrayClassInfo m_dependencies;
// and the named dependencies: those will be resolved during run-time and // and the named dependencies: those will be resolved during run-time and
// added to m_dependencies // added to m_dependencies
wxArrayString m_namedDependencies; wxVector<wxString> m_namedDependencies;
// used internally while initializing/cleaning up modules // used internally while initializing/cleaning up modules
enum enum

View File

@@ -20,12 +20,8 @@
#include "wx/log.h" #include "wx/log.h"
#endif #endif
#include "wx/listimpl.cpp"
#define TRACE_MODULE wxT("module") #define TRACE_MODULE wxT("module")
WX_DEFINE_LIST(wxModuleList)
wxIMPLEMENT_ABSTRACT_CLASS(wxModule, wxObject) wxIMPLEMENT_ABSTRACT_CLASS(wxModule, wxObject)
wxModuleList wxModule::ms_modules; wxModuleList wxModule::ms_modules;
@@ -33,12 +29,22 @@ wxModuleList wxModule::ms_modules;
void wxModule::RegisterModule(wxModule* module) void wxModule::RegisterModule(wxModule* module)
{ {
module->m_state = State_Registered; module->m_state = State_Registered;
ms_modules.Append(module); ms_modules.push_back(module);
} }
void wxModule::UnregisterModule(wxModule* module) void wxModule::UnregisterModule(wxModule* module)
{ {
ms_modules.DeleteObject(module); for ( wxModuleList::iterator it = ms_modules.begin();
it != ms_modules.end();
++it )
{
if ( *it == module )
{
ms_modules.erase(it);
break;
}
}
delete module; delete module;
} }
@@ -87,23 +93,25 @@ bool wxModule::DoInitializeModule(wxModule *module,
wxClassInfo * cinfo = dependencies[i]; wxClassInfo * cinfo = dependencies[i];
// Check if the module is already initialized // Check if the module is already initialized
wxModuleList::compatibility_iterator node; wxModuleList::const_iterator it;
for ( node = initializedModules.GetFirst(); node; node = node->GetNext() ) for ( it = initializedModules.begin();
it != initializedModules.end();
++it )
{ {
if ( node->GetData()->GetClassInfo() == cinfo ) if ( (*it)->GetClassInfo() == cinfo )
break; break;
} }
if ( node ) if ( it != initializedModules.end() )
{ {
// this dependency is already initialized, nothing to do // this dependency is already initialized, nothing to do
continue; continue;
} }
// find the module in the registered modules list // find the module in the registered modules list
for ( node = ms_modules.GetFirst(); node; node = node->GetNext() ) for ( it = ms_modules.begin(); it != ms_modules.end(); ++it )
{ {
wxModule *moduleDep = node->GetData(); wxModule *moduleDep = *it;
if ( moduleDep->GetClassInfo() == cinfo ) if ( moduleDep->GetClassInfo() == cinfo )
{ {
if ( !DoInitializeModule(moduleDep, initializedModules ) ) if ( !DoInitializeModule(moduleDep, initializedModules ) )
@@ -116,7 +124,7 @@ bool wxModule::DoInitializeModule(wxModule *module,
} }
} }
if ( !node ) if ( it == ms_modules.end() )
{ {
wxLogError(_("Dependency \"%s\" of module \"%s\" doesn't exist."), wxLogError(_("Dependency \"%s\" of module \"%s\" doesn't exist."),
cinfo->GetClassName(), cinfo->GetClassName(),
@@ -136,7 +144,7 @@ bool wxModule::DoInitializeModule(wxModule *module,
module->GetClassInfo()->GetClassName()); module->GetClassInfo()->GetClassName());
module->m_state = State_Initialized; module->m_state = State_Initialized;
initializedModules.Append(module); initializedModules.push_back(module);
return true; return true;
} }
@@ -146,11 +154,11 @@ bool wxModule::InitializeModules()
{ {
wxModuleList initializedModules; wxModuleList initializedModules;
for ( wxModuleList::compatibility_iterator node = ms_modules.GetFirst(); for ( wxModuleList::const_iterator it = ms_modules.begin();
node; it != ms_modules.end();
node = node->GetNext() ) ++it )
{ {
wxModule *module = node->GetData(); wxModule *module = *it;
// the module could have been already initialized as dependency of // the module could have been already initialized as dependency of
// another one // another one
@@ -178,14 +186,14 @@ void wxModule::DoCleanUpModules(const wxModuleList& modules)
{ {
// cleanup user-defined modules in the reverse order compared to their // cleanup user-defined modules in the reverse order compared to their
// initialization -- this ensures that dependencies are respected // initialization -- this ensures that dependencies are respected
for ( wxModuleList::compatibility_iterator node = modules.GetLast(); for ( wxModuleList::const_reverse_iterator rit = modules.rbegin();
node; rit != modules.rend();
node = node->GetPrevious() ) ++rit )
{ {
wxLogTrace(TRACE_MODULE, wxT("Cleanup module %s"), wxLogTrace(TRACE_MODULE, wxT("Cleanup module %s"),
node->GetData()->GetClassInfo()->GetClassName()); (*rit)->GetClassInfo()->GetClassName());
wxModule * module = node->GetData(); wxModule * module = *rit;
wxASSERT_MSG( module->m_state == State_Initialized, wxASSERT_MSG( module->m_state == State_Initialized,
wxT("not initialized module being cleaned up") ); wxT("not initialized module being cleaned up") );
@@ -195,7 +203,14 @@ void wxModule::DoCleanUpModules(const wxModuleList& modules)
} }
// clear all modules, even the non-initialized ones // clear all modules, even the non-initialized ones
WX_CLEAR_LIST(wxModuleList, ms_modules); for ( wxModuleList::const_iterator it = ms_modules.begin();
it != ms_modules.end();
++it )
{
delete *it;
}
ms_modules.clear();
} }
bool wxModule::ResolveNamedDependencies() bool wxModule::ResolveNamedDependencies()
@@ -214,7 +229,7 @@ bool wxModule::ResolveNamedDependencies()
// add it even if it is not derived from wxModule because // add it even if it is not derived from wxModule because
// DoInitializeModule() will make sure a module with the same class // DoInitializeModule() will make sure a module with the same class
// info exists and fail if it doesn't // info exists and fail if it doesn't
m_dependencies.Add(info); m_dependencies.push_back(info);
} }
return true; return true;