]> git.saurik.com Git - wxWidgets.git/blame - src/common/persist.cpp
Make mouse capture checking asserts stronger and more detailed.
[wxWidgets.git] / src / common / persist.cpp
CommitLineData
0fa541e8
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/persist.cpp
3// Purpose: common persistence support classes
4// Author: Vadim Zeitlin
5// Created: 2009-01-20
0fa541e8
VZ
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
7ac1b576
PC
25#if wxUSE_CONFIG
26
0fa541e8
VZ
27#ifndef WX_PRECOMP
28#endif // WX_PRECOMP
29
30#include "wx/persist.h"
31
911219b2
VZ
32namespace
33{
34
35wxPersistenceManager* gs_manager = NULL;
36
37} // anonymous namespace
38
0fa541e8
VZ
39// ============================================================================
40// wxPersistenceManager implementation
41// ============================================================================
42
911219b2
VZ
43/* static */
44void wxPersistenceManager::Set(wxPersistenceManager& manager)
45{
46 gs_manager = &manager;
47}
48
0fa541e8
VZ
49/* static */
50wxPersistenceManager& wxPersistenceManager::Get()
51{
911219b2
VZ
52 if ( !gs_manager )
53 {
54 static wxPersistenceManager s_manager;
55
56 gs_manager = &s_manager;
57 }
0fa541e8 58
911219b2 59 return *gs_manager;
0fa541e8
VZ
60}
61
5bca8be2
VZ
62wxPersistenceManager::~wxPersistenceManager()
63{
64}
65
0fa541e8
VZ
66wxString
67wxPersistenceManager::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
78wxPersistentObject *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
85wxPersistentObject *
86wxPersistenceManager::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
101void 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
111void 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
122bool 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
c7c8fac6
VZ
133namespace
134{
135
136template <typename T>
137inline bool
138DoSaveValue(wxConfigBase *conf, const wxString& key, T value)
139{
140 return conf && conf->Write(key, value);
141}
142
143template <typename T>
144bool
145DoRestoreValue(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
167wxPERSIST_DEFINE_SAVE_RESTORE_FOR(bool)
168wxPERSIST_DEFINE_SAVE_RESTORE_FOR(int)
169wxPERSIST_DEFINE_SAVE_RESTORE_FOR(long)
170wxPERSIST_DEFINE_SAVE_RESTORE_FOR(wxString)
171
03647350 172#undef wxPERSIST_DEFINE_SAVE_RESTORE_FOR
7ac1b576
PC
173
174#endif // wxUSE_CONFIG