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"
31 #include "wx/persist.h"
33 // ============================================================================
34 // wxPersistenceManager implementation
35 // ============================================================================
38 wxPersistenceManager
& wxPersistenceManager::Get()
40 static wxPersistenceManager s_manager
;
45 wxPersistenceManager::~wxPersistenceManager()
50 wxPersistenceManager::GetKey(const wxPersistentObject
& who
,
51 const wxString
& name
) const
53 wxString
key("Persistent_Options"); // TODO: make this configurable
54 key
<< wxCONFIG_PATH_SEPARATOR
<< who
.GetKind()
55 << wxCONFIG_PATH_SEPARATOR
<< who
.GetName()
56 << wxCONFIG_PATH_SEPARATOR
<< name
;
61 wxPersistentObject
*wxPersistenceManager::Find(void *obj
) const
63 const wxPersistentObjectsMap::const_iterator
64 it
= m_persistentObjects
.find(obj
);
65 return it
== m_persistentObjects
.end() ? NULL
: it
->second
;
69 wxPersistenceManager::Register(void *obj
, wxPersistentObject
*po
)
71 if ( wxPersistentObject
*old
= Find(obj
) )
73 wxFAIL_MSG( "object is already registered" );
75 delete po
; // still avoid the memory leaks
79 m_persistentObjects
[obj
] = po
;
84 void wxPersistenceManager::Unregister(void *obj
)
86 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
87 wxCHECK_RET( it
!= m_persistentObjects
.end(), "not registered" );
89 wxPersistentObject
* const po
= it
->second
;
90 m_persistentObjects
.erase(it
);
94 void wxPersistenceManager::Save(void *obj
)
99 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
100 wxCHECK_RET( it
!= m_persistentObjects
.end(), "not registered" );
105 bool wxPersistenceManager::Restore(void *obj
)
110 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
111 wxCHECK_MSG( it
!= m_persistentObjects
.end(), false, "not registered" );
113 return it
->second
->Restore();
119 template <typename T
>
121 DoSaveValue(wxConfigBase
*conf
, const wxString
& key
, T value
)
123 return conf
&& conf
->Write(key
, value
);
126 template <typename T
>
128 DoRestoreValue(wxConfigBase
*conf
, const wxString
& key
, T
*value
)
130 return conf
&& conf
->Read(key
, value
);
133 } // anonymous namespace
135 #define wxPERSIST_DEFINE_SAVE_RESTORE_FOR(Type) \
136 bool wxPersistenceManager::SaveValue(const wxPersistentObject& who, \
137 const wxString& name, \
140 return DoSaveValue(GetConfig(), GetKey(who, name), value); \
143 bool wxPersistenceManager::RestoreValue(const wxPersistentObject& who, \
144 const wxString& name, \
147 return DoRestoreValue(GetConfig(), GetKey(who, name), value); \
150 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(bool)
151 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(int)
152 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(long)
153 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(wxString
)
155 #undef wxPERSIST_DEFINE_SAVE_RESTORE_FOR
157 #endif // wxUSE_CONFIG