1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: common classes for persistence support
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_PERSIST_H_
12 #define _WX_PERSIST_H_
14 #include "wx/string.h"
15 #include "wx/hashmap.h"
16 #include "wx/confbase.h"
18 class wxPersistentObject
;
20 WX_DECLARE_VOIDPTR_HASH_MAP(wxPersistentObject
*, wxPersistentObjectsMap
);
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
27 We do _not_ declare this function as doing this would force us to specialize
28 it for the user classes deriving from the standard persistent classes.
29 However we do define overloads of wxCreatePersistentObject() for all the wx
30 classes which means that template wxPersistentObject::Restore() picks up the
31 right overload to use provided that the header defining the correct overload
32 is included before calling it. And a compilation error happens if this is
36 wxPersistentObject *wxCreatePersistentObject(T *obj);
40 // ----------------------------------------------------------------------------
41 // wxPersistenceManager: global aspects of persistent windows
42 // ----------------------------------------------------------------------------
44 class WXDLLIMPEXP_CORE wxPersistenceManager
47 // Call this method to specify a non-default persistence manager to use.
48 // This function should usually be called very early to affect creation of
49 // all persistent controls and the object passed to it must have a lifetime
50 // long enough to be still alive when the persistent controls are destroyed
51 // and need it to save their state so typically this would be a global or a
53 static void Set(wxPersistenceManager
& manager
);
55 // accessor to the unique persistence manager object
56 static wxPersistenceManager
& Get();
58 // trivial but virtual dtor
60 // FIXME-VC6: this only needs to be public because of VC6 bug
61 virtual ~wxPersistenceManager();
64 // globally disable restoring or saving the persistent properties (both are
65 // enabled by default)
66 void DisableSaving() { m_doSave
= false; }
67 void DisableRestoring() { m_doRestore
= false; }
70 // register an object with the manager: when using the first overload,
71 // wxCreatePersistentObject() must be specialized for this object class;
72 // with the second one the persistent adapter is created by the caller
74 // the object shouldn't be already registered with us
76 wxPersistentObject
*Register(T
*obj
)
78 return Register(obj
, wxCreatePersistentObject(obj
));
81 wxPersistentObject
*Register(void *obj
, wxPersistentObject
*po
);
83 // check if the object is registered and return the associated
84 // wxPersistentObject if it is or NULL otherwise
85 wxPersistentObject
*Find(void *obj
) const;
87 // unregister the object, this is called by wxPersistentObject itself so
88 // there is usually no need to do it explicitly
90 // deletes the associated wxPersistentObject
91 void Unregister(void *obj
);
94 // save/restore the state of an object
96 // these methods do nothing if DisableSaving/Restoring() was called
98 // Restore() returns true if the object state was really restored
100 bool Restore(void *obj
);
102 // combines both Save() and Unregister() calls
103 void SaveAndUnregister(void *obj
)
109 // combines both Register() and Restore() calls
111 bool RegisterAndRestore(T
*obj
)
113 return Register(obj
) && Restore(obj
);
116 bool RegisterAndRestore(void *obj
, wxPersistentObject
*po
)
118 return Register(obj
, po
) && Restore(obj
);
122 // methods used by the persistent objects to save and restore the data
124 // currently these methods simply use wxConfig::Get() but they may be
125 // overridden in the derived class (once we allow creating custom
126 // persistent managers)
127 #define wxPERSIST_DECLARE_SAVE_RESTORE_FOR(Type) \
128 virtual bool SaveValue(const wxPersistentObject& who, \
129 const wxString& name, \
133 RestoreValue(const wxPersistentObject& who, \
134 const wxString& name, \
137 wxPERSIST_DECLARE_SAVE_RESTORE_FOR(bool);
138 wxPERSIST_DECLARE_SAVE_RESTORE_FOR(int);
139 wxPERSIST_DECLARE_SAVE_RESTORE_FOR(long);
140 wxPERSIST_DECLARE_SAVE_RESTORE_FOR(wxString
);
142 #undef wxPERSIST_DECLARE_SAVE_RESTORE_FOR
145 // ctor is private, use Get()
146 wxPersistenceManager()
153 // Return the config object to use, by default just the global one but a
154 // different one could be used by the derived class if needed.
155 virtual wxConfigBase
*GetConfig() const { return wxConfigBase::Get(); }
157 // Return the path to use for saving the setting with the given name for
158 // the specified object (notice that the name is the name of the setting,
159 // not the name of the object itself which can be retrieved with GetName()).
160 virtual wxString
GetKey(const wxPersistentObject
& who
,
161 const wxString
& name
) const;
165 // map with the registered objects as keys and associated
166 // wxPersistentObjects as values
167 wxPersistentObjectsMap m_persistentObjects
;
169 // true if we should restore/save the settings (it doesn't make much sense
170 // to use this class when both of them are false but setting one of them to
171 // false may make sense in some situations)
175 wxDECLARE_NO_COPY_CLASS(wxPersistenceManager
);
178 // ----------------------------------------------------------------------------
179 // wxPersistentObject: ABC for anything persistent
180 // ----------------------------------------------------------------------------
182 class wxPersistentObject
185 // ctor associates us with the object whose options we save/restore
186 wxPersistentObject(void *obj
) : m_obj(obj
) { }
188 // trivial but virtual dtor
189 virtual ~wxPersistentObject() { }
192 // methods used by wxPersistenceManager
193 // ------------------------------------
195 // save/restore the corresponding objects settings
197 // these methods shouldn't be used directly as they don't respect the
198 // global wxPersistenceManager::DisableSaving/Restoring() settings, use
199 // wxPersistenceManager methods with the same name instead
200 virtual void Save() const = 0;
201 virtual bool Restore() = 0;
204 // get the kind of the objects we correspond to, e.g. "Frame"
205 virtual wxString
GetKind() const = 0;
207 // get the name of the object we correspond to, e.g. "Main"
208 virtual wxString
GetName() const = 0;
211 // return the associated object
212 void *GetObject() const { return m_obj
; }
215 // wrappers for wxPersistenceManager methods which don't require passing
216 // "this" as the first parameter all the time
217 template <typename T
>
218 bool SaveValue(const wxString
& name
, T value
) const
220 return wxPersistenceManager::Get().SaveValue(*this, name
, value
);
223 template <typename T
>
224 bool RestoreValue(const wxString
& name
, T
*value
)
226 return wxPersistenceManager::Get().RestoreValue(*this, name
, value
);
232 wxDECLARE_NO_COPY_CLASS(wxPersistentObject
);
235 // FIXME-VC6: VC6 has troubles with template methods of DLL-exported classes,
236 // apparently it believes they should be defined in the DLL (which
237 // is, of course, impossible as the DLL doesn't know for which types
238 // will they be instantiated) instead of compiling them when
239 // building the main application itself. Because of this problem
240 // (which only arises in debug build!) we can't use the usual
241 // RegisterAndRestore(obj) with it and need to explicitly create the
242 // persistence adapter. To hide this ugliness we define a global
243 // function which does it for us.
244 template <typename T
>
245 inline bool wxPersistentRegisterAndRestore(T
*obj
)
247 wxPersistentObject
* const pers
= wxCreatePersistentObject(obj
);
249 return wxPersistenceManager::Get().RegisterAndRestore(obj
, pers
);
253 // A helper function which also sets the name for the (wxWindow-derived) object
254 // before registering and restoring it.
255 template <typename T
>
256 inline bool wxPersistentRegisterAndRestore(T
*obj
, const wxString
& name
)
260 return wxPersistentRegisterAndRestore(obj
);
263 #endif // _WX_PERSIST_H_