]>
Commit | Line | Data |
---|---|---|
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 | #if wxUSE_CONFIG | |
27 | ||
28 | #ifndef WX_PRECOMP | |
29 | #endif // WX_PRECOMP | |
30 | ||
31 | #include "wx/persist.h" | |
32 | ||
33 | // ============================================================================ | |
34 | // wxPersistenceManager implementation | |
35 | // ============================================================================ | |
36 | ||
37 | /* static */ | |
38 | wxPersistenceManager& wxPersistenceManager::Get() | |
39 | { | |
40 | static wxPersistenceManager s_manager; | |
41 | ||
42 | return s_manager; | |
43 | } | |
44 | ||
45 | wxPersistenceManager::~wxPersistenceManager() | |
46 | { | |
47 | } | |
48 | ||
49 | wxString | |
50 | wxPersistenceManager::GetKey(const wxPersistentObject& who, | |
51 | const wxString& name) const | |
52 | { | |
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; | |
57 | ||
58 | return key; | |
59 | } | |
60 | ||
61 | wxPersistentObject *wxPersistenceManager::Find(void *obj) const | |
62 | { | |
63 | const wxPersistentObjectsMap::const_iterator | |
64 | it = m_persistentObjects.find(obj); | |
65 | return it == m_persistentObjects.end() ? NULL : it->second; | |
66 | } | |
67 | ||
68 | wxPersistentObject * | |
69 | wxPersistenceManager::Register(void *obj, wxPersistentObject *po) | |
70 | { | |
71 | if ( wxPersistentObject *old = Find(obj) ) | |
72 | { | |
73 | wxFAIL_MSG( "object is already registered" ); | |
74 | ||
75 | delete po; // still avoid the memory leaks | |
76 | return old; | |
77 | } | |
78 | ||
79 | m_persistentObjects[obj] = po; | |
80 | ||
81 | return po; | |
82 | } | |
83 | ||
84 | void wxPersistenceManager::Unregister(void *obj) | |
85 | { | |
86 | wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj); | |
87 | wxCHECK_RET( it != m_persistentObjects.end(), "not registered" ); | |
88 | ||
89 | wxPersistentObject * const po = it->second; | |
90 | m_persistentObjects.erase(it); | |
91 | delete po; | |
92 | } | |
93 | ||
94 | void wxPersistenceManager::Save(void *obj) | |
95 | { | |
96 | if ( !m_doSave ) | |
97 | return; | |
98 | ||
99 | wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj); | |
100 | wxCHECK_RET( it != m_persistentObjects.end(), "not registered" ); | |
101 | ||
102 | it->second->Save(); | |
103 | } | |
104 | ||
105 | bool wxPersistenceManager::Restore(void *obj) | |
106 | { | |
107 | if ( !m_doRestore ) | |
108 | return false; | |
109 | ||
110 | wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj); | |
111 | wxCHECK_MSG( it != m_persistentObjects.end(), false, "not registered" ); | |
112 | ||
113 | return it->second->Restore(); | |
114 | } | |
115 | ||
116 | namespace | |
117 | { | |
118 | ||
119 | template <typename T> | |
120 | inline bool | |
121 | DoSaveValue(wxConfigBase *conf, const wxString& key, T value) | |
122 | { | |
123 | return conf && conf->Write(key, value); | |
124 | } | |
125 | ||
126 | template <typename T> | |
127 | bool | |
128 | DoRestoreValue(wxConfigBase *conf, const wxString& key, T *value) | |
129 | { | |
130 | return conf && conf->Read(key, value); | |
131 | } | |
132 | ||
133 | } // anonymous namespace | |
134 | ||
135 | #define wxPERSIST_DEFINE_SAVE_RESTORE_FOR(Type) \ | |
136 | bool wxPersistenceManager::SaveValue(const wxPersistentObject& who, \ | |
137 | const wxString& name, \ | |
138 | Type value) \ | |
139 | { \ | |
140 | return DoSaveValue(GetConfig(), GetKey(who, name), value); \ | |
141 | } \ | |
142 | \ | |
143 | bool wxPersistenceManager::RestoreValue(const wxPersistentObject& who, \ | |
144 | const wxString& name, \ | |
145 | Type *value) \ | |
146 | { \ | |
147 | return DoRestoreValue(GetConfig(), GetKey(who, name), value); \ | |
148 | } | |
149 | ||
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) | |
154 | ||
155 | #undef wxPERSIST_DEFINE_SAVE_RESTORE_FOR | |
156 | ||
157 | #endif // wxUSE_CONFIG |