OnExit() is called for modules which were initialized even if the init of
the subsequent modules fails git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1422 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -13,37 +13,46 @@
|
|||||||
#define _WX_MODULEH__
|
#define _WX_MODULEH__
|
||||||
|
|
||||||
#ifdef __GNUG__
|
#ifdef __GNUG__
|
||||||
#pragma interface "module.h"
|
#pragma interface "module.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "wx/object.h"
|
#include "wx/object.h"
|
||||||
#include "wx/list.h"
|
#include "wx/list.h"
|
||||||
#include "wx/setup.h"
|
|
||||||
|
|
||||||
class WXDLLEXPORT wxModule: public wxObject
|
// declare a linked list of modules
|
||||||
|
class wxModule;
|
||||||
|
WX_DECLARE_LIST(wxModule, wxModuleList);
|
||||||
|
|
||||||
|
// declaring a class derived from wxModule will automatically create an
|
||||||
|
// instance of this class on program startup, call its OnInit() method and call
|
||||||
|
// OnExit() on program termination (but only if OnInit() succeeded)
|
||||||
|
class WXDLLEXPORT wxModule : public wxObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
wxModule(void) {}
|
wxModule() {}
|
||||||
~wxModule(void) {}
|
virtual ~wxModule() {}
|
||||||
|
|
||||||
// If returns FALSE, quits the application immediately.
|
// if module init routine returns FALSE application will fail to startup
|
||||||
bool Init(void) { return OnInit(); }
|
bool Init() { return OnInit(); }
|
||||||
void Exit(void) { OnExit(); }
|
void Exit() { OnExit(); }
|
||||||
|
|
||||||
// Override both of these
|
// Override both of these
|
||||||
virtual bool OnInit(void) = 0;
|
// called on program startup
|
||||||
virtual void OnExit(void) = 0;
|
virtual bool OnInit() = 0;
|
||||||
|
// called just before program termination, but only if OnInit()
|
||||||
|
// succeeded
|
||||||
|
virtual void OnExit() = 0;
|
||||||
|
|
||||||
static void RegisterModule(wxModule* module);
|
static void RegisterModule(wxModule* module);
|
||||||
static bool RegisterModules(void);
|
static void RegisterModules();
|
||||||
static bool InitializeModules(void);
|
static bool InitializeModules();
|
||||||
static void CleanUpModules(void);
|
static void CleanUpModules();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static wxList m_modules;
|
static wxModuleList m_modules;
|
||||||
|
|
||||||
DECLARE_CLASS(wxModule)
|
DECLARE_CLASS(wxModule)
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif // _WX_MODULEH__
|
||||||
|
|
||||||
|
@@ -22,19 +22,22 @@
|
|||||||
|
|
||||||
#include "wx/module.h"
|
#include "wx/module.h"
|
||||||
#include "wx/hash.h"
|
#include "wx/hash.h"
|
||||||
|
#include "wx/listimpl.cpp"
|
||||||
|
|
||||||
|
WX_DEFINE_LIST(wxModuleList);
|
||||||
|
|
||||||
IMPLEMENT_CLASS(wxModule, wxObject)
|
IMPLEMENT_CLASS(wxModule, wxObject)
|
||||||
|
|
||||||
wxList wxModule::m_modules;
|
wxModuleList wxModule::m_modules;
|
||||||
|
|
||||||
void wxModule::RegisterModule(wxModule* module)
|
void wxModule::RegisterModule(wxModule* module)
|
||||||
{
|
{
|
||||||
m_modules.Append(module);
|
m_modules.Append(module);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collect up all module-derived classes, create an instance of each,
|
// Collect up all module-derived classes, create an instance of each,
|
||||||
// and register them.
|
// and register them.
|
||||||
bool wxModule::RegisterModules(void)
|
void wxModule::RegisterModules()
|
||||||
{
|
{
|
||||||
wxNode *node;
|
wxNode *node;
|
||||||
wxClassInfo* classInfo;
|
wxClassInfo* classInfo;
|
||||||
@@ -44,36 +47,48 @@ bool wxModule::RegisterModules(void)
|
|||||||
while (node)
|
while (node)
|
||||||
{
|
{
|
||||||
classInfo = (wxClassInfo *)node->Data();
|
classInfo = (wxClassInfo *)node->Data();
|
||||||
if ((classInfo != (& (wxModule::sm_classwxModule))) &&
|
if ( classInfo->IsKindOf(CLASSINFO(wxModule)) &&
|
||||||
classInfo->IsKindOf(CLASSINFO(wxModule)))
|
(classInfo != (& (wxModule::sm_classwxModule))) )
|
||||||
{
|
{
|
||||||
wxModule* module = (wxModule*) classInfo->CreateObject();
|
wxModule* module = (wxModule *)classInfo->CreateObject();
|
||||||
RegisterModule(module);
|
RegisterModule(module);
|
||||||
}
|
}
|
||||||
node = wxClassInfo::sm_classTable->Next();
|
node = wxClassInfo::sm_classTable->Next();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wxModule::InitializeModules()
|
||||||
|
{
|
||||||
|
// Initialize user-defined modules
|
||||||
|
wxModuleList::Node *node;
|
||||||
|
for ( node = m_modules.GetFirst(); node; node = node->GetNext() )
|
||||||
|
{
|
||||||
|
if ( !node->GetData()->Init() )
|
||||||
|
{
|
||||||
|
// clean up already initialized modules - process in reverse order
|
||||||
|
wxModuleList::Node *n;
|
||||||
|
for ( n = node->GetPrevious(); n; n = n->GetPrevious() )
|
||||||
|
{
|
||||||
|
n->GetData()->OnExit();
|
||||||
|
}
|
||||||
|
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxModule::InitializeModules(void)
|
void wxModule::CleanUpModules()
|
||||||
{
|
{
|
||||||
// Initialize user-defined modules
|
// Cleanup user-defined modules
|
||||||
for (wxNode *node = m_modules.First(); node; node = node->Next())
|
wxModuleList::Node *node;
|
||||||
|
for ( node = m_modules.GetFirst(); node; node = node->GetNext() )
|
||||||
{
|
{
|
||||||
if (!((wxModule*)(node->Data()))->Init())
|
node->GetData()->Exit();
|
||||||
return FALSE;
|
|
||||||
}
|
}
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxModule::CleanUpModules(void)
|
m_modules.DeleteContents(TRUE);
|
||||||
{
|
|
||||||
// Cleanup user-defined modules
|
|
||||||
for(wxNode* node = m_modules.Last(); node; node = node->Previous())
|
|
||||||
{
|
|
||||||
((wxModule*)(node->Data()))->Exit();
|
|
||||||
delete (wxModule*)(node->Data());
|
|
||||||
}
|
|
||||||
m_modules.Clear();
|
m_modules.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user