| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/module.h |
| 3 | // Purpose: Modules handling |
| 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 |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifndef _WX_MODULE_H_ |
| 13 | #define _WX_MODULE_H_ |
| 14 | |
| 15 | #include "wx/object.h" |
| 16 | #include "wx/list.h" |
| 17 | #include "wx/arrstr.h" |
| 18 | #include "wx/dynarray.h" |
| 19 | |
| 20 | // declare a linked list of modules |
| 21 | class WXDLLIMPEXP_FWD_BASE wxModule; |
| 22 | WX_DECLARE_USER_EXPORTED_LIST(wxModule, wxModuleList, WXDLLIMPEXP_BASE); |
| 23 | |
| 24 | // and an array of class info objects |
| 25 | WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxClassInfo *, wxArrayClassInfo, |
| 26 | class WXDLLIMPEXP_BASE); |
| 27 | |
| 28 | |
| 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 |
| 33 | { |
| 34 | public: |
| 35 | wxModule() {} |
| 36 | virtual ~wxModule() {} |
| 37 | |
| 38 | // if module init routine returns false the application |
| 39 | // will fail to startup |
| 40 | |
| 41 | bool Init() { return OnInit(); } |
| 42 | void Exit() { OnExit(); } |
| 43 | |
| 44 | // Override both of these |
| 45 | |
| 46 | // called on program startup |
| 47 | |
| 48 | virtual bool OnInit() = 0; |
| 49 | |
| 50 | // called just before program termination, but only if OnInit() |
| 51 | // succeeded |
| 52 | |
| 53 | virtual void OnExit() = 0; |
| 54 | |
| 55 | static void RegisterModule(wxModule *module); |
| 56 | static void RegisterModules(); |
| 57 | static bool InitializeModules(); |
| 58 | static void CleanUpModules() { DoCleanUpModules(m_modules); } |
| 59 | |
| 60 | // used by wxObjectLoader when unloading shared libs's |
| 61 | |
| 62 | static void UnregisterModule(wxModule *module); |
| 63 | |
| 64 | protected: |
| 65 | static wxModuleList m_modules; |
| 66 | |
| 67 | // the function to call from constructor of a deriving class add module |
| 68 | // dependency which will be initialized before the module and unloaded |
| 69 | // after that |
| 70 | void AddDependency(wxClassInfo *dep) |
| 71 | { |
| 72 | wxCHECK_RET( dep, _T("NULL module dependency") ); |
| 73 | |
| 74 | m_dependencies.Add(dep); |
| 75 | } |
| 76 | |
| 77 | // same as the version above except it will look up wxClassInfo by name on |
| 78 | // its own |
| 79 | void AddDependency(const char *className) |
| 80 | { |
| 81 | m_namedDependencies.Add(className); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | private: |
| 86 | // initialize module and Append it to initializedModules list recursively |
| 87 | // calling itself to satisfy module dependencies if needed |
| 88 | static bool |
| 89 | DoInitializeModule(wxModule *module, wxModuleList &initializedModules); |
| 90 | |
| 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); |
| 95 | |
| 96 | // resolve all named dependencies and add them to the normal m_dependencies |
| 97 | bool ResolveNamedDependencies(); |
| 98 | |
| 99 | |
| 100 | // module dependencies: contains wxClassInfo pointers for all modules which |
| 101 | // must be initialized before this one |
| 102 | wxArrayClassInfo m_dependencies; |
| 103 | |
| 104 | // and the named dependencies: those will be resolved during run-time and |
| 105 | // added to m_dependencies |
| 106 | wxArrayString m_namedDependencies; |
| 107 | |
| 108 | // used internally while initializing/cleaning up modules |
| 109 | enum |
| 110 | { |
| 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 |
| 114 | } m_state; |
| 115 | |
| 116 | |
| 117 | DECLARE_CLASS(wxModule) |
| 118 | }; |
| 119 | |
| 120 | #endif // _WX_MODULE_H_ |