File: persist.h

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (351 lines) | stat: -rw-r--r-- 11,476 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/////////////////////////////////////////////////////////////////////////////
// Name:        wx/persist.h
// Purpose:     interface of wxPersistenceManager and related classes
// Author:      Vadim Zeitlin
// Copyright:   (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

/**
    Provides support for automatically saving and restoring object properties
    to persistent storage.

    This class is the central element of wxWidgets persistence framework, see
    @ref overview_persistence for its overview.

    This is a singleton class and its unique instance can be retrieved using
    Get() method.

    @since 2.9.0

    @library{wxcore}
 */
class wxPersistenceManager
{
public:
    /**
        Set the global persistence manager to use.

        Call this method to specify a non-default persistence manager to use.
        It should usually be called very early (e.g. in wxApp-derived class
        constructor or in the beginning of overridden wxApp::OnInit()) to
        affect creation of all persistent controls and the object passed to it
        must have a lifetime long enough to be still alive when the persistent
        controls are destroyed and need it to save their state so typically
        this would be a global or a wxApp member.

        @since 2.9.3
     */
    static void Set(wxPersistenceManager& manager);

    /**
        Returns the unique persistence manager object.

        If Set() hadn't been called before, a default persistence manager
        implementation is returned.
     */
    static wxPersistenceManager& Get();

    /**
        Globally disable saving the persistence object properties.

        By default, saving properties in Save() is enabled but the program may
        wish to disable if, for example, it detects that it is running on a
        system which shouldn't be modified in any way and so configuration
        file (or Windows registry) shouldn't be written to.

        @see DisableRestoring()
     */
    void DisableSaving();

    /**
        Globally disable restoring the persistence object properties.

        By default, restoring properties in Restore() is enabled but this
        function allows disabling it. This is mostly useful for testing.

        @see DisableSaving()
     */
    void DisableRestoring();


    /**
        Register an object with the manager automatically creating a
        persistence adapter for it.

        This is equivalent to calling Register(void *, wxPersistentObject *)
        with wxCreatePersistentObject(obj) as the second argument.

        @param obj
            The object to register. wxCreatePersistentObject() overload must be
            defined for the objects of this class.
     */
    template <class T>
    wxPersistentObject *Register(T *obj);

    /**
        Register an object with the manager.

        Note that registering the object doesn't do anything except allowing to
        call Restore() for it later. If you want to register the object and
        restore its properties, use RegisterAndRestore().

        The manager takes ownership of @a po and will delete it when it is
        unregistered.

        @param obj
            The object to register.
        @param po
            The wxPersistentObject to use for saving and restoring this object
            properties.
     */
    wxPersistentObject *Register(void *obj, wxPersistentObject *po);

    /**
        Check if the object is registered and return the associated
        wxPersistentObject if it is or @NULL otherwise.
     */
    wxPersistentObject *Find(void *obj) const;

    /**
        Unregister the object and delete the associated wxPersistentObject.

        For the persistent windows this is done automatically (via
        SaveAndUnregister()) when the window is destroyed so you only need to
        call this function explicitly if you are using custom persistent
        objects or if you want to prevent the object properties from being
        saved.

        @param obj
            An object previously registered with Register().
     */
    void Unregister(void *obj);


    /**
        Save the object properties to persistent storage.

        This method does nothing if DisableSaving() had been called.

        @param obj
            An object previously registered with Register().

        @see SaveAndUnregister()
     */
    void Save(void *obj);

    /**
        Restore the object properties previously saved by Save().

        This method does nothing if DisableRestoring() had been called.

        @param obj
            An object previously registered with Register().
        @return
            @true if the object properties were restored or @false if nothing
            was found to restore or the saved settings were invalid.

        @see RegisterAndRestore()
     */
    bool Restore(void *obj);

    /// Combines both Save() and Unregister() calls.
    void SaveAndUnregister(void *obj);

    /// Combines both Register() and Restore() calls.
    //@{
    template <class T>
    bool RegisterAndRestore(T *obj);

    bool RegisterAndRestore(void *obj, wxPersistentObject *po);
    //@}

protected:
    /**
        Protected default constructor.

        This constructor is only provided for the derived classes, to use an
        object of this class static Get() method should be called.
     */
    wxPersistenceManager();

    /**
        Return the config object to use.

        By default the global wxConfig, returned by wxConfigBase::Get(), is
        used but a derived class could override this function to return a
        different one if necessary.

        @since 2.9.3
     */
    virtual wxConfigBase *GetConfig() const;

