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