1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Modules handling
4 // Author: Wolfram Gloger/adapted by Guilhem Lavaux
8 // Copyright: (c) Wolfram Gloger and Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #include "wx/object.h"
17 #include "wx/arrstr.h"
18 #include "wx/dynarray.h"
20 // declare a linked list of modules
21 class WXDLLIMPEXP_FWD_BASE wxModule
;
22 WX_DECLARE_USER_EXPORTED_LIST(wxModule
, wxModuleList
, WXDLLIMPEXP_BASE
);
24 // and an array of class info objects
25 WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxClassInfo
*, wxArrayClassInfo
,
26 class WXDLLIMPEXP_BASE
);
29 // declaring a class derived from wxModule will automatically create an
30 // instance of this class on program startup, call its OnInit() method and call
31 // OnExit() on program termination (but only if OnInit() succeeded)
32 class WXDLLIMPEXP_BASE wxModule
: public wxObject
36 virtual ~wxModule() {}
38 // if module init routine returns false the application
39 // will fail to startup
41 bool Init() { return OnInit(); }
42 void Exit() { OnExit(); }
44 // Override both of these
46 // called on program startup
48 virtual bool OnInit() = 0;
50 // called just before program termination, but only if OnInit()
53 virtual void OnExit() = 0;
55 static void RegisterModule(wxModule
*module);
56 static void RegisterModules();
57 static bool InitializeModules();
58 static void CleanUpModules() { DoCleanUpModules(m_modules
); }
60 // used by wxObjectLoader when unloading shared libs's
62 static void UnregisterModule(wxModule
*module);
65 static wxModuleList m_modules
;
67 // the function to call from constructor of a deriving class add module
68 // dependency which will be initialized before the module and unloaded
70 void AddDependency(wxClassInfo
*dep
)
72 wxCHECK_RET( dep
, wxT("NULL module dependency") );
74 m_dependencies
.Add(dep
);
77 // same as the version above except it will look up wxClassInfo by name on
79 void AddDependency(const char *className
)
81 m_namedDependencies
.Add(className
);
86 // initialize module and Append it to initializedModules list recursively
87 // calling itself to satisfy module dependencies if needed
89 DoInitializeModule(wxModule
*module, wxModuleList
&initializedModules
);
91 // cleanup the modules in the specified list (which may not contain all
92 // modules if we're called during initialization because not all modules
93 // could be initialized) and also empty m_modules itself
94 static void DoCleanUpModules(const wxModuleList
& modules
);
96 // resolve all named dependencies and add them to the normal m_dependencies
97 bool ResolveNamedDependencies();
100 // module dependencies: contains wxClassInfo pointers for all modules which
101 // must be initialized before this one
102 wxArrayClassInfo m_dependencies
;
104 // and the named dependencies: those will be resolved during run-time and
105 // added to m_dependencies
106 wxArrayString m_namedDependencies
;
108 // used internally while initializing/cleaning up modules
111 State_Registered
, // module registered but not initialized yet
112 State_Initializing
, // we're initializing this module but not done yet
113 State_Initialized
// module initialized successfully
117 DECLARE_CLASS(wxModule
)
120 #endif // _WX_MODULE_H_