module.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: module.h
  3. // Purpose: interface of wxModule
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxModule
  9. The module system is a very simple mechanism to allow applications (and parts
  10. of wxWidgets itself) to define initialization and cleanup functions that are
  11. automatically called on wxWidgets startup and exit.
  12. To define a new kind of module, derive a class from wxModule, override the
  13. wxModule::OnInit and wxModule::OnExit functions, and add the
  14. wxDECLARE_DYNAMIC_CLASS and wxIMPLEMENT_DYNAMIC_CLASS to header and implementation
  15. files (which can be the same file).
  16. On initialization, wxWidgets will find all classes derived from wxModule, create
  17. an instance of each, and call each wxModule::OnInit function. On exit, wxWidgets
  18. will call the wxModule::OnExit function for each module instance.
  19. Note that your module class does not have to be in a header file.
  20. For example:
  21. @code
  22. // A module to allow DDE initialization/cleanup
  23. // without calling these functions from app.cpp or from
  24. // the user's application.
  25. class wxDDEModule: public wxModule
  26. {
  27. public:
  28. wxDDEModule() { }
  29. virtual bool OnInit() { wxDDEInitialize(); return true; };
  30. virtual void OnExit() { wxDDECleanUp(); };
  31. private:
  32. wxDECLARE_DYNAMIC_CLASS(wxDDEModule);
  33. };
  34. wxIMPLEMENT_DYNAMIC_CLASS(wxDDEModule, wxModule);
  35. // Another module which uses DDE in its OnInit()
  36. class MyModule: public wxModule
  37. {
  38. public:
  39. MyModule() { AddDependency(wxCLASSINFO(wxDDEModule)); }
  40. virtual bool OnInit() { ... code using DDE ... }
  41. virtual void OnExit() { ... }
  42. private:
  43. wxDECLARE_DYNAMIC_CLASS(MyModule);
  44. };
  45. wxIMPLEMENT_DYNAMIC_CLASS(MyModule, wxModule);
  46. // Another module which uses DDE in its OnInit()
  47. // but uses a named dependency
  48. class MyModule2: public wxModule
  49. {
  50. public:
  51. MyModule2() { AddDependency("wxDDEModule"); }
  52. virtual bool OnInit() { ... code using DDE ... }
  53. virtual void OnExit() { ... }
  54. private:
  55. wxDECLARE_DYNAMIC_CLASS(MyModule2)
  56. };
  57. wxIMPLEMENT_DYNAMIC_CLASS(MyModule2, wxModule)
  58. @endcode
  59. @library{wxbase}
  60. @category{appmanagement}
  61. */
  62. class wxModule : public wxObject
  63. {
  64. public:
  65. /**
  66. Constructs a wxModule object.
  67. */
  68. wxModule();
  69. /**
  70. Destructor.
  71. */
  72. virtual ~wxModule();
  73. /**
  74. Provide this function with appropriate cleanup for your module.
  75. */
  76. virtual void OnExit() = 0;
  77. /**
  78. Provide this function with appropriate initialization for your module.
  79. If the function returns @false, wxWidgets will exit immediately.
  80. */
  81. virtual bool OnInit() = 0;
  82. protected:
  83. /**
  84. Call this function from the constructor of the derived class.
  85. @a dep must be the wxCLASSINFO() of a wxModule-derived class and the
  86. corresponding module will be loaded before and unloaded after this module.
  87. @param dep
  88. The class information object for the dependent module.
  89. */
  90. void AddDependency(wxClassInfo* dep);
  91. /**
  92. Call this function from the constructor of the derived class.
  93. This overload allows a dependency to be added by name without access to
  94. the class info.
  95. This is useful when a module is declared entirely in a source file and
  96. there is no header for the declaration of the module needed by wxCLASSINFO(),
  97. however errors are not detected until run-time, instead of compile-time, then.
  98. Note that circular dependencies are detected and result in a fatal error.
  99. @param classname
  100. The class name of the dependent module.
  101. */
  102. void AddDependency(const char* classname);
  103. };