]> git.saurik.com Git - wxWidgets.git/blob - src/common/persist.cpp
restore the default bitmap icons size to 16*15, making them larger if necessary is...
[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 #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
110 namespace
111 {
112
113 template <typename T>
114 inline bool
115 DoSaveValue(wxConfigBase *conf, const wxString& key, T value)
116 {
117 return conf && conf->Write(key, value);
118 }
119
120 template <typename T>
121 bool
122 DoRestoreValue(wxConfigBase *conf, const wxString& key, T *value)
123 {
124 return conf && conf->Read(key, value);
125 }
126
127 } // anonymous namespace
128
129 #define wxPERSIST_DEFINE_SAVE_RESTORE_FOR(Type) \
130 bool wxPersistenceManager::SaveValue(const wxPersistentObject& who, \
131 const wxString& name, \
132 Type value) \
133 { \
134 return DoSaveValue(GetConfig(), GetKey(who, name), value); \
135 } \
136 \
137 bool wxPersistenceManager::RestoreValue(const wxPersistentObject& who, \
138 const wxString& name, \
139 Type *value) \
140 { \
141 return DoRestoreValue(GetConfig(), GetKey(who, name), value); \
142 }
143
144 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(bool)
145 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(int)
146 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(long)
147 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(wxString)
148
149 #undef wxPERSIST_DEFINE_SAVE_RESTORE_FOR