persist.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/persist.h
  3. // Purpose: common classes for persistence support
  4. // Author: Vadim Zeitlin
  5. // Created: 2009-01-18
  6. // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_PERSIST_H_
  10. #define _WX_PERSIST_H_
  11. #include "wx/string.h"
  12. #include "wx/hashmap.h"
  13. #include "wx/confbase.h"
  14. class wxPersistentObject;
  15. WX_DECLARE_VOIDPTR_HASH_MAP(wxPersistentObject *, wxPersistentObjectsMap);
  16. // ----------------------------------------------------------------------------
  17. // global functions
  18. // ----------------------------------------------------------------------------
  19. /*
  20. We do _not_ declare this function as doing this would force us to specialize
  21. it for the user classes deriving from the standard persistent classes.
  22. However we do define overloads of wxCreatePersistentObject() for all the wx
  23. classes which means that template wxPersistentObject::Restore() picks up the
  24. right overload to use provided that the header defining the correct overload
  25. is included before calling it. And a compilation error happens if this is
  26. not done.
  27. template <class T>
  28. wxPersistentObject *wxCreatePersistentObject(T *obj);
  29. */
  30. // ----------------------------------------------------------------------------
  31. // wxPersistenceManager: global aspects of persistent windows
  32. // ----------------------------------------------------------------------------
  33. class WXDLLIMPEXP_CORE wxPersistenceManager
  34. {
  35. public:
  36. // Call this method to specify a non-default persistence manager to use.
  37. // This function should usually be called very early to affect creation of
  38. // all persistent controls and the object passed to it must have a lifetime
  39. // long enough to be still alive when the persistent controls are destroyed
  40. // and need it to save their state so typically this would be a global or a
  41. // wxApp member.
  42. static void Set(wxPersistenceManager& manager);
  43. // accessor to the unique persistence manager object
  44. static wxPersistenceManager& Get();
  45. // trivial but virtual dtor
  46. //
  47. // FIXME-VC6: this only needs to be public because of VC6 bug
  48. virtual ~wxPersistenceManager();
  49. // globally disable restoring or saving the persistent properties (both are
  50. // enabled by default)
  51. void DisableSaving() { m_doSave = false; }
  52. void DisableRestoring() { m_doRestore = false; }
  53. // register an object with the manager: when using the first overload,
  54. // wxCreatePersistentObject() must be specialized for this object class;
  55. // with the second one the persistent adapter is created by the caller
  56. //
  57. // the object shouldn't be already registered with us
  58. template <class T>
  59. wxPersistentObject *Register(T *obj)
  60. {
  61. return Register(obj, wxCreatePersistentObject(obj));
  62. }
  63. wxPersistentObject *Register(void *obj, wxPersistentObject *po);
  64. // check if the object is registered and return the associated
  65. // wxPersistentObject if it is or NULL otherwise
  66. wxPersistentObject *Find(void *obj) const;
  67. // unregister the object, this is called by wxPersistentObject itself so
  68. // there is usually no need to do it explicitly
  69. //
  70. // deletes the associated wxPersistentObject
  71. void Unregister(void *obj);
  72. // save/restore the state of an object
  73. //
  74. // these methods do nothing if DisableSaving/Restoring() was called
  75. //
  76. // Restore() returns true if the object state was really restored
  77. void Save(void *obj);
  78. bool Restore(void *obj);
  79. // combines both Save() and Unregister() calls
  80. void SaveAndUnregister(void *obj)
  81. {
  82. Save(obj);
  83. Unregister(obj);
  84. }
  85. // combines both Register() and Restore() calls
  86. template <class T>
  87. bool RegisterAndRestore(T *obj)
  88. {
  89. return Register(obj) && Restore(obj);
  90. }
  91. bool RegisterAndRestore(void *obj, wxPersistentObject *po)
  92. {
  93. return Register(obj, po) && Restore(obj);
  94. }
  95. // methods used by the persistent objects to save and restore the data
  96. //
  97. // currently these methods simply use wxConfig::Get() but they may be
  98. // overridden in the derived class (once we allow creating custom
  99. // persistent managers)
  100. #define wxPERSIST_DECLARE_SAVE_RESTORE_FOR(Type) \
  101. virtual bool SaveValue(const wxPersistentObject& who, \
  102. const wxString& name, \
  103. Type value); \
  104. \
  105. virtual bool \
  106. RestoreValue(const wxPersistentObject& who, \
  107. const wxString& name, \
  108. Type *value)
  109. wxPERSIST_DECLARE_SAVE_RESTORE_FOR(bool);
  110. wxPERSIST_DECLARE_SAVE_RESTORE_FOR(int);
  111. wxPERSIST_DECLARE_SAVE_RESTORE_FOR(long);
  112. wxPERSIST_DECLARE_SAVE_RESTORE_FOR(wxString);
  113. #undef wxPERSIST_DECLARE_SAVE_RESTORE_FOR
  114. protected:
  115. // ctor is private, use Get()
  116. wxPersistenceManager()
  117. {
  118. m_doSave =
  119. m_doRestore = true;
  120. }
  121. // Return the config object to use, by default just the global one but a
  122. // different one could be used by the derived class if needed.
  123. virtual wxConfigBase *GetConfig() const { return wxConfigBase::Get(); }
  124. // Return the path to use for saving the setting with the given name for
  125. // the specified object (notice that the name is the name of the setting,
  126. // not the name of the object itself which can be retrieved with GetName()).
  127. virtual wxString GetKey(const wxPersistentObject& who,
  128. const wxString& name) const;
  129. private:
  130. // map with the registered objects as keys and associated
  131. // wxPersistentObjects as values
  132. wxPersistentObjectsMap m_persistentObjects;
  133. // true if we should restore/save the settings (it doesn't make much sense
  134. // to use this class when both of them are false but setting one of them to
  135. // false may make sense in some situations)
  136. bool m_doSave,
  137. m_doRestore;
  138. wxDECLARE_NO_COPY_CLASS(wxPersistenceManager);
  139. };
  140. // ----------------------------------------------------------------------------
  141. // wxPersistentObject: ABC for anything persistent
  142. // ----------------------------------------------------------------------------
  143. class wxPersistentObject
  144. {
  145. public:
  146. // ctor associates us with the object whose options we save/restore
  147. wxPersistentObject(void *obj) : m_obj(obj) { }
  148. // trivial but virtual dtor
  149. virtual ~wxPersistentObject() { }
  150. // methods used by wxPersistenceManager
  151. // ------------------------------------
  152. // save/restore the corresponding objects settings
  153. //
  154. // these methods shouldn't be used directly as they don't respect the
  155. // global wxPersistenceManager::DisableSaving/Restoring() settings, use
  156. // wxPersistenceManager methods with the same name instead
  157. virtual void Save() const = 0;
  158. virtual bool Restore() = 0;
  159. // get the kind of the objects we correspond to, e.g. "Frame"
  160. virtual wxString GetKind() const = 0;
  161. // get the name of the object we correspond to, e.g. "Main"
  162. virtual wxString GetName() const = 0;
  163. // return the associated object
  164. void *GetObject() const { return m_obj; }
  165. protected:
  166. // wrappers for wxPersistenceManager methods which don't require passing
  167. // "this" as the first parameter all the time
  168. template <typename T>
  169. bool SaveValue(const wxString& name, T value) const
  170. {
  171. return wxPersistenceManager::Get().SaveValue(*this, name, value);
  172. }
  173. template <typename T>
  174. bool RestoreValue(const wxString& name, T *value)
  175. {
  176. return wxPersistenceManager::Get().RestoreValue(*this, name, value);
  177. }
  178. private:
  179. void * const m_obj;
  180. wxDECLARE_NO_COPY_CLASS(wxPersistentObject);
  181. };
  182. // FIXME-VC6: VC6 has troubles with template methods of DLL-exported classes,
  183. // apparently it believes they should be defined in the DLL (which
  184. // is, of course, impossible as the DLL doesn't know for which types
  185. // will they be instantiated) instead of compiling them when
  186. // building the main application itself. Because of this problem
  187. // (which only arises in debug build!) we can't use the usual
  188. // RegisterAndRestore(obj) with it and need to explicitly create the
  189. // persistence adapter. To hide this ugliness we define a global
  190. // function which does it for us.
  191. template <typename T>
  192. inline bool wxPersistentRegisterAndRestore(T *obj)
  193. {
  194. wxPersistentObject * const pers = wxCreatePersistentObject(obj);
  195. return wxPersistenceManager::Get().RegisterAndRestore(obj, pers);
  196. }
  197. // A helper function which also sets the name for the (wxWindow-derived) object
  198. // before registering and restoring it.
  199. template <typename T>
  200. inline bool wxPersistentRegisterAndRestore(T *obj, const wxString& name)
  201. {
  202. obj->SetName(name);
  203. return wxPersistentRegisterAndRestore(obj);
  204. }
  205. #endif // _WX_PERSIST_H_