]> git.saurik.com Git - wxWidgets.git/blob - src/common/persist.cpp
fixing mem leak
[wxWidgets.git] / src / common / persist.cpp
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 namespace
34 {
35
36 wxPersistenceManager* gs_manager = NULL;
37
38 } // anonymous namespace
39
40 // ============================================================================
41 // wxPersistenceManager implementation
42 // ============================================================================
43
44 /* static */
45 void wxPersistenceManager::Set(wxPersistenceManager& manager)
46 {
47 gs_manager = &manager;
48 }
49
50 /* static */
51 wxPersistenceManager& wxPersistenceManager::Get()
52 {
53 if ( !gs_manager )
54 {
55 static wxPersistenceManager s_manager;
56
57 gs_manager = &s_manager;
58 }
59
60 return *gs_manager;
61 }
62
63 wxPersistenceManager::~wxPersistenceManager()
64 {
65 }
66
67 wxString
68 wxPersistenceManager::GetKey(const wxPersistentObject& who,
69 const wxString& name) const
70 {
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;
75
76 return key;
77 }
78
79 wxPersistentObject *wxPersistenceManager::Find(void *obj) const
80 {
81 const wxPersistentObjectsMap::const_iterator
82 it = m_persistentObjects.find(obj);
83 return it == m_persistentObjects.end() ? NULL : it->second;
84 }
85
86 wxPersistentObject *
87 wxPersistenceManager::Register(void *obj, wxPersistentObject *po)
88 {
89 if ( wxPersistentObject *old = Find(obj) )
90 {
91 wxFAIL_MSG( "object is already registered" );
92
93 delete po; // still avoid the memory leaks
94 return old;
95 }
96
97 m_persistentObjects[obj] = po;
98
99 return po;
100 }
101
102 void wxPersistenceManager::Unregister(void *obj)
103 {
104 wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj);
105 wxCHECK_RET( it != m_persistentObjects.end(), "not registered" );
106
107 wxPersistentObject * const po = it->second;
108 m_persistentObjects.erase(it);
109 delete po;
110 }
111
112 void wxPersistenceManager::Save(void *obj)
113 {
114 if ( !m_doSave )
115 return;
116
117 wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj);
118 wxCHECK_RET( it != m_persistentObjects.end(), "not registered" );
119
120 it->second->Save();
121 }
122
123 bool wxPersistenceManager::Restore(void *obj)
124 {
125 if ( !m_doRestore )
126 return false;
127
128 wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj);
129 wxCHECK_MSG( it != m_persistentObjects.end(), false, "not registered" );
130
131 return it->second->Restore();
132 }
133
134 namespace
135 {
136
137 template <typename T>
138 inline bool
139 DoSaveValue(wxConfigBase *conf, const wxString& key, T value)
140 {
141 return conf && conf->Write(key, value);
142 }
143
144 template <typename T>
145 bool
146 DoRestoreValue(wxConfigBase *conf, const wxString& key, T *value)
147 {
148 return conf && conf->Read(key, value);
149 }
150
151 } // anonymous namespace
152
153 #define wxPERSIST_DEFINE_SAVE_RESTORE_FOR(Type) \
154 bool wxPersistenceManager::SaveValue(const wxPersistentObject& who, \
155 const wxString& name, \
156 Type value) \
157 { \
158 return DoSaveValue(GetConfig(), GetKey(who, name), value); \
159 } \
160 \
161 bool wxPersistenceManager::RestoreValue(const wxPersistentObject& who, \
162 const wxString& name, \
163 Type *value) \
164 { \
165 return DoRestoreValue(GetConfig(), GetKey(who, name), value); \
166 }
167
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)
172
173 #undef wxPERSIST_DEFINE_SAVE_RESTORE_FOR
174
175 #endif // wxUSE_CONFIG