/src/wxwidgets/include/wx/module.h
Line | Count | Source |
1 | | ///////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/module.h |
3 | | // Purpose: Modules handling |
4 | | // Author: Wolfram Gloger/adapted by Guilhem Lavaux |
5 | | // Created: 04/11/98 |
6 | | // Copyright: (c) Wolfram Gloger and Guilhem Lavaux |
7 | | // Licence: wxWindows licence |
8 | | ///////////////////////////////////////////////////////////////////////////// |
9 | | |
10 | | #ifndef _WX_MODULE_H_ |
11 | | #define _WX_MODULE_H_ |
12 | | |
13 | | #include "wx/object.h" |
14 | | #include "wx/string.h" |
15 | | #include "wx/vector.h" |
16 | | |
17 | | class wxModule; |
18 | | |
19 | | typedef wxVector<wxModule*> wxModuleList; |
20 | | |
21 | | // declaring a class derived from wxModule will automatically create an |
22 | | // instance of this class on program startup, call its OnInit() method and call |
23 | | // OnExit() on program termination (but only if OnInit() succeeded) |
24 | | class WXDLLIMPEXP_BASE wxModule : public wxObject |
25 | | { |
26 | | public: |
27 | 0 | wxModule() = default; |
28 | 0 | virtual ~wxModule() = default; |
29 | | |
30 | | // if module init routine returns false the application |
31 | | // will fail to startup |
32 | | |
33 | 0 | bool Init() { return OnInit(); } |
34 | 0 | void Exit() { OnExit(); } |
35 | | |
36 | | // Override both of these |
37 | | |
38 | | // called on program startup |
39 | | |
40 | | virtual bool OnInit() = 0; |
41 | | |
42 | | // called just before program termination, but only if OnInit() |
43 | | // succeeded |
44 | | |
45 | | virtual void OnExit() = 0; |
46 | | |
47 | | static void RegisterModule(wxModule *module); |
48 | | static void RegisterModules(); |
49 | | static bool InitializeModules(); |
50 | | static void CleanUpModules(); |
51 | 0 | static bool AreInitialized() { return ms_areInitialized; } |
52 | | |
53 | | // used by wxObjectLoader when unloading shared libs's |
54 | | |
55 | | static void UnregisterModule(wxModule *module); |
56 | | |
57 | | // Initialize the module with the given type information if it's not |
58 | | // already initialized. This is for internal use only currently. |
59 | | static void AddModuleIfNecessary(const wxClassInfo *classInfo); |
60 | | |
61 | | protected: |
62 | | static wxModuleList ms_modules; |
63 | | |
64 | | static bool ms_areInitialized; |
65 | | |
66 | | // the function to call from constructor of a deriving class add module |
67 | | // dependency which will be initialized before the module and unloaded |
68 | | // after that |
69 | | void AddDependency(wxClassInfo *dep) |
70 | 0 | { |
71 | 0 | wxCHECK_RET( dep, wxT("null module dependency") ); |
72 | 0 |
|
73 | 0 | m_dependencies.push_back(dep); |
74 | 0 | } |
75 | | |
76 | | // same as the version above except it will look up wxClassInfo by name on |
77 | | // its own. Note that className must be ASCII |
78 | | void AddDependency(const char *className) |
79 | 0 | { |
80 | 0 | m_namedDependencies.push_back(wxASCII_STR(className)); |
81 | 0 | } |
82 | | |
83 | | |
84 | | private: |
85 | | // initialize module and Append it to initializedModules list recursively |
86 | | // calling itself to satisfy module dependencies if needed |
87 | | static bool |
88 | | DoInitializeModule(wxModule *module, wxModuleList &initializedModules); |
89 | | |
90 | | // cleanup the modules in the specified list (which may not contain all |
91 | | // modules if we're called during initialization because not all modules |
92 | | // could be initialized) and also empty ms_modules itself |
93 | | static void DoCleanUpModules(const wxModuleList& modules); |
94 | | |
95 | | // resolve all named dependencies and add them to the normal m_dependencies |
96 | | bool ResolveNamedDependencies(); |
97 | | |
98 | | |
99 | | // module dependencies: contains wxClassInfo pointers for all modules which |
100 | | // must be initialized before this one |
101 | | typedef wxVector<wxClassInfo*> wxArrayClassInfo; |
102 | | wxArrayClassInfo m_dependencies; |
103 | | |
104 | | // and the named dependencies: those will be resolved during run-time and |
105 | | // added to m_dependencies |
106 | | wxVector<wxString> 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 | | wxDECLARE_CLASS(wxModule); |
118 | | }; |
119 | | |
120 | | #endif // _WX_MODULE_H_ |