    /**
        Return the path to use for saving the setting with the given name for
        the specified object.

        Notice that the @a name argument is the name of the setting, not the
        name of the object itself which can be retrieved with its GetName()
        method.

        This method can be overridden by a derived class to change where in
        wxConfig the different options are stored. By default, all settings of
        the persistent controls are stored under "Persistent_Options" group and
        grouped by control type (e.g. "Window" for top level windows or
        "Splitter") and name, so that the position of a splitter called "sep"
        could be stored under "Persistent_Options/Splitter/sep/Position" key.

        @since 2.9.3
     */
    virtual wxString GetKey(const wxPersistentObject& who,
                            const wxString& name) const;
};

/**
    Base class for persistent object adapters.

    wxWidgets persistence framework is non-intrusive, i.e. can work with the
    classes which have no relationship to nor knowledge of it. To allow this,
    an intermediate persistence adapter is used: this is just a simple object
    which provides the methods used by wxPersistenceManager to save and restore
    the object properties and implements them using the concrete class methods.

    You may derive your own classes from wxPersistentObject to implement
    persistence support for your common classes, see @ref persistence_defining.

    @see wxPersistentWindow<>
 */
class wxPersistentObject
{
public:
    /**
        Constructor takes the object which we're associated with.

        This object must have life-time greater than ours as we keep a pointer
        to it.
     */
    wxPersistentObject(void *obj);

    /// Trivial but virtual destructor.
    virtual ~wxPersistentObject();


    /**
        @name Methods to be implemented in the derived classes.

        Notice that these methods are only used by wxPersistenceManager
        normally and shouldn't be called directly.
     */
    //@{

    /**
        Save the object properties.

        The implementation of this method should use SaveValue().
     */
    virtual void Save() const = 0;

    /**
        Restore the object properties.

        The implementation of this method should use RestoreValue().
     */
    virtual bool Restore() = 0;


    /**
        Returns the string uniquely identifying the objects supported by this
        adapter.

        This method is called from SaveValue() and RestoreValue() and normally
        returns some short (but not too cryptic) strings, e.g. @c "Checkbox".
     */
    virtual wxString GetKind() const = 0;

    /**
        Returns the string uniquely identifying the object we're associated
        with among all the other objects of the same type.

        This method is used together with GetKind() to construct the unique
        full name of the object in e.g. a configuration file.
     */
    virtual wxString GetName() const = 0;

    //@}


    /// Return the associated object.
    void *GetObject() const;

protected:
    /**
        Save the specified value using the given name.

        @param name
            The name of the value in the configuration file.
        @param value
            The value to save, currently must be a type supported by wxConfig.
        @return
            @true if the value was saved or @false if an error occurred.
     */
    template <typename T>
    bool SaveValue(const wxString& name, T value) const;

    /**
        Restore the value saved by Save().

        @param name
            The same name as was used by Save().
        @param value
            Non-@NULL pointer which will be filled with the value if it was
            read successfully or not modified if it wasn't.
        @return
            @true if the value was successfully read or @false if it was not
            found or an error occurred.
     */
    template <typename T>
    bool RestoreValue(const wxString& name, T *value);
};

/**
    Function used to create the correct persistent adapter for the given type
    of objects.

    To be precise, there is no such template function definition but there are
    overloads of wxCreatePersistentObject() taking different object types for
    all wxWidgets classes supporting persistence. And you may also define your
    own overloads to integrate your custom classes with wxWidgets persistence
    framework.

    @see @ref persistence_defining

    @header{wx/persist.h}
 */
template <class T>
wxPersistentObject *wxCreatePersistentObject(T *obj);

/**
    A shorter synonym for wxPersistenceManager::RegisterAndRestore().

    This function simply calls wxPersistenceManager::RegisterAndRestore() but
    using it results in slightly shorter code as it calls
    wxPersistenceManager::Get() internally. As an additional convenience, this
    function can also set the window name.

    For the implementation reasons, this function @em must be used instead of
    the template method when using Microsoft Visual C++ 6 compiler.

    @param obj wxWindow-derived object to register with persistence manager and
        to try to restore the settings for.
    @param name If not empty, @a obj name is changed to the provided value
        before registering it.
    @return true if the settings were restored or false otherwise (this will
        always be the case when the program runs for the first time, for
        example).

    @since 2.9.0, @a name is new in 2.9.1.

    @header{wx/persist.h}
 */
template <class T>
bool wxPersistentRegisterAndRestore(T *obj, const wxString& name = wxString());