module.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Copyright: (c) Wolfram Gloger and Guilhem Lavaux
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_MODULE_H_
  11. #define _WX_MODULE_H_
  12. #include "wx/object.h"
  13. #include "wx/list.h"
  14. #include "wx/arrstr.h"
  15. #include "wx/dynarray.h"
  16. // declare a linked list of modules
  17. class WXDLLIMPEXP_FWD_BASE wxModule;
  18. WX_DECLARE_USER_EXPORTED_LIST(wxModule, wxModuleList, WXDLLIMPEXP_BASE);
  19. // and an array of class info objects
  20. WX_DEFINE_USER_EXPORTED_ARRAY_PTR(wxClassInfo *, wxArrayClassInfo,
  21. class WXDLLIMPEXP_BASE);
  22. // declaring a class derived from wxModule will automatically create an
  23. // instance of this class on program startup, call its OnInit() method and call
  24. // OnExit() on program termination (but only if OnInit() succeeded)
  25. class WXDLLIMPEXP_BASE wxModule : public wxObject
  26. {
  27. public:
  28. wxModule() {}
  29. virtual ~wxModule() {}
  30. // if module init routine returns false the application
  31. // will fail to startup
  32. bool Init() { return OnInit(); }
  33. void Exit() { OnExit(); }
  34. // Override both of these
  35. // called on program startup
  36. virtual bool OnInit() = 0;
  37. // called just before program termination, but only if OnInit()
  38. // succeeded
  39. virtual void OnExit() = 0;
  40. static void RegisterModule(wxModule *module);
  41. static void RegisterModules();
  42. static bool InitializeModules();
  43. static void CleanUpModules() { DoCleanUpModules(m_modules); }
  44. // used by wxObjectLoader when unloading shared libs's
  45. static void UnregisterModule(wxModule *module);
  46. protected:
  47. static wxModuleList m_modules;
  48. // the function to call from constructor of a deriving class add module
  49. // dependency which will be initialized before the module and unloaded
  50. // after that
  51. void AddDependency(wxClassInfo *dep)
  52. {
  53. wxCHECK_RET( dep, wxT("NULL module dependency") );
  54. m_dependencies.Add(dep);
  55. }
  56. // same as the version above except it will look up wxClassInfo by name on
  57. // its own
  58. void AddDependency(const char *className)
  59. {
  60. m_namedDependencies.Add(className);
  61. }
  62. private:
  63. // initialize module and Append it to initializedModules list recursively
  64. // calling itself to satisfy module dependencies if needed
  65. static bool
  66. DoInitializeModule(wxModule *module, wxModuleList &initializedModules);
  67. // cleanup the modules in the specified list (which may not contain all
  68. // modules if we're called during initialization because not all modules
  69. // could be initialized) and also empty m_modules itself
  70. static void DoCleanUpModules(const wxModuleList& modules);
  71. // resolve all named dependencies and add them to the normal m_dependencies
  72. bool ResolveNamedDependencies();
  73. // module dependencies: contains wxClassInfo pointers for all modules which
  74. // must be initialized before this one
  75. wxArrayClassInfo m_dependencies;
  76. // and the named dependencies: those will be resolved during run-time and
  77. // added to m_dependencies
  78. wxArrayString m_namedDependencies;
  79. // used internally while initializing/cleaning up modules
  80. enum
  81. {
  82. State_Registered, // module registered but not initialized yet
  83. State_Initializing, // we're initializing this module but not done yet
  84. State_Initialized // module initialized successfully
  85. } m_state;
  86. DECLARE_CLASS(wxModule)
  87. };
  88. #endif // _WX_MODULE_H_