]>
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 license
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
22 class wxPersistenceManager
26 Returns the unique persistence manager object.
28 static wxPersistenceManager
& Get();
31 Globally disable saving the persistence object properties.
33 By default, saving properties in Save() is enabled but the program may
34 wish to disable if, for example, it detects that it is running on a
35 system which shouldn't be modified in any way and so configuration
36 file (or Windows registry) shouldn't be written to.
38 @see DisableRestoring()
43 Globally disable restoring the persistence object properties.
45 By default, restoring properties in Restore() is enabled but this
46 function allows to disable it. This is mostly useful for testing.
50 bool DisableRestoring();
54 Register an object with the manager automatically creating a
55 persistence adapter for it.
57 This is equivalent to calling Register(void *, wxPersistentObject *)
58 with wxCreatePersistentObject(obj) as the second argument.
61 The object to register. wxCreatePersistentObject() overload must be
62 defined for the objects of this class.
65 wxPersistentObject
*Register(T
*obj
)
67 return Register(obj
, wxCreatePersistentObject(obj
));
71 Register an object with the manager.
73 Note that registering the object doesn't do anything except allowing to
74 call Restore() for it later. If you want to register the object and
75 restore its properties, use RegisterAndRestore().
77 The manager takes ownership of @a po and will delete it when it is
81 The object to register.
83 The wxPersistentObject to use for saving and restoring this object
86 wxPersistentObject
*Register(void *obj
, wxPersistentObject
*po
);
89 Check if the object is registered and return the associated
90 wxPersistentObject if it is or @NULL otherwise.
92 wxPersistentObject
*Find(void *obj
) const;
95 Unregister the object and delete the associated wxPersistentObject.
97 For the persistent windows this is done automatically (via
98 SaveAndUnregister()) when the window is destroyed so you only need to
99 call this function explicitly if you are using custom persistent
100 objects or if you want to prevent the object properties from being
104 An object previously registered with Register().
106 void Unregister(void *obj
);
110 Save the object properties to persistent storage.
112 This method does nothing if DisableSaving() had been called.
115 An object previously registered with Register().
117 @see SaveAndUnregister()
119 void Save(void *obj
);
122 Restore the object properties previously saved by Save().
124 This method does nothing if DisableRestoring() had been called.
127 An object previously registered with Register().
129 @true if the object properties were restored or @false if nothing
130 was found to restore or the saved settings were invalid.
132 @see RegisterAndRestore()
134 bool Restore(void *obj
);
136 /// Combines both Save() and Unregister() calls.
137 void SaveAndUnregister(void *obj
);
139 /// Combines both Register() and Restore() calls.
142 bool RegisterAndRestore(T
*obj
);
144 bool RegisterAndRestore(void *obj
, wxPersistentObject
*po
);
149 Base class for persistent object adapters.
151 wxWidgets persistence framework is non-intrusive, i.e. can work with the
152 classes which have no relationship to nor knowledge of it. To allow this,
153 an intermediate persistence adapter is used: this is just a simple object
154 which provides the methods used by wxPersistenceManager to save and restore
155 the object properties and implements them using the concrete class methods.
157 You may derive your own classes from wxPersistentObject to implement
158 persistence support for your common classes, see @ref persistence_defining.
160 @see wxPersistentWindow<>
162 class wxPersistentObject
166 Constructor takes the object which we're associated with.
168 This object must have life-time greater than ours as we keep a pointer
171 wxPersistentObject(void *obj
);
173 /// Trivial but virtual destructor.
174 virtual ~wxPersistentObject();
178 @name Methods to be implemented in the derived classes.
180 Notice that these methods are only used by wxPersistenceManager
181 normally and shouldn't be called directly.
186 Save the object properties.
188 The implementation of this method should use SaveValue().
190 virtual void Save() const = 0;
193 Restore the object properties.
195 The implementation of this method should use RestoreValue().
197 virtual bool Restore() = 0;
201 Returns the string uniquely identifying the objects supported by this
204 This method is called from SaveValue() and RestoreValue() and normally
205 returns some short (but not too cryptic) strings, e.g. @c "Checkbox".
207 virtual wxString
GetKind() const = 0;
210 Returns the string uniquely identifying the object we're associated
211 with among all the other objects of the same type.
213 This method is used together with GetKind() to construct the unique
214 full name of the object in e.g. a configuration file.
216 virtual wxString
GetName() const = 0;
221 /// Return the associated object.
222 void *GetObject() const;
226 Save the specified value using the given name.
229 The name of the value in the configuration file.
231 The value to save, currently must be a type supported by wxConfig.
233 @true if the value was saved or @false if an error occurred.
235 template <typename T
>
236 bool SaveValue(const wxString
& name
, T value
) const
238 return wxPersistenceManager::Get().SaveValue(*this, name
, value
);
242 Restore the value saved by Save().
245 The same name as was used by Save().
247 Non-@NULL pointer which will be filled with the value if it was
248 read successfully or not modified if it wasn't.
250 @true if the value was successfully read or @false if it was not
251 found or an error occurred.
253 template <typename T
>
254 bool RestoreValue(const wxString
& name
, T
*value
)
256 return wxPersistenceManager::Get().RestoreValue(*this, name
, value
);
261 Function used to create the correct persistent adapter for the given type
264 To be precise, there is no such template function definition but there are
265 overloads of wxCreatePersistentObject() taking different object types for
266 all wxWidgets classes supporting persistence. And you may also define your
267 own overloads to integrate your custom classes with wxWidgets persistence
270 @see @ref persistence_defining
273 wxPersistentObject
*wxCreatePersistentObject(T
*obj
);