1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        src/common/persist.cpp
3 // Purpose:     common persistence support classes
4 // Author:      Vadim Zeitlin
5 // Created:     2009-01-20
6 // Copyright:   (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence:     wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9 
10 // ============================================================================
11 // declarations
12 // ============================================================================
13 
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17 
18 // for compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
20 
21 #ifdef __BORLANDC__
22     #pragma hdrstop
23 #endif
24 
25 #if wxUSE_CONFIG
26 
27 #ifndef WX_PRECOMP
28 #endif // WX_PRECOMP
29 
30 #include "wx/persist.h"
31 
32 namespace
33 {
34 
35 wxPersistenceManager* gs_manager = NULL;
36 
37 } // anonymous namespace
38 
39 // ============================================================================
40 // wxPersistenceManager implementation
41 // ============================================================================
42 
43 /* static */
Set(wxPersistenceManager & manager)44 void wxPersistenceManager::Set(wxPersistenceManager& manager)
45 {
46     gs_manager = &manager;
47 }
48 
49 /* static */
Get()50 wxPersistenceManager& wxPersistenceManager::Get()
51 {
52     if ( !gs_manager )
53     {
54         static wxPersistenceManager s_manager;
55 
56         gs_manager = &s_manager;
57     }
58 
59     return *gs_manager;
60 }
61 
~wxPersistenceManager()62 wxPersistenceManager::~wxPersistenceManager()
63 {
64 }
65 
66 wxString
GetKey(const wxPersistentObject & who,const wxString & name) const67 wxPersistenceManager::GetKey(const wxPersistentObject& who,
68                              const wxString& name) const
69 {
70     wxString key("Persistent_Options"); // TODO: make this configurable
71     key << wxCONFIG_PATH_SEPARATOR << who.GetKind()
72         << wxCONFIG_PATH_SEPARATOR << who.GetName()
73         << wxCONFIG_PATH_SEPARATOR << name;
74 
75     return key;
76 }
77 
Find(void * obj) const78 wxPersistentObject *wxPersistenceManager::Find(void *obj) const
79 {
80     const wxPersistentObjectsMap::const_iterator
81         it = m_persistentObjects.find(obj);
82     return it == m_persistentObjects.end() ? NULL : it->second;
83 }
84 
85 wxPersistentObject *
Register(void * obj,wxPersistentObject * po)86 wxPersistenceManager::Register(void *obj, wxPersistentObject *po)
87 {
88     if ( wxPersistentObject *old = Find(obj) )
89     {
90         wxFAIL_MSG( "object is already registered" );
91 
92         delete po; // still avoid the memory leaks
93         return old;
94     }
95 
96     m_persistentObjects[obj] = po;
97 
98     return po;
99 }
100 
Unregister(void * obj)101 void wxPersistenceManager::Unregister(void *obj)
102 {
103     wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj);
104     wxCHECK_RET( it != m_persistentObjects.end(), "not registered" );
105 
106     wxPersistentObject * const po = it->second;
107     m_persistentObjects.erase(it);
108     delete po;
109 }
110 
Save(void * obj)111 void wxPersistenceManager::Save(void *obj)
112 {
113     if ( !m_doSave )
114         return;
115 
116     wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj);
117     wxCHECK_RET( it != m_persistentObjects.end(), "not registered" );
118 
119     it->second->Save();
120 }
121 
Restore(void * obj)122 bool wxPersistenceManager::Restore(void *obj)
123 {
124     if ( !m_doRestore )
125         return false;
126 
127     wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj);
128     wxCHECK_MSG( it != m_persistentObjects.end(), false, "not registered" );
129 
130     return it->second->Restore();
131 }
132 
133 namespace
134 {
135 
136 template <typename T>
137 inline bool
DoSaveValue(wxConfigBase * conf,const wxString & key,T value)138 DoSaveValue(wxConfigBase *conf, const wxString& key, T value)
139 {
140     return conf && conf->Write(key, value);
141 }
142 
143 template <typename T>
144 bool
DoRestoreValue(wxConfigBase * conf,const wxString & key,T * value)145 DoRestoreValue(wxConfigBase *conf, const wxString& key, T *value)
146 {
147     return conf && conf->Read(key, value);
148 }
149 
150 } // anonymous namespace
151 
152 #define wxPERSIST_DEFINE_SAVE_RESTORE_FOR(Type)                               \
153     bool wxPersistenceManager::SaveValue(const wxPersistentObject& who,       \
154                                          const wxString& name,                \
155                                          Type value)                          \
156     {                                                                         \
157         return DoSaveValue(GetConfig(), GetKey(who, name), value);            \
158     }                                                                         \
159                                                                               \
160     bool wxPersistenceManager::RestoreValue(const wxPersistentObject& who,    \
161                                             const wxString& name,             \
162                                             Type *value)                      \
163     {                                                                         \
164         return DoRestoreValue(GetConfig(), GetKey(who, name), value);         \
165     }
166 
167 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(bool)
168 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(int)
169 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(long)
170 wxPERSIST_DEFINE_SAVE_RESTORE_FOR(wxString)
171 
172 #undef wxPERSIST_DEFINE_SAVE_RESTORE_FOR
173 
174 #endif // wxUSE_CONFIG
175