Changeset 377 for bdm/user_info.cpp

Show
Ignore:
Timestamp:
06/15/09 18:27:16 (15 years ago)
Author:
mido
Message:

1) globalni prejmenovani Setting &root na Setting &set
2) smazani par zastaralych adresaru
3) oprava warningu v doc\local
4) prejmenovani SettingsResolver? na SettingResolver? a drobne vylepseni funkcnosti
5) odstranena duplikace kodu v user_info.cpp

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bdm/user_info.cpp

    r364 r377  
    1515namespace bdm 
    1616{ 
    17  
    18  
    1917///////////////////////// UI FILE ///////////////////////////////////////////// 
    20  
    2118 
    2219UI_File::UI_File () 
     
    8885    String_To_UI_Map::const_iterator iter = mapped_strings().find( class_name ); 
    8986    if ( iter == mapped_strings().end()) 
     87                // TODO dat sem vypis seznamu registrovanych trid 
    9088        it_error ( "UI error: class " + class_name + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." ); 
    9189    return *iter->second; 
     
    102100///////////////////////// INTERNAL LINK EXPANDER ///////////////////////////////////////////// 
    103101 
    104 UI::SettingsResolver::SettingsResolver( const Setting &potential_link ) 
    105 { 
    106     file = NULL; 
    107     result = &potential_link; 
    108  
     102UI::SettingResolver::SettingResolver( const Setting &potential_link ): result( initialize_reference( file, potential_link ) ) 
     103{ 
     104} 
     105 
     106const Setting& UI::SettingResolver::initialize_reference(UI_File *&file, const Setting &potential_link) 
     107{ 
    109108    if ( potential_link.getType() !=  Setting::TypeString ) 
    110         return; 
    111  
    112     string link = (const char*) potential_link; 
     109            return potential_link; 
     110         
     111        string link = (const char*) potential_link; 
    113112    size_t aerobase = link.find('@'); 
     113 
     114        const Setting *result; 
    114115    if ( aerobase != string::npos ) 
    115116    { 
     
    120121    } 
    121122    else 
     123        { 
     124                file = NULL; 
     125                result = &potential_link; 
    122126        while ( !result->isRoot() ) 
    123127            result = &result->getParent(); 
     128        } 
    124129 
    125130    if ( !result->exists( link ) ) 
    126131        ui_error( "linked Setting was not found", potential_link ); 
    127132 
    128     result = &(*result)[link]; 
    129 } 
    130  
    131 UI::SettingsResolver::~SettingsResolver() 
     133    return (*result)[link]; 
     134} 
     135 
     136UI::SettingResolver::~SettingResolver() 
    132137{ 
    133138    if ( file ) delete file; 
    134 } 
    135  
    136 const Setting& UI::SettingsResolver::root() const 
    137 { 
    138     return *result; 
    139139} 
    140140 
     
    151151void UI::save( const string &str, Setting &element ) 
    152152{ 
    153     Setting &root = element.add( Setting::TypeString ); 
    154     root = str; 
     153    Setting &set = element.add( Setting::TypeString ); 
     154    set = str; 
    155155} 
    156156 
     
    158158{ 
    159159 
    160     Setting &root = (name == "") ? element.add( Setting::TypeList ) 
     160    Setting &set = (name == "") ? element.add( Setting::TypeList ) 
    161161                    : element.add( name, Setting::TypeList ); 
    162162 
    163     Setting &cols = root.add( Setting::TypeInt ); 
     163    Setting &cols = set.add( Setting::TypeInt ); 
    164164    cols = matrix.cols(); 
    165165 
    166     Setting &rows = root.add( Setting::TypeInt ); 
     166    Setting &rows = set.add( Setting::TypeInt ); 
    167167    rows = matrix.rows(); 
    168168 
    169     Setting &elements = root.add( Setting::TypeArray ); 
     169    Setting &elements = set.add( Setting::TypeArray ); 
    170170 
    171171    // build matrix row-wise 
     
    183183{ 
    184184 
    185     Setting &root = (name == "") ? element.add( Setting::TypeArray ) 
     185    Setting &set = (name == "") ? element.add( Setting::TypeArray ) 
    186186                    : element.add( name, Setting::TypeArray ); 
    187187    for ( int i=0; i<vector.length(); i++ ) 
    188188    { 
    189         Setting &new_field = root.add(Setting::TypeInt); 
     189        Setting &new_field = set.add(Setting::TypeInt); 
    190190        new_field = vector(i); 
    191191    } 
     
    196196void UI::save( const vec &vector, Setting &element, const string &name) 
    197197{ 
    198  
    199     Setting &root = (name == "") ? element.add( Setting::TypeArray ) 
     198    Setting &set = (name == "") ? element.add( Setting::TypeArray ) 
    200199                    : element.add( name, Setting::TypeArray ); 
    201200    for ( int i=0; i<vector.length(); i++ ) 
    202201    { 
    203                 Setting &new_field = root.add(Setting::TypeFloat); 
     202                Setting &new_field = set.add(Setting::TypeFloat); 
    204203        new_field = vector(i); 
    205204    } 
     
    211210void UI::from_setting( mat& matrix, const Setting &element ) 
    212211{ 
    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() ) 
    217215    { 
    218216        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]; 
    231229 
    232230        ASSERT_UITYPE(cols_setting,TypeInt); 
     
    238236 
    239237        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 ); 
    241239 
    242240        if ( elements.getLength() != cols * rows ) 
     
    259257    } 
    260258 
    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 ); 
    262260} 
    263261 
     
    265263void UI::from_setting( ivec &vector, const Setting &element ) 
    266264{ 
    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); 
    273270        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(); 
    304284        vector.set_length ( len ); 
    305285        if ( len == 0 ) return; 
    306286 
     287                Setting &elements = link.result[2]; 
    307288        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(); 
    316304        vector.set_length( len ); 
    317305        if ( len == 0 ) return; 
    318306 
    319         ASSERT_UITYPE(root[0],TypeInt); 
     307        ASSERT_UITYPE(link.result[0],TypeInt); 
    320308        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 ); 
    326314} 
    327315 
     
    329317void UI::from_setting( vec &vector, const Setting &element ) 
    330318{ 
    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() ) 
    335322    { 
    336323        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(); 
    367337        vector.set_length ( len ); 
    368338        if ( len == 0 ) return; 
    369339 
    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(); 
    381352        vector.set_length( len ); 
    382353        if ( len == 0 ) return; 
    383354 
    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 ); 
    395365} 
    396366