]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - include/preferencesystem.cpp
Introduce Property<T> to simplify preferences system
[xonotic/netradiant.git] / include / preferencesystem.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #if 0
23
24 #include "preferencesystem.h"
25 #include "preferencedictionary.h"
26
27 #include "xml/xmlparser.h"
28 #include "xml/xmlwriter.h"
29
30
31 void LoadPrefs( PreferenceDictionary& preferences, const char* filename ){
32         TextFileInputStream file( filename );
33         if ( !file.failed() ) {
34                 XMLStreamParser parser( file );
35                 XMLPreferenceDictionaryImporter importer( preferences );
36                 parser.exportXML( importer );
37         }
38         else
39         {
40                 // error
41         }
42 }
43
44 void SavePrefs( PreferenceDictionary& preferences, const char* filename ){
45         TextFileOutputStream file( filename );
46         if ( !file.failed() ) {
47                 XMLStreamWriter writer( file );
48                 XMLPreferenceDictionaryExporter exporter( preferences, "1" );
49                 exporter.exportXML( writer );
50         }
51         else
52         {
53                 // error
54         }
55 }
56
57
58 class StringPreference
59 {
60 public:
61 class Observer
62 {
63 public:
64 virtual void onChanged() = 0;
65 };
66
67 private:
68 CopiedString m_string;
69 Observer& m_observer;
70 public:
71 StringPreference( Observer& observer )
72         : m_observer( observer ){
73 }
74 void importString( const char* value ){
75         m_string = value;
76         m_observer.onChanged();
77 }
78 typedef MemberCaller<StringPreference, void(const char*), &StringPreference::importString> ImportStringCaller;
79 void exportString( Callback<void(const char *)>& importer ){
80         importer( m_string.c_str() );
81 }
82 typedef MemberCaller<StringPreference, void(Callback<void(const char *)>&), &StringPreference::exportString> ExportStringCaller;
83 };
84
85 inline void int_export( int i, Callback<void(const char *)>& importer ){
86         char buffer[16];
87         sprintf( buffer, "%d", i );
88         importer( buffer );
89 }
90
91 inline int int_import( const char* value ){
92         return atoi( value );
93 }
94
95 class IntPreference
96 {
97 public:
98 class Observer
99 {
100 public:
101 virtual void onChanged() = 0;
102 };
103
104 private:
105 int m_int;
106 Observer& m_observer;
107 public:
108
109 IntPreference( Observer& observer )
110         : m_observer( observer ){
111 }
112 void importString( const char* value ){
113         m_int = int_import( value );
114         m_observer.onChanged();
115 }
116 typedef MemberCaller<IntPreference, void(const char*), &IntPreference::importString> ImportStringCaller;
117 void exportString( Callback<void(const char *)>& importer ){
118         int_export( m_int, importer );
119 }
120 typedef MemberCaller<IntPreference, void(Callback<void(const char *)>&), &IntPreference::exportString> ExportStringCaller;
121 };
122
123 class IntPreferenceImporter
124 {
125 int& m_i;
126 public:
127
128 IntPreferenceImporter( int& i )
129         : m_i( i ){
130 }
131 void importString( const char* value ){
132         m_i = int_import( value );
133 }
134 };
135
136
137 class TestPrefs
138 {
139 public:
140 TestPrefs(){
141         PreferenceDictionary preferences;
142
143         class StringObserver : public StringPreference::Observer
144         {
145 public:
146         void onChanged(){
147                 int bleh = 0;
148         }
149         } string_observer;
150         StringPreference string1( string_observer );
151         string1.importString( "twenty-three" );
152
153         class IntObserver : public IntPreference::Observer
154         {
155 public:
156         void onChanged(){
157                 int bleh = 0;
158         }
159
160         } int_observer;
161         IntPreference int1( int_observer );
162         int1.importString( "23" );
163
164         preferences.registerPreference( "string1", StringPreference::ImportStringCaller( string1 ), StringPreference::ExportStringCaller( string1 ) );
165         preferences.registerPreference( "int1", IntPreference::ImportStringCaller( int1 ), IntPreference::ExportStringCaller( int1 ) );
166
167         LoadPrefs( preferences, "test.pref" );
168         SavePrefs( preferences, "test.pref" );
169
170 }
171 };
172
173 #if 0
174 TestPrefs g_TestPrefs;
175 #endif
176
177 void readpref( PreferenceDictionary& preferences, int& int_variable ){
178         PreferenceDictionary::iterator i = preferences.find( "int_variable" );
179         IntPreferenceImporter importer( int_variable );
180         ( *i ).second.exporter().exportString( importer );
181 }
182
183 void writepref( PreferenceDictionary& preferences, int& int_variable ){
184         PreferenceDictionary::iterator i = preferences.find( "int_variable" );
185         int_export( int_variable, ( *i ).second.importer() );
186 }
187 #endif