]> git.saurik.com Git - wxWidgets.git/blame - src/common/persist.cpp
supporting SetFont(wxNullFont), fixes #10980
[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
7ac1b576
PC
26#if wxUSE_CONFIG
27
0fa541e8
VZ
28#ifndef WX_PRECOMP
29#endif // WX_PRECOMP
30
31#include "wx/persist.h"
32
33// ============================================================================
34// wxPersistenceManager implementation
35// ============================================================================
36
37/* static */
38wxPersistenceManager& wxPersistenceManager::Get()
39{
40 static wxPersistenceManager s_manager;
41
42 return s_manager;
43}
44
5bca8be2
VZ
45wxPersistenceManager::~wxPersistenceManager()
46{
47}
48
0fa541e8
VZ
49wxString
50wxPersistenceManager::GetKey(const wxPersistentObject& who,
51 const wxString& name) const
52{
53 wxString key("Persistent_Options"); // TODO: make this configurable
54 key << wxCONFIG_PATH_SEPARATOR << who.GetKind()
55 << wxCONFIG_PATH_SEPARATOR << who.GetName()
56 << wxCONFIG_PATH_SEPARATOR << name;
57
58 return key;
59}
60
61wxPersistentObject *wxPersistenceManager::Find(void *obj) const
62{
63 const wxPersistentObjectsMap::const_iterator
64 it = m_persistentObjects.find(obj);
65 return it == m_persistentObjects.end() ? NULL : it->second;
66}
67
68wxPersistentObject *
69wxPersistenceManager::Register(void *obj, wxPersistentObject *po)
70{
71 if ( wxPersistentObject *old = Find(obj) )
72 {
73 wxFAIL_MSG( "object is already registered" );
74
75 delete po; // still avoid the memory leaks
76 return old;
77 }
78
79 m_persistentObjects[obj] = po;
80
81 return po;
82}
83
84void wxPersistenceManager::Unregister(void *obj)
85{
86 wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj);
87 wxCHECK_RET( it != m_persistentObjects.end(), "not registered" );
88
89 wxPersistentObject * const po = it->second;
90 m_persistentObjects.erase(it);
91 delete po;
92}
93
94void wxPersistenceManager::Save(void *obj)
95{
96 if ( !m_doSave )
97 return;
98
99 wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj);
100 wxCHECK_RET( it != m_persistentObjects.end(), "not registered" );
101
102 it->second->Save();
103}
104
105bool wxPersistenceManager::Restore(void *obj)
106{
107 if ( !m_doRestore )
108 return false;
109
110 wxPersistentObjectsMap::iterator it = m_persistentObjects.find(obj);
111 wxCHECK_MSG( it != m_persistentObjects.end(), false, "not registered" );
112
113 return it->second->Restore();
114}
115
c7c8fac6
VZ
116namespace
117{
118
119template <typename T>
120inline bool
121DoSaveValue(wxConfigBase *conf, const wxString& key, T value)
122{
123 return conf && conf->Write(key, value);
124}
125
126template <typename T>
127bool
128DoRestoreValue(wxConfigBase *conf, const wxString& key, T *value)
129{
130 return conf && conf->Read(key, value);
131}
132
133} // anonymous namespace
134
135#define wxPERSIST_DEFINE_SAVE_RESTORE_FOR(Type) \
136 bool wxPersistenceManager::SaveValue(const wxPersistentObject& who, \
137 const wxString& name, \
138 Type value) \
139 { \
140 return DoSaveValue(GetConfig(), GetKey(who, name), value); \
141 } \
142 \
143 bool wxPersistenceManager::RestoreValue(const wxPersistentObject& who, \
144 const wxString& name, \
145 Type *value) \
146 { \
147 return DoRestoreValue(GetConfig(), GetKey(who, name), value); \
148 }
149
150wxPERSIST_DEFINE_SAVE_RESTORE_FOR(bool)
151wxPERSIST_DEFINE_SAVE_RESTORE_FOR(int)
152wxPERSIST_DEFINE_SAVE_RESTORE_FOR(long)
153wxPERSIST_DEFINE_SAVE_RESTORE_FOR(wxString)
154
155#undef wxPERSIST_DEFINE_SAVE_RESTORE_FOR
7ac1b576
PC
156
157#endif // wxUSE_CONFIG