Changeset 377 for bdm/user_info.cpp
- Timestamp:
- 06/15/09 18:27:16 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/user_info.cpp
r364 r377 15 15 namespace bdm 16 16 { 17 18 19 17 ///////////////////////// UI FILE ///////////////////////////////////////////// 20 21 18 22 19 UI_File::UI_File () … … 88 85 String_To_UI_Map::const_iterator iter = mapped_strings().find( class_name ); 89 86 if ( iter == mapped_strings().end()) 87 // TODO dat sem vypis seznamu registrovanych trid 90 88 it_error ( "UI error: class " + class_name + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." ); 91 89 return *iter->second; … … 102 100 ///////////////////////// INTERNAL LINK EXPANDER ///////////////////////////////////////////// 103 101 104 UI::SettingsResolver::SettingsResolver( const Setting &potential_link ) 105 { 106 file = NULL; 107 result = &potential_link; 108 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 { 109 108 if ( potential_link.getType() != Setting::TypeString ) 110 return;111 112 109 return potential_link; 110 111 string link = (const char*) potential_link; 113 112 size_t aerobase = link.find('@'); 113 114 const Setting *result; 114 115 if ( aerobase != string::npos ) 115 116 { … … 120 121 } 121 122 else 123 { 124 file = NULL; 125 result = &potential_link; 122 126 while ( !result->isRoot() ) 123 127 result = &result->getParent(); 128 } 124 129 125 130 if ( !result->exists( link ) ) 126 131 ui_error( "linked Setting was not found", potential_link ); 127 132 128 re sult = &(*result)[link];129 } 130 131 UI::Setting sResolver::~SettingsResolver()133 return (*result)[link]; 134 } 135 136 UI::SettingResolver::~SettingResolver() 132 137 { 133 138 if ( file ) delete file; 134 }135 136 const Setting& UI::SettingsResolver::root() const137 {138 return *result;139 139 } 140 140 … … 151 151 void UI::save( const string &str, Setting &element ) 152 152 { 153 Setting & root = element.add( Setting::TypeString );154 root = str;153 Setting &set = element.add( Setting::TypeString ); 154 set = str; 155 155 } 156 156 … … 158 158 { 159 159 160 Setting & root = (name == "") ? element.add( Setting::TypeList )160 Setting &set = (name == "") ? element.add( Setting::TypeList ) 161 161 : element.add( name, Setting::TypeList ); 162 162 163 Setting &cols = root.add( Setting::TypeInt );163 Setting &cols = set.add( Setting::TypeInt ); 164 164 cols = matrix.cols(); 165 165 166 Setting &rows = root.add( Setting::TypeInt );166 Setting &rows = set.add( Setting::TypeInt ); 167 167 rows = matrix.rows(); 168 168 169 Setting &elements = root.add( Setting::TypeArray );169 Setting &elements = set.add( Setting::TypeArray ); 170 170 171 171 // build matrix row-wise … … 183 183 { 184 184 185 Setting & root = (name == "") ? element.add( Setting::TypeArray )185 Setting &set = (name == "") ? element.add( Setting::TypeArray ) 186 186 : element.add( name, Setting::TypeArray ); 187 187 for ( int i=0; i<vector.length(); i++ ) 188 188 { 189 Setting &new_field = root.add(Setting::TypeInt);189 Setting &new_field = set.add(Setting::TypeInt); 190 190 new_field = vector(i); 191 191 } … … 196 196 void UI::save( const vec &vector, Setting &element, const string &name) 197 197 { 198 199 Setting &root = (name == "") ? element.add( Setting::TypeArray ) 198 Setting &set = (name == "") ? element.add( Setting::TypeArray ) 200 199 : element.add( name, Setting::TypeArray ); 201 200 for ( int i=0; i<vector.length(); i++ ) 202 201 { 203 Setting &new_field = root.add(Setting::TypeFloat);202 Setting &new_field = set.add(Setting::TypeFloat); 204 203 new_field = vector(i); 205 204 } … … 211 210 void UI::from_setting( mat& matrix, const Setting &element ) 212 211 { 213 const SettingsResolver link_expander( element ); 214 const Setting &root = link_expander.root(); 215 216 if ( root.isNumber() ) 212 const SettingResolver link( element ); 213 214 if ( link.result.isNumber() ) 217 215 { 218 216 matrix.set_size( 1, 1 ); 219 matrix(0,0) = root;220 return; 221 } 222 223 if ( root.isList() )224 { 225 if ( root.getLength() != 3 )226 ui_error( "the setting supposed to represent a matrix element has wrong syntax", root );227 228 Setting &rows_setting = root[0];229 Setting &cols_setting = root[1];230 Setting &elements = root[2];217 matrix(0,0) = link.result; 218 return; 219 } 220 221 if ( link.result.isList() ) 222 { 223 if ( link.result.getLength() != 3 ) 224 ui_error( "the setting supposed to represent a matrix element has wrong syntax", link.result ); 225 226 Setting &rows_setting = link.result[0]; 227 Setting &cols_setting = link.result[1]; 228 Setting &elements = link.result[2]; 231 229 232 230 ASSERT_UITYPE(cols_setting,TypeInt); … … 238 236 239 237 if ( cols < 0 | rows < 0 ) 240 ui_error( "the dimensions of a matrix has to be non-negative", root );238 ui_error( "the dimensions of a matrix has to be non-negative", link.result ); 241 239 242 240 if ( elements.getLength() != cols * rows ) … … 259 257 } 260 258 261 ui_error( "only numeric types or TypeList are supported as matrix values", root );259 ui_error( "only numeric types or TypeList are supported as matrix values", link.result ); 262 260 } 263 261 … … 265 263 void UI::from_setting( ivec &vector, const Setting &element ) 266 264 { 267 const SettingsResolver link_expander( element ); 268 const Setting &root = link_expander.root(); 269 270 if ( root.isNumber() ) 271 { 272 ASSERT_UITYPE(root,TypeInt); 265 const SettingResolver link( element ); 266 267 if ( link.result.isNumber() ) 268 { 269 ASSERT_UITYPE(link.result,TypeInt); 273 270 vector.set_length( 1 ); 274 vector(0) = root; 275 return; 276 } 277 278 if ( root.isList() ) 279 { 280 if ( root.getLength() != 3 ) 281 ui_error( "the setting supposed to represent a matrix element has wrong syntax", root ); 282 283 Setting &cols_setting = root[0]; 284 Setting &rows_setting = root[1]; 285 Setting &elements = root[2]; 286 287 ASSERT_UITYPE(cols_setting,TypeInt); 288 ASSERT_UITYPE(rows_setting,TypeInt); 289 ASSERT_UITYPE(elements,TypeArray); 290 291 int cols = cols_setting; 292 int rows = rows_setting; 293 294 if ( cols < 0 | rows < 0) 295 ui_error( "the dimensions of a matrix has to be non-negative", root ); 296 297 if ( elements.getLength() != cols * rows ) 298 ui_error( "the count of the matrix elements is incompatible with matrix dimension", elements ); 299 300 if ( cols != 1 & rows !=1) 301 ui_error( "the vector length is invalid, it seems to be rather a matrix", elements ); 302 303 int len = rows * cols; 271 vector(0) = link.result; 272 return; 273 } 274 275 if ( link.result.isList() ) 276 { 277 mat matrix; 278 from_setting( matrix, link.result ); 279 280 if ( matrix.cols() != 1 & matrix.rows() !=1) 281 ui_error( "the vector length is invalid, it seems to be rather a matrix", link.result ); 282 283 int len = matrix.rows() * matrix.cols(); 304 284 vector.set_length ( len ); 305 285 if ( len == 0 ) return; 306 286 287 Setting &elements = link.result[2]; 307 288 ASSERT_UITYPE(elements[0],TypeInt); 308 for ( int i=0; i<len; i++ ) 309 vector(i) = elements[i]; 310 return; 311 } 312 313 if ( root.isArray() ) 314 { 315 int len = root.getLength(); 289 290 291 if ( matrix.cols() == 1 ) 292 for ( int i=0; i<len; i++ ) 293 vector(i) = matrix(i,1); 294 else 295 for ( int i=0; i<len; i++ ) 296 vector(i) = matrix(1,i); 297 return; 298 299 } 300 301 if ( link.result.isArray() ) 302 { 303 int len = link.result.getLength(); 316 304 vector.set_length( len ); 317 305 if ( len == 0 ) return; 318 306 319 ASSERT_UITYPE( root[0],TypeInt);307 ASSERT_UITYPE(link.result[0],TypeInt); 320 308 for ( int i=0; i < len; i++ ) 321 vector(i) = root[i];322 return; 323 } 324 325 ui_error( "only numeric types, TypeArray or TypeList are supported as vector values", root );309 vector(i) = link.result[i]; 310 return; 311 } 312 313 ui_error( "only numeric types, TypeArray or TypeList are supported as vector values", link.result ); 326 314 } 327 315 … … 329 317 void UI::from_setting( vec &vector, const Setting &element ) 330 318 { 331 const SettingsResolver link_expander( element ); 332 const Setting &root = link_expander.root(); 333 334 if ( root.isNumber() ) 319 const SettingResolver link( element ); 320 321 if ( link.result.isNumber() ) 335 322 { 336 323 vector.set_length( 1 ); 337 vector(0) = root; 338 return; 339 } 340 341 if ( root.isList() ) 342 { 343 if ( root.getLength() != 3 ) 344 ui_error( "the setting supposed to represent a matrix element has wrong syntax", root ); 345 346 Setting &cols_setting = root[0]; 347 Setting &rows_setting = root[1]; 348 Setting &elements = root[2]; 349 350 ASSERT_UITYPE(cols_setting,TypeInt); 351 ASSERT_UITYPE(rows_setting,TypeInt); 352 ASSERT_UITYPE(elements,TypeArray); 353 354 int cols = cols_setting; 355 int rows = rows_setting; 356 357 if ( cols < 0 | rows < 0) 358 ui_error( "the dimensions of a matrix has to be non-negative", root ); 359 360 if ( elements.getLength() != cols * rows ) 361 ui_error( "the count of the matrix elements is incompatible with matrix dimension", elements ); 362 363 if ( cols != 1 & rows !=1) 364 ui_error( "the vector length is invalid, it seems to be rather a matrix", elements ); 365 366 int len = rows * cols; 324 vector(0) = link.result; 325 return; 326 } 327 328 if ( link.result.isList() ) 329 { 330 mat matrix; 331 from_setting( matrix, link.result ); 332 333 if ( matrix.cols() != 1 & matrix.rows() !=1) 334 ui_error( "the vector length is invalid, it seems to be rather a matrix", link.result ); 335 336 int len = matrix.rows() * matrix.cols(); 367 337 vector.set_length ( len ); 368 338 if ( len == 0 ) return; 369 339 370 if ( !elements[0].isNumber()) 371 ui_error("a vector element has to be a number", elements[0]); 372 373 for ( int i=0; i<len; i++ ) 374 vector(i) = elements[i]; 375 return; 376 } 377 378 if ( root.isArray() ) 379 { 380 int len = root.getLength(); 340 if ( matrix.cols() == 1 ) 341 for ( int i=0; i<len; i++ ) 342 vector(i) = matrix(i,1); 343 else 344 for ( int i=0; i<len; i++ ) 345 vector(i) = matrix(1,i); 346 return; 347 } 348 349 if ( link.result.isArray() ) 350 { 351 int len = link.result.getLength(); 381 352 vector.set_length( len ); 382 353 if ( len == 0 ) return; 383 354 384 if ( !root[0].isNumber()) 385 ui_error("a vector element has to be a number", root[0]); 386 387 for ( int i=0; i < len; i++ ) { 388 double tmp= (root[i]); 389 vector(i) = tmp; 390 } 391 return; 392 } 393 394 ui_error( "only numeric types, TypeArray or TypeList are supported as vector values", root ); 355 if ( !link.result[0].isNumber()) 356 ui_error("a vector element has to be a number", link.result[0]); 357 358 for ( int i=0; i < len; i++ ) 359 vector(i) = link.result[i]; 360 361 return; 362 } 363 364 ui_error( "only numeric types, TypeArray or TypeList are supported as vector values", link.result ); 395 365 } 396 366