Changeset 907 for library/bdm/base/user_info.h
- Timestamp:
- 04/19/10 12:44:57 (14 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/user_info.h
r870 r907 304 304 //! This method converts a Setting into a real scalar 305 305 static void from_setting ( double &real, const Setting &element ); 306 307 //! This method converts a Setting into a specific log_level_template<T> variant and initialize options properly 308 template< class T> static void from_setting ( log_level_template<T> &log_level, const Setting &element ) 309 { 310 string raw_log_level; 311 UI::get( raw_log_level, element ); 312 Array<string> loaded_log_level = log_level_template<T>::string2Array( raw_log_level ); 313 314 log_level.values.reset(); 315 316 for( int i = 0; i < loaded_log_level.length(); i++ ) 317 for( int j = 0; j < log_level.names().length(); j++ ){ 318 if( loaded_log_level(i) == log_level.names()(j) ) 319 { 320 log_level.values[j] = true; 321 break; 322 } 323 } 324 } 306 //! This method converts a Setting into a log_level variant and initialize options properly 307 static void from_setting ( log_level_base &log_level, const Setting &element ); 325 308 326 309 //! This method converts a Setting into a class T descendant … … 374 357 375 358 //! This method converts a Setting into a descendant of class 376 //! T, wrapped in an instance of shared_ptr<T> .359 //! T, wrapped in an instance of bdm::shared_ptr<T> . 377 360 template<class T> 378 static void from_setting ( shared_ptr<T> &instance, const Setting &element ) {361 static void from_setting ( bdm::shared_ptr<T> &instance, const Setting &element ) { 379 362 T *tmp_inst = 0; 380 363 from_setting ( tmp_inst, element ); … … 398 381 } 399 382 400 401 383 //! This is dummy version of the from_setting method for other, unsupported types. It just throws an exception. 402 384 //! 403 385 //! At the moment, this is the only way how to compile the library without obtaining the compiler error c2665. 404 386 //! The exception can help to find the place where the template is misused and also to correct it. 405 template<class T> static void from_setting ( T &variable_to_load, const Setting &element ) {406 std::string msg = "UIException: from_setting is not implemented for type ";387 // template<class T> static void from_setting ( T &variable_to_load, const Setting &element ) { 388 // std::string msg = "UIException: from_setting is not implemented for type "; 407 389 // try{ 408 variable_to_load.from_setting(element);390 // variable_to_load.from_setting(element); 409 391 /* } catch (...){ 410 392 msg += typeid ( T ).name(); … … 412 394 throw UISettingException ( msg, element ); 413 395 }*/ 414 }396 // } 415 397 416 398 … … 432 414 //! The new instance of type T* is constructed and initialized with values stored in the Setting element[name] 433 415 //! 434 //! If there is not any sub-element named #name and settingPresence is #optional, an empty shared_ptr<T> is returned. When settingPresence is #compulsory, the returnedshared_ptr<T> is never empty (an exception is thrown when the object isn't found).416 //! If there is not any sub-element named #name and settingPresence is #optional, an empty bdm::shared_ptr<T> is returned. When settingPresence is #compulsory, the returned bdm::shared_ptr<T> is never empty (an exception is thrown when the object isn't found). 435 417 template<class T> 436 static shared_ptr<T> build ( const Setting &element, const string &name, SettingPresence settingPresence = optional ) {418 static bdm::shared_ptr<T> build ( const Setting &element, const string &name, SettingPresence settingPresence = optional ) { 437 419 if ( !element.exists ( name ) ) { 438 420 if ( settingPresence == optional ) 439 return shared_ptr<T>();421 return bdm::shared_ptr<T>(); 440 422 else 441 423 throw UISettingException ( "UIException: the compulsory Setting named \"" + name + "\" is missing.", element ); 442 424 } 443 425 444 shared_ptr<T> instance;426 bdm::shared_ptr<T> instance; 445 427 from_setting<T> ( instance, to_child_setting ( element, name ) ); 446 428 return instance; … … 449 431 //! The new instance of type T* is constructed and initialized with values stored in the Setting element[index] 450 432 //! 451 //! If there is not any sub-element indexed by #index, and settingPresence is #optional, an empty shared_ptr<T> is returned. When settingPresence is #compulsory, the returnedshared_ptr<T> is never empty (an exception is thrown when the object isn't found).433 //! If there is not any sub-element indexed by #index, and settingPresence is #optional, an empty bdm::shared_ptr<T> is returned. When settingPresence is #compulsory, the returned bdm::shared_ptr<T> is never empty (an exception is thrown when the object isn't found). 452 434 template<class T> 453 static shared_ptr<T> build ( const Setting &element, const int index, SettingPresence settingPresence = optional ) {435 static bdm::shared_ptr<T> build ( const Setting &element, const int index, SettingPresence settingPresence = optional ) { 454 436 if ( element.getLength() <= index ) { 455 437 if ( settingPresence == optional ) 456 return shared_ptr<T>();438 return bdm::shared_ptr<T>(); 457 439 else { 458 440 stringstream stream; … … 462 444 } 463 445 464 shared_ptr<T> instance;446 bdm::shared_ptr<T> instance; 465 447 from_setting<T> ( instance, to_child_setting ( element, index ) ); 466 448 return instance; … … 471 453 //! Handy in mex files. Use with care. 472 454 template<class T> 473 static shared_ptr<T> build ( const Setting &element, SettingPresence settingPresence = optional ) {474 shared_ptr<T> instance;455 static bdm::shared_ptr<T> build ( const Setting &element, SettingPresence settingPresence = optional ) { 456 bdm::shared_ptr<T> instance; 475 457 from_setting<T> ( instance, element ); 476 458 return instance; … … 545 527 if ( element.getLength() <= index ) { 546 528 if ( settingPresence == optional ) 547 return shared_ptr<T>();529 return bdm::shared_ptr<T>(); 548 530 else { 549 531 stringstream stream; … … 590 572 } 591 573 592 template< class T> static void save ( const shared_ptr<T> &instance, Setting &element, const string &name = "" ) {574 template< class T> static void save ( const bdm::shared_ptr<T> &instance, Setting &element, const string &name = "" ) { 593 575 save<T> ( instance.get(), element, name ); 594 576 } … … 603 585 } 604 586 605 // The only difference from classical UserInfo approach to log_level_template is the fact 587 //! A matrix(of type mat) is stored in the new child Setting appended to the passed element 588 static void save ( const mat &matrix, Setting &element, const string &name = "" ); 589 590 //! An integer vector (of type ivec) is stored in the new child Setting appended to the passed element 591 static void save ( const ivec &vec, Setting &element, const string &name = "" ); 592 593 //! A double vector (of type vec) is stored in the new child Setting appended to the passed element 594 static void save ( const vec &vector, Setting &element, const string &name = "" ); 595 596 //! A string is stored in the new child Setting appended to the passed element 597 static void save ( const string &str, Setting &element, const string &name = "" ); 598 599 //! An integer is stored in the new child Setting appended to the passed element 600 static void save ( const int &integer, Setting &element, const string &name = "" ); 601 602 //! A double is stored in the new child Setting appended to the passed element 603 static void save ( const double &real, Setting &element, const string &name = "" ); 604 605 // The only difference from classical UserInfo approach to log_level_template is the fact 606 606 // that the actual UI element related to log_level_template could possibly exists yet. In this case, 607 607 // we do not want to throw an exception like in the case of any other type being loaded … … 609 609 // possible that more instances of log_level_template class (templated with different classes) 610 610 // can be stored in only one line in a configuration file 611 template< class T> static void save ( const log_level_template<T> &log_level, Setting &element, const string &name = "log_level" ) { 612 assert_type ( element, Setting::TypeGroup ); 613 614 string string_to_write; 615 616 Setting *list; 617 if( element.exists( name ) ) 618 { 619 list = &element[name]; 620 assert_type ( *list, Setting::TypeString ); 621 string_to_write = ( const char* ) *list; 622 } 623 else 624 list = &element.add ( name, Setting::TypeString ); 625 626 for( unsigned int i = 0; i < log_level.values.size(); i++ ) 627 if( log_level.values[i] ) // a jeste zkontrolovat zda to jmeno uz neni obsazeno? 628 { 629 if( string_to_write.length() > 0 ) 630 string_to_write = string_to_write + ','; 631 string_to_write = string_to_write + log_level.names()(i); 632 } 633 634 *list = string_to_write; 635 } 636 637 638 //! A matrix(of type mat) is stored in the new child Setting appended to the passed element 639 static void save ( const mat &matrix, Setting &element, const string &name = "" ); 640 641 //! An integer vector (of type ivec) is stored in the new child Setting appended to the passed element 642 static void save ( const ivec &vec, Setting &element, const string &name = "" ); 643 644 //! A double vector (of type vec) is stored in the new child Setting appended to the passed element 645 static void save ( const vec &vector, Setting &element, const string &name = "" ); 646 647 //! A string is stored in the new child Setting appended to the passed element 648 static void save ( const string &str, Setting &element, const string &name = "" ); 649 650 //! An integer is stored in the new child Setting appended to the passed element 651 static void save ( const int &integer, Setting &element, const string &name = "" ); 652 653 //! A double is stored in the new child Setting appended to the passed element 654 static void save ( const double &real, Setting &element, const string &name = "" ); 611 static void save ( const log_level_base &log_level, Setting &element, const string &name = "log_level" ); 612 655 613 //!@} 656 614