1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/persist.cpp
3 // Purpose: common persistence support classes
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // for compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
30 #include "wx/persist.h"
35 wxPersistenceManager
* gs_manager
= NULL
;
37 } // anonymous namespace
39 // ============================================================================
40 // wxPersistenceManager implementation
41 // ============================================================================
44 void wxPersistenceManager::Set(wxPersistenceManager
& manager
)
46 gs_manager
= &manager
;
50 wxPersistenceManager
& wxPersistenceManager::Get()
54 static wxPersistenceManager s_manager
;
56 gs_manager
= &s_manager
;
62 wxPersistenceManager::~wxPersistenceManager()
67 wxPersistenceManager::GetKey(const wxPersistentObject
& who
,
68 const wxString
& name
) const
70 wxString
key("Persistent_Options"); // TODO: make this configurable
71 key
<< wxCONFIG_PATH_SEPARATOR
<< who
.GetKind()
72 << wxCONFIG_PATH_SEPARATOR
<< who
.GetName()
73 << wxCONFIG_PATH_SEPARATOR
<< name
;
78 wxPersistentObject
*wxPersistenceManager::Find(void *obj
) const
80 const wxPersistentObjectsMap::const_iterator
81 it
= m_persistentObjects
.find(obj
);
82 return it
== m_persistentObjects
.end() ? NULL
: it
->second
;
86 wxPersistenceManager::Register(void *obj
, wxPersistentObject
*po
)
88 if ( wxPersistentObject
*old
= Find(obj
) )
90 wxFAIL_MSG( "object is already registered" );
92 delete po
; // still avoid the memory leaks
96 m_persistentObjects
[obj
] = po
;
101 void wxPersistenceManager::Unregister(void *obj
)
103 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
104 wxCHECK_RET( it
!= m_persistentObjects
.end(), "not registered" );
106 wxPersistentObject
* const po
= it
->second
;
107 m_persistentObjects
.erase(it
);
111 void wxPersistenceManager::Save(void *obj
)
116 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
117 wxCHECK_RET( it
!= m_persistentObjects
.end(), "not registered" );
122 bool wxPersistenceManager::Restore(void *obj
)
127 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
128 wxCHECK_MSG( it
!= m_persistentObjects
.end(), false, "not registered" );
130 return it
->second
->Restore();
136 template <typename T
>
138 DoSaveValue(wxConfigBase
*conf
, const wxString
& key
, T value
)
140 return conf
&& conf
->Write(key
, value
);
143 template <typename T
>
145 DoRestoreValue(wxConfigBase
*conf
, const wxString
& key
, T
*value
)
147 return conf
&& conf
->Read(key
, value
);
150 } // anonymous namespace
152 #define wxPERSIST_DEFINE_SAVE_RESTORE_FOR(Type) \
153 bool wxPersistenceManager::SaveValue(const wxPersistentObject& who, \
154 const wxString& name, \
157 return DoSaveValue(GetConfig(), GetKey(who, name), value); \
160 bool wxPersistenceManager::RestoreValue(const wxPersistentObject& who, \
161 const wxString& name, \
164 return DoRestoreValue(GetConfig(), GetKey(who, name), value); \
167 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(bool)
168 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(int)
169 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(long)
170 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(wxString
)
172 #undef wxPERSIST_DEFINE_SAVE_RESTORE_FOR
174 #endif // wxUSE_CONFIG