]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/persist.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxPersistenceManager and related classes
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 Provides support for automatically saving and restoring object properties
12 to persistent storage.
14 This class is the central element of wxWidgets persistence framework, see
15 @ref overview_persistence for its overview.
17 This is a singleton class and its unique instance can be retrieved using
24 class wxPersistenceManager
28 Returns the unique persistence manager object.
30 static wxPersistenceManager
& Get();
33 Globally disable saving the persistence object properties.
35 By default, saving properties in Save() is enabled but the program may
36 wish to disable if, for example, it detects that it is running on a
37 system which shouldn't be modified in any way and so configuration
38 file (or Windows registry) shouldn't be written to.
40 @see DisableRestoring()
45 Globally disable restoring the persistence object properties.
47 By default, restoring properties in Restore() is enabled but this
48 function allows to disable it. This is mostly useful for testing.
52 bool DisableRestoring();
56 Register an object with the manager automatically creating a
57 persistence adapter for it.
59 This is equivalent to calling Register(void *, wxPersistentObject *)
60 with wxCreatePersistentObject(obj) as the second argument.
63 The object to register. wxCreatePersistentObject() overload must be
64 defined for the objects of this class.
67 wxPersistentObject
*Register(T
*obj
);
70 Register an object with the manager.
72 Note that registering the object doesn't do anything except allowing to
73 call Restore() for it later. If you want to register the object and
74 restore its properties, use RegisterAndRestore().
76 The manager takes ownership of @a po and will delete it when it is
80 The object to register.
82 The wxPersistentObject to use for saving and restoring this object
85 wxPersistentObject
*Register(void *obj
, wxPersistentObject
*po
);
88 Check if the object is registered and return the associated
89 wxPersistentObject if it is or @NULL otherwise.
91 wxPersistentObject
*Find(void *obj
) const;
94 Unregister the object and delete the associated wxPersistentObject.
96 For the persistent windows this is done automatically (via
97 SaveAndUnregister()) when the window is destroyed so you only need to
98 call this function explicitly if you are using custom persistent
99 objects or if you want to prevent the object properties from being
103 An object previously registered with Register().
105 void Unregister(void *obj
);
109 Save the object properties to persistent storage.
111 This method does nothing if DisableSaving() had been called.
114 An object previously registered with Register().
116 @see SaveAndUnregister()
118 void Save(void *obj
);
121 Restore the object properties previously saved by Save().
123 This method does nothing if DisableRestoring() had been called.
126 An object previously registered with Register().
128 @true if the object properties were restored or @false if nothing
129 was found to restore or the saved settings were invalid.
131 @see RegisterAndRestore()
133 bool Restore(void *obj
);
135 /// Combines both Save() and Unregister() calls.
136 void SaveAndUnregister(void *obj
);
138 /// Combines both Register() and Restore() calls.
141 bool RegisterAndRestore(T
*obj
);
143 bool RegisterAndRestore(void *obj
, wxPersistentObject
*po
);
148 Base class for persistent object adapters.
150 wxWidgets persistence framework is non-intrusive, i.e. can work with the
151 classes which have no relationship to nor knowledge of it. To allow this,
152 an intermediate persistence adapter is used: this is just a simple object
153 which provides the methods used by wxPersistenceManager to save and restore
154 the object properties and implements them using the concrete class methods.
156 You may derive your own classes from wxPersistentObject to implement
157 persistence support for your common classes, see @ref persistence_defining.
159 @see wxPersistentWindow<>
161 class wxPersistentObject
165 Constructor takes the object which we're associated with.
167 This object must have life-time greater than ours as we keep a pointer
170 wxPersistentObject(void *obj
);
172 /// Trivial but virtual destructor.
173 virtual ~wxPersistentObject();
177 @name Methods to be implemented in the derived classes.
179 Notice that these methods are only used by wxPersistenceManager
180 normally and shouldn't be called directly.
185 Save the object properties.
187 The implementation of this method should use SaveValue().
189 virtual void Save() const = 0;
192 Restore the object properties.
194 The implementation of this method should use RestoreValue().
196 virtual bool Restore() = 0;
200 Returns the string uniquely identifying the objects supported by this
203 This method is called from SaveValue() and RestoreValue() and normally
204 returns some short (but not too cryptic) strings, e.g. @c "Checkbox".
206 virtual wxString
GetKind() const = 0;
209 Returns the string uniquely identifying the object we're associated
210 with among all the other objects of the same type.
212 This method is used together with GetKind() to construct the unique
213 full name of the object in e.g. a configuration file.
215 virtual wxString
GetName() const = 0;
220 /// Return the associated object.
221 void *GetObject() const;
225 Save the specified value using the given name.
228 The name of the value in the configuration file.
230 The value to save, currently must be a type supported by wxConfig.
232 @true if the value was saved or @false if an error occurred.
234 template <typename T
>
235 bool SaveValue(const wxString
& name
, T value
) const;
238 Restore the value saved by Save().
241 The same name as was used by Save().
243 Non-@NULL pointer which will be filled with the value if it was
244 read successfully or not modified if it wasn't.
246 @true if the value was successfully read or @false if it was not
247 found or an error occurred.
249 template <typename T
>
250 bool RestoreValue(const wxString
& name
, T
*value
);
254 Function used to create the correct persistent adapter for the given type
257 To be precise, there is no such template function definition but there are
258 overloads of wxCreatePersistentObject() taking different object types for
259 all wxWidgets classes supporting persistence. And you may also define your
260 own overloads to integrate your custom classes with wxWidgets persistence
263 @see @ref persistence_defining
265 @header{wx/persist.h}
268 wxPersistentObject
*wxCreatePersistentObject(T
*obj
);
271 A shorter synonym for wxPersistenceManager::RegisterAndRestore().
273 This function simply calls wxPersistenceManager::RegisterAndRestore() but
274 using it results in slightly shorter code as it calls
275 wxPersistenceManager::Get() internally. As an additional convenience, this
276 function can also set the window name.
278 For the implementation reasons, this function @em must be used instead of
279 the template method when using Microsoft Visual C++ 6 compiler.
281 @param obj wxWindow-derived object to register with persistence manager and
282 to try to restore the settings for.
283 @param name If not empty, @a obj name is changed to the provided value
284 before registering it.
285 @return true if the settings were restored or false otherwise (this will
286 always be the case when the program runs for the first time, for
289 @since 2.9.0, @a name is new in 2.9.1.
291 @header{wx/persist.h}
294 bool wxPersistentRegisterAndRestore(T
*obj
, const wxString
& name
= wxString());