]> git.saurik.com Git - wxWidgets.git/blame - src/common/persist.cpp
The check column needs more space
[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
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 */
36wxPersistenceManager& wxPersistenceManager::Get()
37{
38 static wxPersistenceManager s_manager;
39
40 return s_manager;
41}
42
43wxString
44wxPersistenceManager::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
55wxPersistentObject *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
62wxPersistentObject *
63wxPersistenceManager::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
78void 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
88void 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
99bool 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
c7c8fac6
VZ
110namespace
111{
112
113template <typename T>
114inline bool
115DoSaveValue(wxConfigBase *conf, const wxString& key, T value)
116{
117 return conf && conf->Write(key, value);
118}
119
120template <typename T>
121bool
122DoRestoreValue(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
144wxPERSIST_DEFINE_SAVE_RESTORE_FOR(bool)
145wxPERSIST_DEFINE_SAVE_RESTORE_FOR(int)
146wxPERSIST_DEFINE_SAVE_RESTORE_FOR(long)
147wxPERSIST_DEFINE_SAVE_RESTORE_FOR(wxString)
148
149#undef wxPERSIST_DEFINE_SAVE_RESTORE_FOR