1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/persist.cpp
3 // Purpose: common persistence support classes
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
29 #include "wx/persist.h"
31 // ============================================================================
32 // wxPersistenceManager implementation
33 // ============================================================================
36 wxPersistenceManager
& wxPersistenceManager::Get()
38 static wxPersistenceManager s_manager
;
43 wxPersistenceManager::~wxPersistenceManager()
48 wxPersistenceManager::GetKey(const wxPersistentObject
& who
,
49 const wxString
& name
) const
51 wxString
key("Persistent_Options"); // TODO: make this configurable
52 key
<< wxCONFIG_PATH_SEPARATOR
<< who
.GetKind()
53 << wxCONFIG_PATH_SEPARATOR
<< who
.GetName()
54 << wxCONFIG_PATH_SEPARATOR
<< name
;
59 wxPersistentObject
*wxPersistenceManager::Find(void *obj
) const
61 const wxPersistentObjectsMap::const_iterator
62 it
= m_persistentObjects
.find(obj
);
63 return it
== m_persistentObjects
.end() ? NULL
: it
->second
;
67 wxPersistenceManager::Register(void *obj
, wxPersistentObject
*po
)
69 if ( wxPersistentObject
*old
= Find(obj
) )
71 wxFAIL_MSG( "object is already registered" );
73 delete po
; // still avoid the memory leaks
77 m_persistentObjects
[obj
] = po
;
82 void wxPersistenceManager::Unregister(void *obj
)
84 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
85 wxCHECK_RET( it
!= m_persistentObjects
.end(), "not registered" );
87 wxPersistentObject
* const po
= it
->second
;
88 m_persistentObjects
.erase(it
);
92 void wxPersistenceManager::Save(void *obj
)
97 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
98 wxCHECK_RET( it
!= m_persistentObjects
.end(), "not registered" );
103 bool wxPersistenceManager::Restore(void *obj
)
108 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
109 wxCHECK_MSG( it
!= m_persistentObjects
.end(), false, "not registered" );
111 return it
->second
->Restore();
117 template <typename T
>
119 DoSaveValue(wxConfigBase
*conf
, const wxString
& key
, T value
)
121 return conf
&& conf
->Write(key
, value
);
124 template <typename T
>
126 DoRestoreValue(wxConfigBase
*conf
, const wxString
& key
, T
*value
)
128 return conf
&& conf
->Read(key
, value
);
131 } // anonymous namespace
133 #define wxPERSIST_DEFINE_SAVE_RESTORE_FOR(Type) \
134 bool wxPersistenceManager::SaveValue(const wxPersistentObject& who, \
135 const wxString& name, \
138 return DoSaveValue(GetConfig(), GetKey(who, name), value); \
141 bool wxPersistenceManager::RestoreValue(const wxPersistentObject& who, \
142 const wxString& name, \
145 return DoRestoreValue(GetConfig(), GetKey(who, name), value); \
148 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(bool)
149 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(int)
150 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(long)
151 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(wxString
)
153 #undef wxPERSIST_DEFINE_SAVE_RESTORE_FOR