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"
36 wxPersistenceManager
* gs_manager
= NULL
;
38 } // anonymous namespace
40 // ============================================================================
41 // wxPersistenceManager implementation
42 // ============================================================================
45 void wxPersistenceManager::Set(wxPersistenceManager
& manager
)
47 gs_manager
= &manager
;
51 wxPersistenceManager
& wxPersistenceManager::Get()
55 static wxPersistenceManager s_manager
;
57 gs_manager
= &s_manager
;
63 wxPersistenceManager::~wxPersistenceManager()
68 wxPersistenceManager::GetKey(const wxPersistentObject
& who
,
69 const wxString
& name
) const
71 wxString
key("Persistent_Options"); // TODO: make this configurable
72 key
<< wxCONFIG_PATH_SEPARATOR
<< who
.GetKind()
73 << wxCONFIG_PATH_SEPARATOR
<< who
.GetName()
74 << wxCONFIG_PATH_SEPARATOR
<< name
;
79 wxPersistentObject
*wxPersistenceManager::Find(void *obj
) const
81 const wxPersistentObjectsMap::const_iterator
82 it
= m_persistentObjects
.find(obj
);
83 return it
== m_persistentObjects
.end() ? NULL
: it
->second
;
87 wxPersistenceManager::Register(void *obj
, wxPersistentObject
*po
)
89 if ( wxPersistentObject
*old
= Find(obj
) )
91 wxFAIL_MSG( "object is already registered" );
93 delete po
; // still avoid the memory leaks
97 m_persistentObjects
[obj
] = po
;
102 void wxPersistenceManager::Unregister(void *obj
)
104 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
105 wxCHECK_RET( it
!= m_persistentObjects
.end(), "not registered" );
107 wxPersistentObject
* const po
= it
->second
;
108 m_persistentObjects
.erase(it
);
112 void wxPersistenceManager::Save(void *obj
)
117 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
118 wxCHECK_RET( it
!= m_persistentObjects
.end(), "not registered" );
123 bool wxPersistenceManager::Restore(void *obj
)
128 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
129 wxCHECK_MSG( it
!= m_persistentObjects
.end(), false, "not registered" );
131 return it
->second
->Restore();
137 template <typename T
>
139 DoSaveValue(wxConfigBase
*conf
, const wxString
& key
, T value
)
141 return conf
&& conf
->Write(key
, value
);
144 template <typename T
>
146 DoRestoreValue(wxConfigBase
*conf
, const wxString
& key
, T
*value
)
148 return conf
&& conf
->Read(key
, value
);
151 } // anonymous namespace
153 #define wxPERSIST_DEFINE_SAVE_RESTORE_FOR(Type) \
154 bool wxPersistenceManager::SaveValue(const wxPersistentObject& who, \
155 const wxString& name, \
158 return DoSaveValue(GetConfig(), GetKey(who, name), value); \
161 bool wxPersistenceManager::RestoreValue(const wxPersistentObject& who, \
162 const wxString& name, \
165 return DoRestoreValue(GetConfig(), GetKey(who, name), value); \
168 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(bool)
169 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(int)
170 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(long)
171 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(wxString
)
173 #undef wxPERSIST_DEFINE_SAVE_RESTORE_FOR
175 #endif // wxUSE_CONFIG