1 | // |
---|
2 | // C++ Implementation: itpp_ext |
---|
3 | // |
---|
4 | // Description: |
---|
5 | // |
---|
6 | // |
---|
7 | // Author: smidl <smidl@utia.cas.cz>, (C) 2008 |
---|
8 | // |
---|
9 | // Copyright: See COPYING file that comes with this distribution |
---|
10 | // |
---|
11 | // |
---|
12 | |
---|
13 | #include "user_info.h" |
---|
14 | |
---|
15 | namespace bdm |
---|
16 | { |
---|
17 | ///////////////////////// UI FILE ///////////////////////////////////////////// |
---|
18 | |
---|
19 | UI_File::UI_File () |
---|
20 | { |
---|
21 | setAutoConvert( true ); |
---|
22 | } |
---|
23 | |
---|
24 | //! loads root element from a file |
---|
25 | UI_File::UI_File ( const string &file_name ) |
---|
26 | { |
---|
27 | try |
---|
28 | { |
---|
29 | readFile( file_name.c_str() ); |
---|
30 | setAutoConvert( true ); |
---|
31 | } |
---|
32 | catch ( FileIOException f ) |
---|
33 | { |
---|
34 | it_error ( "UI error: file " + file_name + " not found." ); |
---|
35 | } |
---|
36 | catch ( ParseException& P ) |
---|
37 | { |
---|
38 | stringstream msg; |
---|
39 | msg << "UI error: parsing error """ << P.getError() << """ in file " << file_name << " on line " << P.getLine() << "."; |
---|
40 | it_error ( msg.str() ); |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | //! save UserInfo to the file (typically with an XML extension) |
---|
46 | void UI_File::save( const string &file_name ) |
---|
47 | { |
---|
48 | try |
---|
49 | { |
---|
50 | writeFile ( file_name.c_str() ); |
---|
51 | } |
---|
52 | catch ( FileIOException f ) |
---|
53 | { |
---|
54 | it_error( "UI error: file " + file_name + " is inacessible." ); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | UI_File::operator Setting&() |
---|
59 | { |
---|
60 | return getRoot(); |
---|
61 | } |
---|
62 | ///////////////////////// INTERNAL MAPPED_UI ///////////////////////////////////////////// |
---|
63 | |
---|
64 | UI::Mapped_UI::String_To_UI_Map& UI::Mapped_UI::mapped_strings() |
---|
65 | { |
---|
66 | static String_To_UI_Map var; |
---|
67 | return var; |
---|
68 | } |
---|
69 | |
---|
70 | UI::Mapped_UI::Type_Info_To_String_Map& UI::Mapped_UI::mapped_type_infos() |
---|
71 | { |
---|
72 | static Type_Info_To_String_Map var; |
---|
73 | return var; |
---|
74 | } |
---|
75 | |
---|
76 | void UI::Mapped_UI::add_class( const string &class_name, const type_info * const class_type_info, const UI* const ui ) |
---|
77 | { |
---|
78 | pair< const string, const UI* const > new_pair = make_pair( class_name, ui ); |
---|
79 | mapped_strings().insert( new_pair ); |
---|
80 | mapped_type_infos().insert( make_pair( class_type_info, new_pair.first ) ); |
---|
81 | } |
---|
82 | |
---|
83 | const UI& UI::Mapped_UI::retrieve_ui( const string &class_name ) |
---|
84 | { |
---|
85 | String_To_UI_Map::const_iterator iter = mapped_strings().find( class_name ); |
---|
86 | if ( iter == mapped_strings().end()) |
---|
87 | // TODO dat sem vypis seznamu registrovanych trid |
---|
88 | it_error ( "UI error: class " + class_name + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." ); |
---|
89 | return *iter->second; |
---|
90 | } |
---|
91 | |
---|
92 | const string& UI::Mapped_UI::retrieve_class_name( const type_info * const class_type_info ) |
---|
93 | { |
---|
94 | Type_Info_To_String_Map::const_iterator iter = mapped_type_infos().find( class_type_info ); |
---|
95 | if ( iter == mapped_type_infos().end()) |
---|
96 | it_error ( "UI error: class with RTTI name " + string(class_type_info->name()) + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." ); |
---|
97 | return iter->second; |
---|
98 | } |
---|
99 | |
---|
100 | ///////////////////////// INTERNAL LINK EXPANDER ///////////////////////////////////////////// |
---|
101 | |
---|
102 | UI::SettingResolver::SettingResolver( const Setting &potential_link ): result( initialize_reference( file, potential_link ) ) |
---|
103 | { |
---|
104 | } |
---|
105 | |
---|
106 | const Setting& UI::SettingResolver::initialize_reference(UI_File *&file, const Setting &potential_link) |
---|
107 | { |
---|
108 | file = NULL; |
---|
109 | |
---|
110 | if ( potential_link.getType() != Setting::TypeString ) |
---|
111 | return potential_link; |
---|
112 | |
---|
113 | string link = (const char*) potential_link; |
---|
114 | size_t aerobase = link.find('@'); |
---|
115 | |
---|
116 | const Setting *result; |
---|
117 | if ( aerobase != string::npos ) |
---|
118 | { |
---|
119 | string file_name = link.substr( aerobase + 1, link.length() ); |
---|
120 | file = new UI_File( file_name ); |
---|
121 | result = &(Setting&)(*file); |
---|
122 | link = link.substr( 0, aerobase ); |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | result = &potential_link; |
---|
127 | while ( !result->isRoot() ) |
---|
128 | result = &result->getParent(); |
---|
129 | } |
---|
130 | |
---|
131 | if ( !result->exists( link ) ) |
---|
132 | ui_error( "linked Setting was not found", potential_link ); |
---|
133 | |
---|
134 | return (*result)[link]; |
---|
135 | } |
---|
136 | |
---|
137 | UI::SettingResolver::~SettingResolver() |
---|
138 | { |
---|
139 | if ( file ) delete file; |
---|
140 | } |
---|
141 | |
---|
142 | ///////////////////////// UI ///////////////////////////////////////////// |
---|
143 | |
---|
144 | void UI::ui_error( string message, const Setting &element ) |
---|
145 | { |
---|
146 | stringstream error_message; |
---|
147 | error_message << "UI error: " << message << "! Check path """ << element.getPath() << """, source line " << element.getSourceLine() << "."; |
---|
148 | it_error ( error_message.str() ); |
---|
149 | } |
---|
150 | |
---|
151 | //! This methods - kvuli ukladani pole stringu, dat jen privatne? |
---|
152 | void UI::save( const string &str, Setting &element ) |
---|
153 | { |
---|
154 | Setting &set = element.add( Setting::TypeString ); |
---|
155 | set = str; |
---|
156 | } |
---|
157 | |
---|
158 | void UI::save( const mat &matrix, Setting &element, const string &name) |
---|
159 | { |
---|
160 | |
---|
161 | Setting &set = (name == "") ? element.add( Setting::TypeList ) |
---|
162 | : element.add( name, Setting::TypeList ); |
---|
163 | |
---|
164 | Setting &cols = set.add( Setting::TypeInt ); |
---|
165 | cols = matrix.cols(); |
---|
166 | |
---|
167 | Setting &rows = set.add( Setting::TypeInt ); |
---|
168 | rows = matrix.rows(); |
---|
169 | |
---|
170 | Setting &elements = set.add( Setting::TypeArray ); |
---|
171 | |
---|
172 | // build matrix row-wise |
---|
173 | for ( int i=0; i<matrix.rows(); i++ ) |
---|
174 | for ( int j=0; j<matrix.cols(); j++) |
---|
175 | { |
---|
176 | Setting &new_field = elements.add(Setting::TypeFloat); |
---|
177 | new_field = matrix(i,j); |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | //! This methods tries to save a integer vector |
---|
183 | void UI::save( const ivec &vector, Setting &element, const string &name) |
---|
184 | { |
---|
185 | |
---|
186 | Setting &set = (name == "") ? element.add( Setting::TypeArray ) |
---|
187 | : element.add( name, Setting::TypeArray ); |
---|
188 | for ( int i=0; i<vector.length(); i++ ) |
---|
189 | { |
---|
190 | Setting &new_field = set.add(Setting::TypeInt); |
---|
191 | new_field = vector(i); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | //! This methods tries to save a double vector |
---|
197 | void UI::save( const vec &vector, Setting &element, const string &name) |
---|
198 | { |
---|
199 | Setting &set = (name == "") ? element.add( Setting::TypeArray ) |
---|
200 | : element.add( name, Setting::TypeArray ); |
---|
201 | for ( int i=0; i<vector.length(); i++ ) |
---|
202 | { |
---|
203 | Setting &new_field = set.add(Setting::TypeFloat); |
---|
204 | new_field = vector(i); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | //! This methods tries to build a new double matrix |
---|
210 | |
---|
211 | void UI::from_setting( mat& matrix, const Setting &element ) |
---|
212 | { |
---|
213 | const SettingResolver link( element ); |
---|
214 | |
---|
215 | if ( link.result.isNumber() ) |
---|
216 | { |
---|
217 | matrix.set_size( 1, 1 ); |
---|
218 | matrix(0,0) = link.result; |
---|
219 | return; |
---|
220 | } |
---|
221 | |
---|
222 | if ( link.result.isList() ) |
---|
223 | { |
---|
224 | int data_offset; |
---|
225 | |
---|
226 | if ( link.result.getLength() == 3 ) |
---|
227 | data_offset = 0; |
---|
228 | else if ( link.result.getLength() == 4 ) |
---|
229 | { |
---|
230 | ASSERT_UITYPE(link.result[0],TypeString); |
---|
231 | if( link.result[0] != "matrix" ) |
---|
232 | ui_error( "the setting supposed to represent a matrix element has wrong syntax", link.result ); |
---|
233 | |
---|
234 | data_offset = 1; |
---|
235 | } |
---|
236 | else |
---|
237 | ui_error( "the setting supposed to represent a matrix element has wrong syntax", link.result ); |
---|
238 | |
---|
239 | Setting &rows_setting = link.result[0 + data_offset]; |
---|
240 | Setting &cols_setting = link.result[1 + data_offset]; |
---|
241 | Setting &elements = link.result[2 + data_offset]; |
---|
242 | |
---|
243 | ASSERT_UITYPE(cols_setting,TypeInt); |
---|
244 | ASSERT_UITYPE(rows_setting,TypeInt); |
---|
245 | ASSERT_UITYPE(elements,TypeArray); |
---|
246 | |
---|
247 | int cols = cols_setting; |
---|
248 | int rows = rows_setting; |
---|
249 | |
---|
250 | if ( cols < 0 | rows < 0 ) |
---|
251 | ui_error( "the dimensions of a matrix has to be non-negative", link.result ); |
---|
252 | |
---|
253 | if ( elements.getLength() != cols * rows ) |
---|
254 | ui_error( "the count of the matrix elements is incompatible with matrix dimension", elements ); |
---|
255 | |
---|
256 | matrix.set_size( rows, cols ); |
---|
257 | |
---|
258 | if ( cols == 0 || rows == 0 ) |
---|
259 | return; |
---|
260 | |
---|
261 | if ( !elements[0].isNumber() ) |
---|
262 | ui_error( "matrix elements have to be numbers", elements[0] ); |
---|
263 | |
---|
264 | // build matrix row-wise |
---|
265 | int k = 0; |
---|
266 | for ( int i=0; i<rows; i++ ) |
---|
267 | for ( int j=0; j<cols; j++) |
---|
268 | matrix(i,j) = elements[k++]; |
---|
269 | return; |
---|
270 | } |
---|
271 | |
---|
272 | ui_error( "only numeric types or TypeList are supported as matrix values", link.result ); |
---|
273 | } |
---|
274 | |
---|
275 | //! This methods tries to build a new integer vector |
---|
276 | void UI::from_setting( ivec &vector, const Setting &element ) |
---|
277 | { |
---|
278 | const SettingResolver link( element ); |
---|
279 | |
---|
280 | if ( link.result.isNumber() ) |
---|
281 | { |
---|
282 | ASSERT_UITYPE(link.result,TypeInt); |
---|
283 | vector.set_length( 1 ); |
---|
284 | vector(0) = link.result; |
---|
285 | return; |
---|
286 | } |
---|
287 | |
---|
288 | if ( link.result.isList() ) |
---|
289 | { |
---|
290 | mat matrix; |
---|
291 | from_setting( matrix, link.result ); |
---|
292 | |
---|
293 | if ( matrix.cols() != 1 & matrix.rows() !=1) |
---|
294 | ui_error( "the vector length is invalid, it seems to be rather a matrix", link.result ); |
---|
295 | |
---|
296 | int len = matrix.rows() * matrix.cols(); |
---|
297 | vector.set_length ( len ); |
---|
298 | if ( len == 0 ) return; |
---|
299 | |
---|
300 | Setting &elements = link.result[2]; |
---|
301 | ASSERT_UITYPE(elements[0],TypeInt); |
---|
302 | |
---|
303 | |
---|
304 | if ( matrix.cols() == 1 ) |
---|
305 | for ( int i=0; i<len; i++ ) |
---|
306 | vector(i) = matrix(i,1); |
---|
307 | else |
---|
308 | for ( int i=0; i<len; i++ ) |
---|
309 | vector(i) = matrix(1,i); |
---|
310 | return; |
---|
311 | |
---|
312 | } |
---|
313 | |
---|
314 | if ( link.result.isArray() ) |
---|
315 | { |
---|
316 | int len = link.result.getLength(); |
---|
317 | vector.set_length( len ); |
---|
318 | if ( len == 0 ) return; |
---|
319 | |
---|
320 | ASSERT_UITYPE(link.result[0],TypeInt); |
---|
321 | for ( int i=0; i < len; i++ ) |
---|
322 | vector(i) = link.result[i]; |
---|
323 | return; |
---|
324 | } |
---|
325 | |
---|
326 | ui_error( "only numeric types, TypeArray or TypeList are supported as vector values", link.result ); |
---|
327 | } |
---|
328 | |
---|
329 | //! This methods tries to build a new double vector |
---|
330 | void UI::from_setting( vec &vector, const Setting &element ) |
---|
331 | { |
---|
332 | const SettingResolver link( element ); |
---|
333 | |
---|
334 | if ( link.result.isNumber() ) |
---|
335 | { |
---|
336 | vector.set_length( 1 ); |
---|
337 | vector(0) = link.result; |
---|
338 | return; |
---|
339 | } |
---|
340 | |
---|
341 | if ( link.result.isList() ) |
---|
342 | { |
---|
343 | mat matrix; |
---|
344 | from_setting( matrix, link.result ); |
---|
345 | |
---|
346 | if ( matrix.cols() != 1 & matrix.rows() !=1) |
---|
347 | ui_error( "the vector length is invalid, it seems to be rather a matrix", link.result ); |
---|
348 | |
---|
349 | int len = matrix.rows() * matrix.cols(); |
---|
350 | vector.set_length ( len ); |
---|
351 | if ( len == 0 ) return; |
---|
352 | |
---|
353 | if ( matrix.cols() == 1 ) |
---|
354 | for ( int i=0; i<len; i++ ) |
---|
355 | vector(i) = matrix(i,1); |
---|
356 | else |
---|
357 | for ( int i=0; i<len; i++ ) |
---|
358 | vector(i) = matrix(1,i); |
---|
359 | return; |
---|
360 | } |
---|
361 | |
---|
362 | if ( link.result.isArray() ) |
---|
363 | { |
---|
364 | int len = link.result.getLength(); |
---|
365 | vector.set_length( len ); |
---|
366 | if ( len == 0 ) return; |
---|
367 | |
---|
368 | if ( !link.result[0].isNumber()) |
---|
369 | ui_error("a vector element has to be a number", link.result[0]); |
---|
370 | |
---|
371 | for ( int i=0; i < len; i++ ) |
---|
372 | vector(i) = link.result[i]; |
---|
373 | |
---|
374 | return; |
---|
375 | } |
---|
376 | |
---|
377 | ui_error( "only numeric types, TypeArray or TypeList are supported as vector values", link.result ); |
---|
378 | } |
---|
379 | |
---|
380 | |
---|
381 | void UI::from_setting( string &str, const Setting &element ) |
---|
382 | { |
---|
383 | ASSERT_UITYPE(element,TypeString); |
---|
384 | str = (const char*) element; |
---|
385 | } |
---|
386 | |
---|
387 | |
---|
388 | ///////////////////////// UI FILE ///////////////////////////////////////////// |
---|
389 | //! This methods tries to save an instance of type T (or some of its descendant types) |
---|
390 | //! and build DOM tree accordingly. Then, it creates a new DOMNode named according class_name |
---|
391 | //! and connecti it to the passed Setting as a new child node. |
---|
392 | const Setting& UI::to_child_setting( const Setting &element, const int index ) |
---|
393 | { |
---|
394 | if ( !element.isList()) |
---|
395 | ui_error( "only TypeList elements could be indexed by integers", element ); |
---|
396 | |
---|
397 | if ( element.getLength() <= index ) |
---|
398 | ui_error( "there is not any child with index " + index, element ); |
---|
399 | |
---|
400 | return element[index]; |
---|
401 | } |
---|
402 | |
---|
403 | const Setting& UI::to_child_setting( const Setting &element, const string &name ) |
---|
404 | { |
---|
405 | ASSERT_UITYPE(element,TypeGroup); |
---|
406 | if ( !element.exists( name ) ) |
---|
407 | ui_error( "there is not any child named """ + name, element ); |
---|
408 | return element[name]; |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | } |
---|