]>
Commit | Line | Data |
---|---|---|
0fa541e8 VZ |
1 | ///////////////////////////////////////////////////////////////////////////////\r |
2 | // Name: wx/persist.h\r | |
3 | // Purpose: common classes for persistence support\r | |
4 | // Author: Vadim Zeitlin\r | |
5 | // Created: 2009-01-18\r | |
6 | // RCS-ID: $Id: wxhead.h,v 1.10 2008-04-15 23:34:19 zeitlin Exp $\r | |
7 | // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>\r | |
8 | // Licence: wxWindows licence\r | |
9 | ///////////////////////////////////////////////////////////////////////////////\r | |
10 | \r | |
11 | #ifndef _WX_PERSIST_H_\r | |
12 | #define _WX_PERSIST_H_\r | |
13 | \r | |
14 | #include "wx/string.h"\r | |
15 | #include "wx/hashmap.h"\r | |
16 | #include "wx/confbase.h"\r | |
17 | \r | |
18 | class wxPersistentObject;\r | |
19 | \r | |
20 | WX_DECLARE_VOIDPTR_HASH_MAP(wxPersistentObject *, wxPersistentObjectsMap);\r | |
21 | \r | |
22 | // ----------------------------------------------------------------------------\r | |
23 | // global functions\r | |
24 | // ----------------------------------------------------------------------------\r | |
25 | \r | |
26 | /*\r | |
27 | We do _not_ declare this function as doing this would force us to specialize\r | |
28 | it for the user classes deriving from the standard persistent classes.\r | |
29 | However we do define overloads of wxCreatePersistentObject() for all the wx\r | |
30 | classes which means that template wxPersistentObject::Restore() picks up the\r | |
31 | right overload to use provided that the header defining the correct overload\r | |
32 | is included before calling it. And a compilation error happens if this is\r | |
33 | not done.\r | |
34 | \r | |
35 | template <class T>\r | |
36 | wxPersistentObject *wxCreatePersistentObject(T *obj);\r | |
37 | \r | |
38 | */\r | |
39 | \r | |
40 | // ----------------------------------------------------------------------------\r | |
41 | // wxPersistenceManager: global aspects of persistent windows\r | |
42 | // ----------------------------------------------------------------------------\r | |
43 | \r | |
44 | class WXDLLIMPEXP_CORE wxPersistenceManager\r | |
45 | {\r | |
46 | public:\r | |
47 | // accessor to the unique persistence manager object\r | |
48 | static wxPersistenceManager& Get();\r | |
49 | \r | |
50 | \r | |
51 | // globally disable restoring or saving the persistent properties (both are\r | |
52 | // enabled by default)\r | |
9966c62b VZ |
53 | void DisableSaving() { m_doSave = false; }\r |
54 | void DisableRestoring() { m_doRestore = false; }\r | |
0fa541e8 VZ |
55 | \r |
56 | \r | |
57 | // register an object with the manager: when using the first overload,\r | |
58 | // wxCreatePersistentObject() must be specialized for this object class;\r | |
59 | // with the second one the persistent adapter is created by the caller\r | |
60 | //\r | |
61 | // the object shouldn't be already registered with us\r | |
62 | template <class T>\r | |
63 | wxPersistentObject *Register(T *obj)\r | |
64 | {\r | |
65 | return Register(obj, wxCreatePersistentObject(obj));\r | |
66 | }\r | |
67 | \r | |
68 | wxPersistentObject *Register(void *obj, wxPersistentObject *po);\r | |
69 | \r | |
70 | // check if the object is registered and return the associated\r | |
71 | // wxPersistentObject if it is or NULL otherwise\r | |
72 | wxPersistentObject *Find(void *obj) const;\r | |
73 | \r | |
74 | // unregister the object, this is called by wxPersistentObject itself so\r | |
75 | // there is usually no need to do it explicitly\r | |
76 | //\r | |
77 | // deletes the associated wxPersistentObject\r | |
78 | void Unregister(void *obj);\r | |
79 | \r | |
80 | \r | |
81 | // save/restore the state of an object\r | |
82 | //\r | |
83 | // these methods do nothing if DisableSaving/Restoring() was called\r | |
84 | //\r | |
85 | // Restore() returns true if the object state was really restored\r | |
86 | void Save(void *obj);\r | |
87 | bool Restore(void *obj);\r | |
88 | \r | |
89 | // combines both Save() and Unregister() calls\r | |
90 | void SaveAndUnregister(void *obj)\r | |
91 | {\r | |
92 | Save(obj);\r | |
93 | Unregister(obj);\r | |
94 | }\r | |
95 | \r | |
96 | // combines both Register() and Restore() calls\r | |
97 | template <class T>\r | |
98 | bool RegisterAndRestore(T *obj)\r | |
99 | {\r | |
100 | return Register(obj) && Restore(obj);\r | |
101 | }\r | |
102 | \r | |
103 | bool RegisterAndRestore(void *obj, wxPersistentObject *po)\r | |
104 | {\r | |
105 | return Register(obj, po) && Restore(obj);\r | |
106 | }\r | |
107 | \r | |
108 | \r | |
109 | // methods used by the persistent objects to save and restore the data\r | |
110 | //\r | |
c7c8fac6 VZ |
111 | // currently these methods simply use wxConfig::Get() but they may be\r |
112 | // overridden in the derived class (once we allow creating custom\r | |
113 | // persistent managers)\r | |
114 | #define wxPERSIST_DECLARE_SAVE_RESTORE_FOR(Type) \\r | |
115 | virtual bool SaveValue(const wxPersistentObject& who, \\r | |
116 | const wxString& name, \\r | |
117 | Type value); \\r | |
118 | \\r | |
119 | virtual bool \\r | |
120 | RestoreValue(const wxPersistentObject& who, \\r | |
121 | const wxString& name, \\r | |
122 | Type *value)\r | |
123 | \r | |
124 | wxPERSIST_DECLARE_SAVE_RESTORE_FOR(bool);\r | |
125 | wxPERSIST_DECLARE_SAVE_RESTORE_FOR(int);\r | |
126 | wxPERSIST_DECLARE_SAVE_RESTORE_FOR(long);\r | |
127 | wxPERSIST_DECLARE_SAVE_RESTORE_FOR(wxString);\r | |
128 | \r | |
129 | #undef wxPERSIST_DECLARE_SAVE_RESTORE_FOR\r | |
0fa541e8 VZ |
130 | \r |
131 | private:\r | |
132 | // ctor is private, use Get()\r | |
133 | wxPersistenceManager()\r | |
134 | {\r | |
135 | m_doSave =\r | |
136 | m_doRestore = true;\r | |
137 | }\r | |
138 | \r | |
c7c8fac6 VZ |
139 | // helpers of Save/Restore()\r |
140 | //\r | |
141 | // TODO: make this customizable by allowing\r | |
142 | // (a) specifying custom wxConfig object to use\r | |
143 | // (b) allowing to use something else entirely\r | |
0fa541e8 VZ |
144 | wxConfigBase *GetConfig() const { return wxConfigBase::Get(); }\r |
145 | wxString GetKey(const wxPersistentObject& who, const wxString& name) const;\r | |
146 | \r | |
147 | \r | |
148 | // map with the registered objects as keys and associated\r | |
149 | // wxPersistentObjects as values\r | |
150 | wxPersistentObjectsMap m_persistentObjects;\r | |
151 | \r | |
152 | // true if we should restore/save the settings (it doesn't make much sense\r | |
153 | // to use this class when both of them are false but setting one of them to\r | |
154 | // false may make sense in some situations)\r | |
155 | bool m_doSave,\r | |
156 | m_doRestore;\r | |
157 | \r | |
158 | DECLARE_NO_COPY_CLASS(wxPersistenceManager);\r | |
159 | };\r | |
160 | \r | |
161 | // ----------------------------------------------------------------------------\r | |
162 | // wxPersistentObject: ABC for anything persistent\r | |
163 | // ----------------------------------------------------------------------------\r | |
164 | \r | |
165 | class wxPersistentObject\r | |
166 | {\r | |
167 | public:\r | |
168 | // ctor associates us with the object whose options we save/restore\r | |
169 | wxPersistentObject(void *obj) : m_obj(obj) { }\r | |
170 | \r | |
171 | // trivial but virtual dtor\r | |
172 | virtual ~wxPersistentObject() { }\r | |
173 | \r | |
174 | \r | |
175 | // methods used by wxPersistenceManager\r | |
176 | // ------------------------------------\r | |
177 | \r | |
178 | // save/restore the corresponding objects settings\r | |
179 | //\r | |
180 | // these methods shouldn't be used directly as they don't respect the\r | |
181 | // global wxPersistenceManager::DisableSaving/Restoring() settings, use\r | |
182 | // wxPersistenceManager methods with the same name instead\r | |
183 | virtual void Save() const = 0;\r | |
184 | virtual bool Restore() = 0;\r | |
185 | \r | |
186 | \r | |
187 | // get the kind of the objects we correspond to, e.g. "Frame"\r | |
188 | virtual wxString GetKind() const = 0;\r | |
189 | \r | |
190 | // get the name of the object we correspond to, e.g. "Main"\r | |
191 | virtual wxString GetName() const = 0;\r | |
192 | \r | |
193 | \r | |
194 | // return the associated object\r | |
195 | void *GetObject() const { return m_obj; }\r | |
196 | \r | |
197 | protected:\r | |
198 | // wrappers for wxPersistenceManager methods which don't require passing\r | |
199 | // "this" as the first parameter all the time\r | |
200 | template <typename T>\r | |
201 | bool SaveValue(const wxString& name, T value) const\r | |
202 | {\r | |
203 | return wxPersistenceManager::Get().SaveValue(*this, name, value);\r | |
204 | }\r | |
205 | \r | |
206 | template <typename T>\r | |
207 | bool RestoreValue(const wxString& name, T *value)\r | |
208 | {\r | |
209 | return wxPersistenceManager::Get().RestoreValue(*this, name, value);\r | |
210 | }\r | |
211 | \r | |
212 | private:\r | |
213 | void * const m_obj;\r | |
214 | \r | |
215 | DECLARE_NO_COPY_CLASS(wxPersistentObject)\r | |
216 | };\r | |
217 | \r | |
218 | #endif // _WX_PERSIST_H_\r | |
219 | \r |