1 | // |
---|
2 | // C++ Implementation: user_info.cpp |
---|
3 | // |
---|
4 | // Description: UI (user info) class for loading/saving objects from/to configuration files. |
---|
5 | // |
---|
6 | // |
---|
7 | // Author: smidl <smidl@utia.cas.cz>, (C) 2009 |
---|
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 | string UIException::format_message ( const string &reason, const string &path ) { |
---|
18 | stringstream ss; |
---|
19 | ss << reason; |
---|
20 | ss << " Check path \"" << path << "\"."; |
---|
21 | return ss.str(); |
---|
22 | } |
---|
23 | |
---|
24 | ///////////////////////////// Class UIFile ///////////////////////////////////////////// |
---|
25 | |
---|
26 | UIFile::UIFile() { |
---|
27 | } |
---|
28 | |
---|
29 | UIFile::UIFile ( const string &file_name ) { |
---|
30 | try { |
---|
31 | readFile ( file_name.c_str() ); |
---|
32 | // this flag has to be set AFTER each file load, that is why it is right here |
---|
33 | setAutoConvert ( true ); |
---|
34 | } catch ( FileIOException f ) { |
---|
35 | string msg = "UI error: file "; |
---|
36 | msg += file_name; |
---|
37 | msg += " not found."; |
---|
38 | bdm_error ( msg ); |
---|
39 | } catch ( ParseException& P ) { |
---|
40 | stringstream msg; |
---|
41 | msg << "UI error: parsing error """ << P.getError() << """ in file " << file_name << " on line " << P.getLine() << "."; |
---|
42 | bdm_error ( msg.str() ); |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | void UIFile::save ( const string &file_name ) { |
---|
48 | try { |
---|
49 | writeFile ( file_name.c_str() ); |
---|
50 | } catch ( FileIOException f ) { |
---|
51 | string msg = "UI error: file "; |
---|
52 | msg += file_name; |
---|
53 | msg += " is inacessible."; |
---|
54 | bdm_error ( msg ); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | UIFile::operator Setting&() { |
---|
59 | return getRoot(); |
---|
60 | } |
---|
61 | |
---|
62 | ///////////////////////////// Class UI::MappedUI ///////////////////////////////////////////// |
---|
63 | |
---|
64 | UI::MappedUI::StringToUIMap& UI::MappedUI::mapped_strings() { |
---|
65 | // this way it is ensured that there is only one instance of StringTpUIMap, and |
---|
66 | // what is more, this declaration leaves its allocation/deallocation on the compiler |
---|
67 | static StringToUIMap var; |
---|
68 | return var; |
---|
69 | } |
---|
70 | |
---|
71 | UI::MappedUI::TypeInfoToStringMap& UI::MappedUI::mapped_type_infos() { |
---|
72 | // this way it is ensured that there is only one instance of TypeInfoToStringMap, and |
---|
73 | // what is more, this declaration leaves its allocation/deallocation on the compiler |
---|
74 | static TypeInfoToStringMap var; |
---|
75 | return var; |
---|
76 | } |
---|
77 | |
---|
78 | void UI::MappedUI::add_class ( const string &class_name, const type_info * const class_type_info, const UI* const ui ) { |
---|
79 | pair< StringToUIMap::iterator, bool> inres = |
---|
80 | mapped_strings().insert ( |
---|
81 | StringToUIMap::value_type ( class_name, ui ) ); |
---|
82 | if ( inres.second ) { |
---|
83 | mapped_type_infos().insert ( |
---|
84 | TypeInfoToStringMap::value_type ( |
---|
85 | class_type_info, class_name ) ); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | void UI::MappedUI::unregistered_class_error ( const string &unregistered_class_name ) { |
---|
90 | stringstream msg; |
---|
91 | msg << "UI error: class " + unregistered_class_name + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." << endl; |
---|
92 | |
---|
93 | if ( mapped_strings().size() ) { |
---|
94 | StringToUIMap::const_iterator iter = mapped_strings().begin(); |
---|
95 | msg << "These classes are already registered: " << iter->first; |
---|
96 | for ( iter++; iter != mapped_strings().end(); iter++ ) |
---|
97 | msg << ", " << iter->first; |
---|
98 | msg << "." << endl; |
---|
99 | } else |
---|
100 | msg << "There is not any registered class yet!" << endl; |
---|
101 | |
---|
102 | bdm_error ( msg.str() ); |
---|
103 | } |
---|
104 | |
---|
105 | const UI& UI::MappedUI::retrieve_ui ( const string &class_name ) { |
---|
106 | StringToUIMap::const_iterator iter = mapped_strings().find ( class_name ); |
---|
107 | if ( iter == mapped_strings().end() ) |
---|
108 | unregistered_class_error ( class_name ); |
---|
109 | |
---|
110 | return *iter->second; |
---|
111 | } |
---|
112 | |
---|
113 | const string& UI::MappedUI::retrieve_class_name ( const type_info * const class_type_info ) { |
---|
114 | TypeInfoToStringMap::const_iterator iter = mapped_type_infos().find ( class_type_info ); |
---|
115 | if ( iter == mapped_type_infos().end() ) |
---|
116 | unregistered_class_error ( "with RTTI name " + string ( class_type_info->name() ) ); |
---|
117 | return iter->second; |
---|
118 | } |
---|
119 | |
---|
120 | ///////////////////////////// Class SettingResolver ///////////////////////////////////////////// |
---|
121 | |
---|
122 | SettingResolver::SettingResolver ( const Setting &potential_link ) |
---|
123 | : result ( initialize_reference ( file, potential_link ) ) { |
---|
124 | } |
---|
125 | |
---|
126 | const Setting& SettingResolver::initialize_reference ( UIFile *&file, const Setting &potential_link ) { |
---|
127 | file = NULL; |
---|
128 | |
---|
129 | if ( potential_link.getType() != Setting::TypeString ) |
---|
130 | return potential_link; |
---|
131 | |
---|
132 | string link = ( const char* ) potential_link; |
---|
133 | size_t aerobase = link.find ( '@' ); |
---|
134 | |
---|
135 | const Setting *result; |
---|
136 | if ( aerobase != string::npos ) { |
---|
137 | string file_name = link.substr ( aerobase + 1, link.length() ); |
---|
138 | file = new UIFile ( file_name ); |
---|
139 | result = & ( Setting& ) ( *file ); |
---|
140 | link = link.substr ( 0, aerobase ); |
---|
141 | } else { |
---|
142 | result = &potential_link; |
---|
143 | while ( !result->isRoot() ) |
---|
144 | result = &result->getParent(); |
---|
145 | } |
---|
146 | |
---|
147 | if ( !result->exists ( link ) ) |
---|
148 | throw UISettingException ( "UIException: linked setting was not found.", string ( ( const char* ) potential_link ) ); |
---|
149 | |
---|
150 | return ( *result ) [link]; |
---|
151 | } |
---|
152 | |
---|
153 | SettingResolver::~SettingResolver() { |
---|
154 | if ( file ) delete file; |
---|
155 | } |
---|
156 | |
---|
157 | ///////////////////////////// Class UI ///////////////////////////////////////////// |
---|
158 | |
---|
159 | void UI::assert_type ( const Setting &element, Setting::Type type ) { |
---|
160 | if ( element.getType() != type ) |
---|
161 | throw UISettingException ( "UIException: wrong setting type.", element ); |
---|
162 | } |
---|
163 | |
---|
164 | const Setting& UI::to_child_setting ( const Setting &element, const int index ) { |
---|
165 | if ( !element.isList() ) |
---|
166 | throw UISettingException ( "UIException: only TypeList elements could be indexed by integers.", element ); |
---|
167 | |
---|
168 | return element[index]; |
---|
169 | } |
---|
170 | |
---|
171 | const Setting& UI::to_child_setting ( const Setting &element, const string &name ) { |
---|
172 | if ( !element.isGroup() ) |
---|
173 | throw UISettingException ( "UIException: only TypeGroup elements could be indexed by strings.", element ); |
---|
174 | |
---|
175 | return element[name]; |
---|
176 | } |
---|
177 | |
---|
178 | void UI::save ( const int &integer, Setting &element, const string &name ) { |
---|
179 | Setting &set = ( name == "" ) ? element.add ( Setting::TypeInt ) |
---|
180 | : element.add ( name, Setting::TypeInt ); |
---|
181 | set = integer; |
---|
182 | } |
---|
183 | |
---|
184 | void UI::save ( const double &real, Setting &element, const string &name ) { |
---|
185 | Setting &set = ( name == "" ) ? element.add ( Setting::TypeFloat ) |
---|
186 | : element.add ( name, Setting::TypeFloat ); |
---|
187 | set = real; |
---|
188 | } |
---|
189 | |
---|
190 | void UI::save ( const string &str, Setting &element, const string &name ) { |
---|
191 | Setting &set = ( name == "" ) ? element.add ( Setting::TypeString ) |
---|
192 | : element.add ( name, Setting::TypeString ); |
---|
193 | set = str; |
---|
194 | } |
---|
195 | |
---|
196 | void UI::save ( const mat &matrix, Setting &element, const string &name ) { |
---|
197 | Setting &set = ( name == "" ) ? element.add ( Setting::TypeList ) |
---|
198 | : element.add ( name, Setting::TypeList ); |
---|
199 | |
---|
200 | Setting &tag = set.add ( Setting::TypeString ); |
---|
201 | tag = "matrix"; |
---|
202 | |
---|
203 | Setting &rows = set.add ( Setting::TypeInt ); |
---|
204 | rows = matrix.rows(); |
---|
205 | |
---|
206 | Setting &cols = set.add ( Setting::TypeInt ); |
---|
207 | cols = matrix.cols(); |
---|
208 | |
---|
209 | Setting &elements = set.add ( Setting::TypeArray ); |
---|
210 | |
---|
211 | // build matrix row-wise |
---|
212 | for ( int i = 0; i < matrix.rows(); i++ ) |
---|
213 | for ( int j = 0; j < matrix.cols(); j++ ) { |
---|
214 | Setting &new_field = elements.add ( Setting::TypeFloat ); |
---|
215 | new_field = matrix ( i, j ); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | void UI::save ( const ivec &vector, Setting &element, const string &name ) { |
---|
220 | Setting &set = ( name == "" ) ? element.add ( Setting::TypeArray ) |
---|
221 | : element.add ( name, Setting::TypeArray ); |
---|
222 | for ( int i = 0; i < vector.length(); i++ ) { |
---|
223 | Setting &new_field = set.add ( Setting::TypeInt ); |
---|
224 | new_field = vector ( i ); |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | void UI::save ( const vec &vector, Setting &element, const string &name ) { |
---|
229 | Setting &set = ( name == "" ) ? element.add ( Setting::TypeArray ) |
---|
230 | : element.add ( name, Setting::TypeArray ); |
---|
231 | for ( int i = 0; i < vector.length(); i++ ) { |
---|
232 | Setting &new_field = set.add ( Setting::TypeFloat ); |
---|
233 | new_field = vector ( i ); |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | void UI::save ( const log_level_base &log_level, Setting &element, const string &name ) { |
---|
238 | assert_type ( element, Setting::TypeGroup ); |
---|
239 | |
---|
240 | string string_to_write; |
---|
241 | |
---|
242 | Setting *list; |
---|
243 | if( element.exists( name ) ) |
---|
244 | { |
---|
245 | list = &element[name]; |
---|
246 | assert_type ( *list, Setting::TypeString ); |
---|
247 | string_to_write = ( const char* ) *list; |
---|
248 | } |
---|
249 | else |
---|
250 | list = &element.add ( name, Setting::TypeString ); |
---|
251 | |
---|
252 | for( unsigned int i = 0; i < log_level.values.size(); i++ ) |
---|
253 | if( log_level.values[i] ) // a jeste zkontrolovat zda to jmeno uz neni obsazeno? |
---|
254 | { |
---|
255 | if( string_to_write.length() > 0 ) |
---|
256 | string_to_write = string_to_write + ','; |
---|
257 | string_to_write = string_to_write + log_level.names()(i); |
---|
258 | } |
---|
259 | |
---|
260 | *list = string_to_write; |
---|
261 | } |
---|
262 | |
---|
263 | void UI::from_setting ( mat& matrix, const Setting &element ) { |
---|
264 | const SettingResolver link ( element ); |
---|
265 | |
---|
266 | if ( link.result.isNumber() ) { |
---|
267 | matrix.set_size ( 1, 1 ); |
---|
268 | matrix ( 0, 0 ) = link.result; |
---|
269 | return; |
---|
270 | } |
---|
271 | |
---|
272 | if ( link.result.isList() ) { |
---|
273 | int data_offset; |
---|
274 | |
---|
275 | if ( link.result.getLength() == 3 ) |
---|
276 | data_offset = 0; |
---|
277 | else if ( link.result.getLength() == 4 ) { |
---|
278 | assert_type ( link.result[0], Setting::TypeString ); |
---|
279 | const char* elem1 = ( const char* ) link.result[0]; |
---|
280 | if ( ( strcmp ( elem1, "matrix" ) ) ) |
---|
281 | throw UISettingException ( "UIException: the setting supposed to represent a matrix element has wrong syntax.", link.result ); |
---|
282 | |
---|
283 | data_offset = 1; |
---|
284 | } else |
---|
285 | throw UISettingException ( "UIException: the setting supposed to represent a matrix element has wrong syntax.", link.result ); |
---|
286 | |
---|
287 | Setting &rows_setting = link.result[0 + data_offset]; |
---|
288 | Setting &cols_setting = link.result[1 + data_offset]; |
---|
289 | Setting &elements = link.result[2 + data_offset]; |
---|
290 | |
---|
291 | // vvv ----- not working in matlab!! |
---|
292 | //assert_type ( cols_setting, Setting::TypeInt ); |
---|
293 | //assert_type ( rows_setting, Setting::TypeInt ); |
---|
294 | //assert_type ( elements, Setting::TypeArray ); |
---|
295 | |
---|
296 | int cols = cols_setting; |
---|
297 | int rows = rows_setting; |
---|
298 | int elems = elements.getLength(); |
---|
299 | |
---|
300 | if ( cols < 0 || rows < 0 ) |
---|
301 | throw UISettingException ( "UIException: the dimensions of a matrix has to be non-negative.", link.result ); |
---|
302 | |
---|
303 | if ( elems != cols * rows ) |
---|
304 | throw UISettingException ( "UIException: the count of the matrix elements is incompatible with matrix dimension.", elements ); |
---|
305 | |
---|
306 | matrix.set_size ( rows, cols ); |
---|
307 | |
---|
308 | if ( cols == 0 || rows == 0 ) |
---|
309 | return; |
---|
310 | |
---|
311 | if ( !elements[0].isNumber() ) |
---|
312 | throw UISettingException ( "UIException: matrix elements have to be numbers.", elements[0] ); |
---|
313 | |
---|
314 | // build matrix row-wise |
---|
315 | int k = 0; |
---|
316 | for ( int i = 0; i < rows; i++ ) |
---|
317 | for ( int j = 0; j < cols; j++ ) |
---|
318 | matrix ( i, j ) = elements[k++]; |
---|
319 | return; |
---|
320 | } |
---|
321 | |
---|
322 | throw UISettingException ( "UIException: only numeric types or TypeList are supported as matrix values.", link.result ); |
---|
323 | } |
---|
324 | |
---|
325 | void UI::from_setting ( vec &vector, const Setting &element ) { |
---|
326 | const SettingResolver link ( element ); |
---|
327 | |
---|
328 | if ( link.result.isNumber() ) { |
---|
329 | vector.set_length ( 1 ); |
---|
330 | vector ( 0 ) = link.result; |
---|
331 | return; |
---|
332 | } |
---|
333 | |
---|
334 | if ( link.result.isList() ) { |
---|
335 | mat matrix; |
---|
336 | from_setting ( matrix, link.result ); |
---|
337 | |
---|
338 | if ( matrix.cols() != 1 && matrix.rows() != 1 && matrix.cols() != 0 ) |
---|
339 | throw UISettingException ( "UIException: the vector length is invalid, it seems to be rather a matrix.", link.result ); |
---|
340 | |
---|
341 | int len = matrix.rows() * matrix.cols(); |
---|
342 | vector.set_length ( len ); |
---|
343 | if ( len == 0 ) return; |
---|
344 | |
---|
345 | if ( matrix.cols() == 1 ) |
---|
346 | for ( int i = 0; i < len; i++ ) |
---|
347 | vector ( i ) = matrix ( i, 0 ); |
---|
348 | else |
---|
349 | for ( int i = 0; i < len; i++ ) |
---|
350 | vector ( i ) = matrix ( 0, i ); |
---|
351 | return; |
---|
352 | } |
---|
353 | |
---|
354 | if ( link.result.isArray() ) { |
---|
355 | int len = link.result.getLength(); |
---|
356 | vector.set_length ( len ); |
---|
357 | if ( len == 0 ) return; |
---|
358 | |
---|
359 | if ( !link.result[0].isNumber() ) |
---|
360 | throw UISettingException ( "UIException: a vector element has to be a number.", link.result[0] ); |
---|
361 | |
---|
362 | for ( int i = 0; i < len; i++ ) |
---|
363 | vector ( i ) = link.result[i]; |
---|
364 | |
---|
365 | return; |
---|
366 | } |
---|
367 | |
---|
368 | throw UISettingException ( "UIException: only numeric types, TypeArray or TypeList are supported as vector values.", link.result ); |
---|
369 | } |
---|
370 | |
---|
371 | void UI::from_setting ( ivec &vector, const Setting &element ) { |
---|
372 | vec double_vector; |
---|
373 | from_setting ( double_vector, element ); |
---|
374 | int len = double_vector.length(); |
---|
375 | vector.set_length ( len ); |
---|
376 | for ( int i = 0; i < len; i++ ) |
---|
377 | vector ( i ) = ( int ) double_vector ( i ); |
---|
378 | } |
---|
379 | |
---|
380 | void UI::from_setting ( string &str, const Setting &element ) { |
---|
381 | assert_type ( element, Setting::TypeString ); |
---|
382 | str = ( const char* ) element; |
---|
383 | } |
---|
384 | |
---|
385 | void UI::from_setting ( int &integer, const Setting &element ) { |
---|
386 | // assert_type ( element, Setting::TypeInt ); |
---|
387 | integer = element; |
---|
388 | } |
---|
389 | |
---|
390 | void UI::from_setting ( double &real, const Setting &element ) { |
---|
391 | // assert_type ( element, Setting::TypeFloat ); |
---|
392 | real = element; |
---|
393 | } |
---|
394 | |
---|
395 | void UI::from_setting ( log_level_base &log_level, const Setting &element ) |
---|
396 | { |
---|
397 | string raw_log_level; |
---|
398 | UI::get( raw_log_level, element ); |
---|
399 | Array<string> loaded_log_level = log_level_base::string2Array( raw_log_level ); |
---|
400 | |
---|
401 | log_level.values.reset(); |
---|
402 | |
---|
403 | for( int i = 0; i < loaded_log_level.length(); i++ ) |
---|
404 | for( int j = 0; j < log_level.names().length(); j++ ){ |
---|
405 | if( loaded_log_level(i) == log_level.names()(j) ) |
---|
406 | { |
---|
407 | log_level.values[j] = true; |
---|
408 | break; |
---|
409 | } |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | }//namespace |
---|