Changeset 394 for library/bdm/base/user_info.cpp
- Timestamp:
- 06/22/09 13:10:28 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/user_info.cpp
r392 r394 15 15 namespace bdm 16 16 { 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 19 UIFile::UIFile() 20 { 21 } 22 23 UIFile::UIFile ( const string &file_name ) 26 24 { 27 25 try 28 26 { 29 27 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 ); 31 30 } 32 31 catch ( FileIOException f ) … … 43 42 44 43 45 //! save UserInfo to the file (typically with an XML extension) 46 void UI_File::save( const string &file_name ) 44 void UIFile::save(const string &file_name ) 47 45 { 48 46 try … … 56 54 } 57 55 58 UI _File::operator Setting&()56 UIFile::operator Setting&() 59 57 { 60 58 return getRoot(); 61 59 } 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 63 UI::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; 67 68 return var; 68 69 } 69 70 70 UI::Mapped_UI::Type_Info_To_String_Map& UI::Mapped_UI::mapped_type_infos() 71 { 72 static Type_Info_To_String_Map var; 71 UI::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; 73 76 return var; 74 77 } 75 78 76 void UI::Mapped _UI::add_class( const string &class_name, const type_info * const class_type_info, const UI* const ui )79 void UI::MappedUI::add_class( const string &class_name, const type_info * const class_type_info, const UI* const ui ) 77 80 { 78 81 pair< const string, const UI* const > new_pair = make_pair( class_name, ui ); … … 81 84 } 82 85 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 ); 86 void 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 105 const UI& UI::MappedUI::retrieve_ui( const string &class_name ) 106 { 107 StringToUIMap::const_iterator iter = mapped_strings().find( class_name ); 86 108 if ( iter == mapped_strings().end()) 87 // TODO dat sem vypis seznamu registrovanych trid88 it_error ( "UI error: class " + class_name + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." ); 89 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 114 const 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 ); 95 117 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()) ); 97 119 return iter->second; 98 120 } 99 121 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 124 UI::SettingResolver::SettingResolver( const Setting &potential_link ) 125 : result( initialize_reference( file, potential_link ) ) 126 { 127 } 128 129 const Setting& UI::SettingResolver::initialize_reference(UIFile *&file, const Setting &potential_link) 107 130 { 108 131 file = NULL; … … 118 141 { 119 142 string file_name = link.substr( aerobase + 1, link.length() ); 120 file = new UI _File( file_name );143 file = new UIFile( file_name ); 121 144 result = &(Setting&)(*file); 122 145 link = link.substr( 0, aerobase ); … … 130 153 131 154 if ( !result->exists( link ) ) 132 ui_error( "linked Setting was not found", potential_link );155 ui_error( "linked setting was not found", potential_link ); 133 156 134 157 return (*result)[link]; … … 140 163 } 141 164 142 ///////////////////////// UI /////////////////////////////////////////////165 ///////////////////////////// Class UI ///////////////////////////////////////////// 143 166 144 167 void UI::ui_error( string message, const Setting &element ) … … 149 172 } 150 173 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 ); 174 const 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 185 const 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 196 void 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 203 void 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 210 void 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 ); 155 214 set = str; 156 215 } … … 158 217 void UI::save( const mat &matrix, Setting &element, const string &name) 159 218 { 160 161 219 Setting &set = (name == "") ? element.add( Setting::TypeList ) 162 : element.add( name, Setting::TypeList );220 : element.add( name, Setting::TypeList ); 163 221 164 222 Setting &cols = set.add( Setting::TypeInt ); … … 179 237 } 180 238 181 182 //! This methods tries to save a integer vector183 239 void UI::save( const ivec &vector, Setting &element, const string &name) 184 240 { 185 186 241 Setting &set = (name == "") ? element.add( Setting::TypeArray ) 187 242 : element.add( name, Setting::TypeArray ); … … 193 248 } 194 249 195 196 //! This methods tries to save a double vector197 250 void UI::save( const vec &vector, Setting &element, const string &name) 198 251 { … … 205 258 } 206 259 } 207 208 209 //! This methods tries to build a new double matrix210 260 211 261 void UI::from_setting( mat& matrix, const Setting &element ) … … 274 324 } 275 325 276 //! This methods tries to build a new integer vector277 326 void UI::from_setting( ivec &vector, const Setting &element ) 278 327 { … … 301 350 Setting &elements = link.result[2]; 302 351 // ASSERT_UITYPE(elements[0],TypeInt); -- spolehame an autoconvert 303 304 352 305 353 if ( matrix.cols() == 1 ) … … 328 376 } 329 377 330 //! This methods tries to build a new double vector331 378 void UI::from_setting( vec &vector, const Setting &element ) 332 379 { … … 379 426 } 380 427 381 382 428 void UI::from_setting( string &str, const Setting &element ) 383 429 { … … 386 432 } 387 433 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 434 void UI::from_setting( int &integer, const Setting &element ) 435 { 436 ASSERT_UITYPE(element,TypeInt); 437 integer = element; 438 } 439 440 void UI::from_setting( double &real, const Setting &element ) 441 { 442 ASSERT_UITYPE(element,TypeFloat); 443 real = element; 444 } 445 446 }