]>
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 | |
111 | // currently these methods simply use wxConfig::Get()\r | |
112 | //\r | |
113 | // TODO: make this customizable by allowing\r | |
114 | // (a) specifying custom wxConfig object to use\r | |
115 | // (b) allowing to use something else entirely\r | |
116 | template <typename T>\r | |
117 | bool\r | |
118 | SaveValue(const wxPersistentObject& who, const wxString& name, T value)\r | |
119 | {\r | |
120 | wxConfigBase * const conf = GetConfig();\r | |
121 | if ( !conf )\r | |
122 | return false;\r | |
123 | \r | |
124 | return conf->Write(GetKey(who, name), value);\r | |
125 | }\r | |
126 | \r | |
127 | template <typename T>\r | |
128 | bool\r | |
129 | RestoreValue(const wxPersistentObject& who, const wxString& name, T *value)\r | |
130 | {\r | |
131 | wxConfigBase * const conf = GetConfig();\r | |
132 | if ( !conf )\r | |
133 | return false;\r | |
134 | \r | |
135 | return conf->Read(GetKey(who, name), value);\r | |
136 | }\r | |
137 | \r | |
138 | private:\r | |
139 | // ctor is private, use Get()\r | |
140 | wxPersistenceManager()\r | |
141 | {\r | |
142 | m_doSave =\r | |
143 | m_doRestore = true;\r | |
144 | }\r | |
145 | \r | |
146 | // helpers of Save/Restore(), will be customized later\r | |
147 | wxConfigBase *GetConfig() const { return wxConfigBase::Get(); }\r | |
148 | wxString GetKey(const wxPersistentObject& who, const wxString& name) const;\r | |
149 | \r | |
150 | \r | |
151 | // map with the registered objects as keys and associated\r | |
152 | // wxPersistentObjects as values\r | |
153 | wxPersistentObjectsMap m_persistentObjects;\r | |
154 | \r | |
155 | // true if we should restore/save the settings (it doesn't make much sense\r | |
156 | // to use this class when both of them are false but setting one of them to\r | |
157 | // false may make sense in some situations)\r | |
158 | bool m_doSave,\r | |
159 | m_doRestore;\r | |
160 | \r | |
161 | DECLARE_NO_COPY_CLASS(wxPersistenceManager);\r | |
162 | };\r | |
163 | \r | |
164 | // ----------------------------------------------------------------------------\r | |
165 | // wxPersistentObject: ABC for anything persistent\r | |
166 | // ----------------------------------------------------------------------------\r | |
167 | \r | |
168 | class wxPersistentObject\r | |
169 | {\r | |
170 | public:\r | |
171 | // ctor associates us with the object whose options we save/restore\r | |
172 | wxPersistentObject(void *obj) : m_obj(obj) { }\r | |
173 | \r | |
174 | // trivial but virtual dtor\r | |
175 | virtual ~wxPersistentObject() { }\r | |
176 | \r | |
177 | \r | |
178 | // methods used by wxPersistenceManager\r | |
179 | // ------------------------------------\r | |
180 | \r | |
181 | // save/restore the corresponding objects settings\r | |
182 | //\r | |
183 | // these methods shouldn't be used directly as they don't respect the\r | |
184 | // global wxPersistenceManager::DisableSaving/Restoring() settings, use\r | |
185 | // wxPersistenceManager methods with the same name instead\r | |
186 | virtual void Save() const = 0;\r | |
187 | virtual bool Restore() = 0;\r | |
188 | \r | |
189 | \r | |
190 | // get the kind of the objects we correspond to, e.g. "Frame"\r | |
191 | virtual wxString GetKind() const = 0;\r | |
192 | \r | |
193 | // get the name of the object we correspond to, e.g. "Main"\r | |
194 | virtual wxString GetName() const = 0;\r | |
195 | \r | |
196 | \r | |
197 | // return the associated object\r | |
198 | void *GetObject() const { return m_obj; }\r | |
199 | \r | |
200 | protected:\r | |
201 | // wrappers for wxPersistenceManager methods which don't require passing\r | |
202 | // "this" as the first parameter all the time\r | |
203 | template <typename T>\r | |
204 | bool SaveValue(const wxString& name, T value) const\r | |
205 | {\r | |
206 | return wxPersistenceManager::Get().SaveValue(*this, name, value);\r | |
207 | }\r | |
208 | \r | |
209 | template <typename T>\r | |
210 | bool RestoreValue(const wxString& name, T *value)\r | |
211 | {\r | |
212 | return wxPersistenceManager::Get().RestoreValue(*this, name, value);\r | |
213 | }\r | |
214 | \r | |
215 | private:\r | |
216 | void * const m_obj;\r | |
217 | \r | |
218 | DECLARE_NO_COPY_CLASS(wxPersistentObject)\r | |
219 | };\r | |
220 | \r | |
221 | #endif // _WX_PERSIST_H_\r | |
222 | \r |