]>
Commit | Line | Data |
---|---|---|
0fa541e8 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/persist.cpp | |
3 | // Purpose: common persistence support classes | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2009-01-20 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org> | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // for compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #ifndef WX_PRECOMP | |
27 | #endif // WX_PRECOMP | |
28 | ||
29 | #include "wx/persist.h" | |
30 | ||
31 | // ============================================================================ | |
32 | // wxPersistenceManager implementation | |
33 | // ============================================================================ | |
34 | ||
35 | /* static */ | |
36 | wxPersistenceManager& wxPersistenceManager::Get() | |
37 | { | |
38 | static wxPersistenceManager s_manager; | |
39 | ||
40 | return s_manager; | |
41 | } | |
42 | ||
43 | wxString | |
44 | wxPersistenceManager::GetKey(const wxPersistentObject& who, | |
45 | const wxString& name) const | |
46 | { | |
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; | |
51 | ||
52 | return key; | |
53 | } | |
54 | ||
55 | wxPersistentObject *wxPersistenceManager::Find(void *obj) const | |
56 | { | |
57 | const wxPersistentObjectsMap::const_iterator | |
58 | it = m_persistentObjects.find(obj); | |
59 | return it == m_persistentObjects.end() ? NULL : it->second; | |
60 | } | |
61 | ||
62 | wxPersistentObject * | |
63 | wxPersistenceManager::Register(void *obj, wxPersistentObject *po) | |
64 | { | |
65 | if ( wxPersistentObject *old = Find(obj) ) | |
66 | { | |
67 | wxFAIL_MSG( "object is already registered" ); | |
68 | ||
69 | delete po; // still avoid the memory leaks | |
70 | return old; | |
71 | } | |
72 | ||
73 | m_persistentObjects[obj] = po; | |
74 | ||
75 | return po; | |
76 | } | |
77 | ||
78 | void wxPersistenceManager::Unregister(void *obj) | |
79 | { | |
80 | wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj); | |
81 | wxCHECK_RET( it != m_persistentObjects.end(), "not registered" ); | |
82 | ||
83 | wxPersistentObject * const po = it->second; | |
84 | m_persistentObjects.erase(it); | |
85 | delete po; | |
86 | } | |
87 | ||
88 | void wxPersistenceManager::Save(void *obj) | |
89 | { | |
90 | if ( !m_doSave ) | |
91 | return; | |
92 | ||
93 | wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj); | |
94 | wxCHECK_RET( it != m_persistentObjects.end(), "not registered" ); | |
95 | ||
96 | it->second->Save(); | |
97 | } | |
98 | ||
99 | bool wxPersistenceManager::Restore(void *obj) | |
100 | { | |
101 | if ( !m_doRestore ) | |
102 | return false; | |
103 | ||
104 | wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj); | |
105 | wxCHECK_MSG( it != m_persistentObjects.end(), false, "not registered" ); | |
106 | ||
107 | return it->second->Restore(); | |
108 | } | |
109 |