Show
Ignore:
Timestamp:
06/22/09 13:10:28 (15 years ago)
Author:
mido
Message:

1) UI_File renamed to UIFile
2) part UI's documentation
3) stat/mixtures.h renamed to stat/emix.h and related changes applied

Files:
1 modified

Legend:

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

    r392 r394  
    1515namespace bdm 
    1616{ 
    17 ///////////////////////// UI FILE ///////////////////////////////////////////// 
    18  
    19 UI_File::UI_File () 
    20 { 
    21     setAutoConvert( true ); 
    22 } 
    23  
    24 //! loads root element from a file 
    25 UI_File::UI_File ( const string &file_name ) 
     17///////////////////////////// Class UIFile ///////////////////////////////////////////// 
     18 
     19UIFile::UIFile() 
     20{ 
     21} 
     22 
     23UIFile::UIFile ( const string &file_name ) 
    2624{ 
    2725    try 
    2826    { 
    2927        readFile( file_name.c_str()  ); 
    30         setAutoConvert( true ); 
     28                // this flag has to be set AFTER each file load, that is why it is right here 
     29                setAutoConvert( true ); 
    3130    } 
    3231    catch ( FileIOException f ) 
     
    4342 
    4443 
    45 //! save UserInfo to the file (typically with an XML extension) 
    46 void UI_File::save(  const string &file_name ) 
     44void UIFile::save(const string &file_name ) 
    4745{ 
    4846    try 
     
    5654} 
    5755 
    58 UI_File::operator Setting&() 
     56UIFile::operator Setting&() 
    5957{ 
    6058    return getRoot(); 
    6159} 
    62 ///////////////////////// INTERNAL MAPPED_UI ///////////////////////////////////////////// 
    63  
    64 UI::Mapped_UI::String_To_UI_Map& UI::Mapped_UI::mapped_strings() 
    65 { 
    66     static String_To_UI_Map var; 
     60 
     61///////////////////////////// Class UI::MappedUI ///////////////////////////////////////////// 
     62 
     63UI::MappedUI::StringToUIMap& UI::MappedUI::mapped_strings() 
     64{ 
     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; 
    6768    return var; 
    6869} 
    6970 
    70 UI::Mapped_UI::Type_Info_To_String_Map& UI::Mapped_UI::mapped_type_infos() 
    71 { 
    72     static Type_Info_To_String_Map var; 
     71UI::MappedUI::TypeInfoToStringMap& UI::MappedUI::mapped_type_infos() 
     72{ 
     73        // this way it is ensured that there is only one instance of TypeInfoToStringMap, and  
     74        // what is more, this declaration leaves its allocation/deallocation on the compiler 
     75    static TypeInfoToStringMap var; 
    7376    return var; 
    7477} 
    7578 
    76 void UI::Mapped_UI::add_class( const string &class_name, const type_info * const class_type_info, const UI* const ui ) 
     79void UI::MappedUI::add_class( const string &class_name, const type_info * const class_type_info, const UI* const ui ) 
    7780{ 
    7881    pair< const string, const UI* const > new_pair = make_pair( class_name, ui ); 
     
    8184} 
    8285 
    83 const UI& UI::Mapped_UI::retrieve_ui( const string &class_name ) 
    84 { 
    85     String_To_UI_Map::const_iterator iter = mapped_strings().find( class_name ); 
     86void UI::MappedUI::unregistered_class_error( const string &unregistered_class_name ) 
     87{ 
     88        stringstream msg; 
     89    msg << "UI error: class " + unregistered_class_name + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." << endl; 
     90 
     91        if( mapped_strings().size() ) 
     92        { 
     93            StringToUIMap::const_iterator iter = mapped_strings().begin(); 
     94                msg << "These classes are already registered: " << iter->first; 
     95                for(; iter != mapped_strings().end(); iter++) 
     96                        msg << ", " << iter->first; 
     97                msg << "." << endl; 
     98        }                        
     99        else 
     100                msg << "There is not any registered class yet!" << endl; 
     101         
     102    it_error ( msg.str() );              
     103} 
     104 
     105const UI& UI::MappedUI::retrieve_ui( const string &class_name ) 
     106{ 
     107    StringToUIMap::const_iterator iter = mapped_strings().find( class_name ); 
    86108    if ( iter == mapped_strings().end()) 
    87                 // TODO dat sem vypis seznamu registrovanych trid 
    88         it_error ( "UI error: class " + class_name + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." ); 
    89     return *iter->second; 
    90 } 
    91  
    92 const string& UI::Mapped_UI::retrieve_class_name( const type_info * const class_type_info ) 
    93 { 
    94     Type_Info_To_String_Map::const_iterator iter = mapped_type_infos().find( class_type_info ); 
     109                unregistered_class_error( class_name ); 
     110             
     111        return *iter->second; 
     112} 
     113 
     114const string& UI::MappedUI::retrieve_class_name( const type_info * const class_type_info ) 
     115{ 
     116    TypeInfoToStringMap::const_iterator iter = mapped_type_infos().find( class_type_info ); 
    95117    if ( iter == mapped_type_infos().end()) 
    96         it_error ( "UI error: class with RTTI name " + string(class_type_info->name()) + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." ); 
     118                unregistered_class_error( "with RTTI name " + string(class_type_info->name()) ); 
    97119    return iter->second; 
    98120} 
    99121 
    100 ///////////////////////// INTERNAL LINK EXPANDER ///////////////////////////////////////////// 
    101  
    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) 
     122///////////////////////////// Class UI::SettingResolver ///////////////////////////////////////////// 
     123 
     124UI::SettingResolver::SettingResolver( const Setting &potential_link ) 
     125        : result( initialize_reference( file, potential_link ) ) 
     126{ 
     127} 
     128 
     129const Setting& UI::SettingResolver::initialize_reference(UIFile *&file, const Setting &potential_link) 
    107130{ 
    108131        file = NULL; 
     
    118141    { 
    119142        string file_name = link.substr( aerobase + 1, link.length() ); 
    120         file = new UI_File( file_name ); 
     143        file = new UIFile( file_name ); 
    121144        result = &(Setting&)(*file); 
    122145        link = link.substr( 0, aerobase ); 
     
    130153 
    131154    if ( !result->exists( link ) ) 
    132         ui_error( "linked Setting was not found", potential_link ); 
     155        ui_error( "linked setting was not found", potential_link ); 
    133156 
    134157    return (*result)[link]; 
     
    140163} 
    141164 
    142 ///////////////////////// UI ///////////////////////////////////////////// 
     165///////////////////////////// Class UI ///////////////////////////////////////////// 
    143166 
    144167void UI::ui_error( string message, const Setting &element ) 
     
    149172} 
    150173 
    151 //! This methods - kvuli ukladani pole stringu, dat jen privatne? 
    152 void UI::save( const string &str, Setting &element ) 
    153 { 
    154     Setting &set = element.add( Setting::TypeString ); 
     174const Setting& UI::to_child_setting( const Setting &element, const int index ) 
     175{ 
     176    if ( !element.isList()) 
     177        ui_error( "only TypeList elements could be indexed by integers", element ); 
     178 
     179    if ( element.getLength() <= index ) 
     180        ui_error( "there is not any child with index " + index, element ); 
     181 
     182    return element[index]; 
     183} 
     184 
     185const Setting& UI::to_child_setting( const Setting &element, const string &name ) 
     186{ 
     187    if ( !element.isGroup()) 
     188        ui_error( "only TypeGroup elements could be indexed by strings", element ); 
     189 
     190    if ( !element.exists( name ) ) 
     191        ui_error( "there is not any child named """ + name, element ); 
     192 
     193    return element[name]; 
     194} 
     195 
     196void UI::save( const int &integer, Setting &element, const string &name) 
     197{ 
     198    Setting &set = (name == "") ? element.add( Setting::TypeInt ) 
     199                                            : element.add( name, Setting::TypeInt ); 
     200    set = integer; 
     201} 
     202 
     203void UI::save( const double &real, Setting &element, const string &name) 
     204{ 
     205    Setting &set = (name == "") ? element.add( Setting::TypeFloat ) 
     206                                            : element.add( name, Setting::TypeFloat ); 
     207    set = real; 
     208} 
     209 
     210void UI::save( const string &str, Setting &element, const string &name) 
     211{ 
     212    Setting &set = (name == "") ? element.add( Setting::TypeString ) 
     213                                            : element.add( name, Setting::TypeString ); 
    155214    set = str; 
    156215} 
     
    158217void UI::save( const mat &matrix, Setting &element, const string &name) 
    159218{ 
    160  
    161219    Setting &set = (name == "") ? element.add( Setting::TypeList ) 
    162                     : element.add( name, Setting::TypeList ); 
     220                                            : element.add( name, Setting::TypeList ); 
    163221 
    164222    Setting &cols = set.add( Setting::TypeInt ); 
     
    179237} 
    180238 
    181  
    182 //! This methods tries to save a integer vector 
    183239void UI::save( const ivec &vector, Setting &element, const string &name) 
    184240{ 
    185  
    186241    Setting &set = (name == "") ? element.add( Setting::TypeArray ) 
    187242                    : element.add( name, Setting::TypeArray ); 
     
    193248} 
    194249 
    195  
    196 //! This methods tries to save a double vector 
    197250void UI::save( const vec &vector, Setting &element, const string &name) 
    198251{ 
     
    205258    } 
    206259} 
    207  
    208  
    209 //! This methods tries to build a new double matrix 
    210260 
    211261void UI::from_setting( mat& matrix, const Setting &element ) 
     
    274324} 
    275325 
    276 //! This methods tries to build a new integer vector 
    277326void UI::from_setting( ivec &vector, const Setting &element ) 
    278327{ 
     
    301350                Setting &elements = link.result[2]; 
    302351        // ASSERT_UITYPE(elements[0],TypeInt); -- spolehame an autoconvert 
    303  
    304352 
    305353        if ( matrix.cols() == 1 ) 
     
    328376} 
    329377 
    330 //! This methods tries to build a new double vector 
    331378void UI::from_setting( vec &vector, const Setting &element ) 
    332379{ 
     
    379426} 
    380427 
    381  
    382428void UI::from_setting( string &str, const Setting &element ) 
    383429{ 
     
    386432} 
    387433 
    388  
    389 ///////////////////////// UI FILE ///////////////////////////////////////////// 
    390 //! This methods tries to save an instance of type T (or some of its descendant types) 
    391 //! and build DOM tree accordingly. Then, it creates a new DOMNode named according class_name 
    392 //! and connecti it to the passed Setting as a new child node. 
    393 const Setting& UI::to_child_setting( const Setting &element, const int index ) 
    394 { 
    395     if ( !element.isList()) 
    396         ui_error( "only TypeList elements could be indexed by integers", element ); 
    397  
    398     if ( element.getLength() <= index ) 
    399         ui_error( "there is not any child with index " + index, element ); 
    400  
    401     return element[index]; 
    402 } 
    403  
    404 const Setting& UI::to_child_setting( const Setting &element, const string &name ) 
    405 { 
    406     ASSERT_UITYPE(element,TypeGroup); 
    407     if ( !element.exists( name ) ) 
    408         ui_error( "there is not any child named """ + name, element ); 
    409     return element[name]; 
    410 } 
    411  
    412  
    413 } 
    414  
     434void UI::from_setting( int &integer, const Setting &element ) 
     435{ 
     436    ASSERT_UITYPE(element,TypeInt); 
     437    integer = element; 
     438} 
     439 
     440void UI::from_setting( double &real, const Setting &element ) 
     441{ 
     442    ASSERT_UITYPE(element,TypeFloat); 
     443    real = element; 
     444} 
     445 
     446}