set standard properties

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58591 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2009-02-01 12:32:29 +00:00
parent 654c223bc7
commit 1f0539a4a2

View File

@ -1,219 +1,219 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Name: wx/persist.h // Name: wx/persist.h
// Purpose: common classes for persistence support // Purpose: common classes for persistence support
// Author: Vadim Zeitlin // Author: Vadim Zeitlin
// Created: 2009-01-18 // Created: 2009-01-18
// RCS-ID: $Id: wxhead.h,v 1.10 2008-04-15 23:34:19 zeitlin Exp $ // RCS-ID: $Id$
// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org> // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence // Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_PERSIST_H_ #ifndef _WX_PERSIST_H_
#define _WX_PERSIST_H_ #define _WX_PERSIST_H_
#include "wx/string.h" #include "wx/string.h"
#include "wx/hashmap.h" #include "wx/hashmap.h"
#include "wx/confbase.h" #include "wx/confbase.h"
class wxPersistentObject; class wxPersistentObject;
WX_DECLARE_VOIDPTR_HASH_MAP(wxPersistentObject *, wxPersistentObjectsMap); WX_DECLARE_VOIDPTR_HASH_MAP(wxPersistentObject *, wxPersistentObjectsMap);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// global functions // global functions
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/* /*
We do _not_ declare this function as doing this would force us to specialize We do _not_ declare this function as doing this would force us to specialize
it for the user classes deriving from the standard persistent classes. it for the user classes deriving from the standard persistent classes.
However we do define overloads of wxCreatePersistentObject() for all the wx However we do define overloads of wxCreatePersistentObject() for all the wx
classes which means that template wxPersistentObject::Restore() picks up the classes which means that template wxPersistentObject::Restore() picks up the
right overload to use provided that the header defining the correct overload right overload to use provided that the header defining the correct overload
is included before calling it. And a compilation error happens if this is is included before calling it. And a compilation error happens if this is
not done. not done.
template <class T> template <class T>
wxPersistentObject *wxCreatePersistentObject(T *obj); wxPersistentObject *wxCreatePersistentObject(T *obj);
*/ */
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxPersistenceManager: global aspects of persistent windows // wxPersistenceManager: global aspects of persistent windows
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class WXDLLIMPEXP_CORE wxPersistenceManager class WXDLLIMPEXP_CORE wxPersistenceManager
{ {
public: public:
// accessor to the unique persistence manager object // accessor to the unique persistence manager object
static wxPersistenceManager& Get(); static wxPersistenceManager& Get();
// globally disable restoring or saving the persistent properties (both are // globally disable restoring or saving the persistent properties (both are
// enabled by default) // enabled by default)
void DisableSaving() { m_doSave = false; } void DisableSaving() { m_doSave = false; }
void DisableRestoring() { m_doRestore = false; } void DisableRestoring() { m_doRestore = false; }
// register an object with the manager: when using the first overload, // register an object with the manager: when using the first overload,
// wxCreatePersistentObject() must be specialized for this object class; // wxCreatePersistentObject() must be specialized for this object class;
// with the second one the persistent adapter is created by the caller // with the second one the persistent adapter is created by the caller
// //
// the object shouldn't be already registered with us // the object shouldn't be already registered with us
template <class T> template <class T>
wxPersistentObject *Register(T *obj) wxPersistentObject *Register(T *obj)
{ {
return Register(obj, wxCreatePersistentObject(obj)); return Register(obj, wxCreatePersistentObject(obj));
} }
wxPersistentObject *Register(void *obj, wxPersistentObject *po); wxPersistentObject *Register(void *obj, wxPersistentObject *po);
// check if the object is registered and return the associated // check if the object is registered and return the associated
// wxPersistentObject if it is or NULL otherwise // wxPersistentObject if it is or NULL otherwise
wxPersistentObject *Find(void *obj) const; wxPersistentObject *Find(void *obj) const;
// unregister the object, this is called by wxPersistentObject itself so // unregister the object, this is called by wxPersistentObject itself so
// there is usually no need to do it explicitly // there is usually no need to do it explicitly
// //
// deletes the associated wxPersistentObject // deletes the associated wxPersistentObject
void Unregister(void *obj); void Unregister(void *obj);
// save/restore the state of an object // save/restore the state of an object
// //
// these methods do nothing if DisableSaving/Restoring() was called // these methods do nothing if DisableSaving/Restoring() was called
// //
// Restore() returns true if the object state was really restored // Restore() returns true if the object state was really restored
void Save(void *obj); void Save(void *obj);
bool Restore(void *obj); bool Restore(void *obj);
// combines both Save() and Unregister() calls // combines both Save() and Unregister() calls
void SaveAndUnregister(void *obj) void SaveAndUnregister(void *obj)
{ {
Save(obj); Save(obj);
Unregister(obj); Unregister(obj);
} }
// combines both Register() and Restore() calls // combines both Register() and Restore() calls
template <class T> template <class T>
bool RegisterAndRestore(T *obj) bool RegisterAndRestore(T *obj)
{ {
return Register(obj) && Restore(obj); return Register(obj) && Restore(obj);
} }
bool RegisterAndRestore(void *obj, wxPersistentObject *po) bool RegisterAndRestore(void *obj, wxPersistentObject *po)
{ {
return Register(obj, po) && Restore(obj); return Register(obj, po) && Restore(obj);
} }
// methods used by the persistent objects to save and restore the data // methods used by the persistent objects to save and restore the data
// //
// currently these methods simply use wxConfig::Get() but they may be // currently these methods simply use wxConfig::Get() but they may be
// overridden in the derived class (once we allow creating custom // overridden in the derived class (once we allow creating custom
// persistent managers) // persistent managers)
#define wxPERSIST_DECLARE_SAVE_RESTORE_FOR(Type) \ #define wxPERSIST_DECLARE_SAVE_RESTORE_FOR(Type) \
virtual bool SaveValue(const wxPersistentObject& who, \ virtual bool SaveValue(const wxPersistentObject& who, \
const wxString& name, \ const wxString& name, \
Type value); \ Type value); \
\ \
virtual bool \ virtual bool \
RestoreValue(const wxPersistentObject& who, \ RestoreValue(const wxPersistentObject& who, \
const wxString& name, \ const wxString& name, \
Type *value) Type *value)
wxPERSIST_DECLARE_SAVE_RESTORE_FOR(bool); wxPERSIST_DECLARE_SAVE_RESTORE_FOR(bool);
wxPERSIST_DECLARE_SAVE_RESTORE_FOR(int); wxPERSIST_DECLARE_SAVE_RESTORE_FOR(int);
wxPERSIST_DECLARE_SAVE_RESTORE_FOR(long); wxPERSIST_DECLARE_SAVE_RESTORE_FOR(long);
wxPERSIST_DECLARE_SAVE_RESTORE_FOR(wxString); wxPERSIST_DECLARE_SAVE_RESTORE_FOR(wxString);
#undef wxPERSIST_DECLARE_SAVE_RESTORE_FOR #undef wxPERSIST_DECLARE_SAVE_RESTORE_FOR
private: private:
// ctor is private, use Get() // ctor is private, use Get()
wxPersistenceManager() wxPersistenceManager()
{ {
m_doSave = m_doSave =
m_doRestore = true; m_doRestore = true;
} }
// helpers of Save/Restore() // helpers of Save/Restore()
// //
// TODO: make this customizable by allowing // TODO: make this customizable by allowing
// (a) specifying custom wxConfig object to use // (a) specifying custom wxConfig object to use
// (b) allowing to use something else entirely // (b) allowing to use something else entirely
wxConfigBase *GetConfig() const { return wxConfigBase::Get(); } wxConfigBase *GetConfig() const { return wxConfigBase::Get(); }
wxString GetKey(const wxPersistentObject& who, const wxString& name) const; wxString GetKey(const wxPersistentObject& who, const wxString& name) const;
// map with the registered objects as keys and associated // map with the registered objects as keys and associated
// wxPersistentObjects as values // wxPersistentObjects as values
wxPersistentObjectsMap m_persistentObjects; wxPersistentObjectsMap m_persistentObjects;
// true if we should restore/save the settings (it doesn't make much sense // true if we should restore/save the settings (it doesn't make much sense
// to use this class when both of them are false but setting one of them to // to use this class when both of them are false but setting one of them to
// false may make sense in some situations) // false may make sense in some situations)
bool m_doSave, bool m_doSave,
m_doRestore; m_doRestore;
DECLARE_NO_COPY_CLASS(wxPersistenceManager); DECLARE_NO_COPY_CLASS(wxPersistenceManager);
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxPersistentObject: ABC for anything persistent // wxPersistentObject: ABC for anything persistent
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class wxPersistentObject class wxPersistentObject
{ {
public: public:
// ctor associates us with the object whose options we save/restore // ctor associates us with the object whose options we save/restore
wxPersistentObject(void *obj) : m_obj(obj) { } wxPersistentObject(void *obj) : m_obj(obj) { }
// trivial but virtual dtor // trivial but virtual dtor
virtual ~wxPersistentObject() { } virtual ~wxPersistentObject() { }
// methods used by wxPersistenceManager // methods used by wxPersistenceManager
// ------------------------------------ // ------------------------------------
// save/restore the corresponding objects settings // save/restore the corresponding objects settings
// //
// these methods shouldn't be used directly as they don't respect the // these methods shouldn't be used directly as they don't respect the
// global wxPersistenceManager::DisableSaving/Restoring() settings, use // global wxPersistenceManager::DisableSaving/Restoring() settings, use
// wxPersistenceManager methods with the same name instead // wxPersistenceManager methods with the same name instead
virtual void Save() const = 0; virtual void Save() const = 0;
virtual bool Restore() = 0; virtual bool Restore() = 0;
// get the kind of the objects we correspond to, e.g. "Frame" // get the kind of the objects we correspond to, e.g. "Frame"
virtual wxString GetKind() const = 0; virtual wxString GetKind() const = 0;
// get the name of the object we correspond to, e.g. "Main" // get the name of the object we correspond to, e.g. "Main"
virtual wxString GetName() const = 0; virtual wxString GetName() const = 0;
// return the associated object // return the associated object
void *GetObject() const { return m_obj; } void *GetObject() const { return m_obj; }
protected: protected:
// wrappers for wxPersistenceManager methods which don't require passing // wrappers for wxPersistenceManager methods which don't require passing
// "this" as the first parameter all the time // "this" as the first parameter all the time
template <typename T> template <typename T>
bool SaveValue(const wxString& name, T value) const bool SaveValue(const wxString& name, T value) const
{ {
return wxPersistenceManager::Get().SaveValue(*this, name, value); return wxPersistenceManager::Get().SaveValue(*this, name, value);
} }
template <typename T> template <typename T>
bool RestoreValue(const wxString& name, T *value) bool RestoreValue(const wxString& name, T *value)
{ {
return wxPersistenceManager::Get().RestoreValue(*this, name, value); return wxPersistenceManager::Get().RestoreValue(*this, name, value);
} }
private: private:
void * const m_obj; void * const m_obj;
DECLARE_NO_COPY_CLASS(wxPersistentObject) DECLARE_NO_COPY_CLASS(wxPersistentObject)
}; };
#endif // _WX_PERSIST_H_ #endif // _WX_PERSIST_H_