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