Show
Ignore:
Timestamp:
05/14/10 12:16:00 (14 years ago)
Author:
mido
Message:

the functionality of user info was improved, it supports an initialization of root descendant via UI::get directly, however it is save only for static attributes, for dynamically allocated attributes UI::build should be called to handle with intahrence issues

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/user_info.cpp

    r907 r942  
    176176} 
    177177 
     178void UI::call_to_setting( const root &instance, Setting &set, string class_name ) { 
     179        try { 
     180                instance.to_setting ( set ); 
     181        } catch ( SettingException &sttng_xcptn ) { 
     182                string msg = "UIException: method "; 
     183                msg += class_name; 
     184                msg += ".to_setting(Setting&) has thrown a SettingException."; 
     185                throw UISettingException ( msg, sttng_xcptn.getPath() ); 
     186        } 
     187} 
     188 
     189void UI::save ( const root &instance, Setting &element, const string &name ) { 
     190        Setting &set = ( name == "" ) ?  (element.getType()==Setting::TypeArray || element.getType()==Setting::TypeList) ? element.add ( Setting::TypeGroup ) : element  
     191                            : element.add ( name, Setting::TypeGroup ); 
     192 
     193        call_to_setting( instance, set ); 
     194} 
     195 
     196void UI::save ( const root * const instance, Setting &element, const string &name ) { 
     197        Setting &set = ( name == "" ) ?  (element.getType()==Setting::TypeArray || element.getType()==Setting::TypeList) ? element.add ( Setting::TypeGroup ) : element  
     198                            : element.add ( name, Setting::TypeGroup ); 
     199 
     200        // add attribute "class" 
     201        const string &class_name = MappedUI::retrieve_class_name ( &typeid ( *instance ) ); 
     202        Setting &type = set.add ( "class", Setting::TypeString ); 
     203        type = class_name; 
     204 
     205        call_to_setting( *instance, set, class_name ); 
     206} 
     207 
    178208void UI::save ( const int &integer, Setting &element, const string &name ) { 
    179209        Setting &set = ( name == "" ) ? element.add ( Setting::TypeInt ) 
     
    235265} 
    236266 
    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; 
     267 
     268void UI::call_from_setting( root &instance, const Setting &set, string class_name) { 
     269        try { 
     270                instance.from_setting ( set ); 
     271        } catch ( SettingException &sttng_xcptn ) { 
     272                string msg = "UIException: method "; 
     273                msg += class_name; 
     274                msg += ".from_setting(Setting&) has thrown a SettingException."; 
     275                throw UISettingException ( msg, sttng_xcptn.getPath() ); 
     276        } catch ( std::runtime_error &e ) { 
     277                string msg = "UIException: method "; 
     278                msg += class_name; 
     279                msg += " says: "; 
     280                msg += e.what(); 
     281                throw UISettingException ( msg, set ); 
     282        }  
     283 
     284        // validate the new instance 
     285        instance.validate();     
     286} 
     287 
     288void UI::from_setting ( root &instance, const Setting &element ) { 
     289        const SettingResolver link ( element ); 
     290        assert_type( link.result, Setting::TypeGroup ); 
     291        call_from_setting( instance, link.result ); 
    261292} 
    262293 
     
    393424} 
    394425 
    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  
    413426}//namespace