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
;
44 wxPersistenceManager::GetKey(const wxPersistentObject
& who
,
45 const wxString
& name
) const
47 wxString
key("Persistent_Options"); // TODO: make this configurable
48 key
<< wxCONFIG_PATH_SEPARATOR
<< who
.GetKind()
49 << wxCONFIG_PATH_SEPARATOR
<< who
.GetName()
50 << wxCONFIG_PATH_SEPARATOR
<< name
;
55 wxPersistentObject
*wxPersistenceManager::Find(void *obj
) const
57 const wxPersistentObjectsMap::const_iterator
58 it
= m_persistentObjects
.find(obj
);
59 return it
== m_persistentObjects
.end() ? NULL
: it
->second
;
63 wxPersistenceManager::Register(void *obj
, wxPersistentObject
*po
)
65 if ( wxPersistentObject
*old
= Find(obj
) )
67 wxFAIL_MSG( "object is already registered" );
69 delete po
; // still avoid the memory leaks
73 m_persistentObjects
[obj
] = po
;
78 void wxPersistenceManager::Unregister(void *obj
)
80 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
81 wxCHECK_RET( it
!= m_persistentObjects
.end(), "not registered" );
83 wxPersistentObject
* const po
= it
->second
;
84 m_persistentObjects
.erase(it
);
88 void wxPersistenceManager::Save(void *obj
)
93 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
94 wxCHECK_RET( it
!= m_persistentObjects
.end(), "not registered" );
99 bool wxPersistenceManager::Restore(void *obj
)
104 wxPersistentObjectsMap::iterator it
= m_persistentObjects
.find(obj
);
105 wxCHECK_MSG( it
!= m_persistentObjects
.end(), false, "not registered" );
107 return it
->second
->Restore();