Changeset 345
- Timestamp:
- 05/20/09 23:15:05 (16 years ago)
- Files:
-
- 4 modified
- 5 moved
Legend:
- Unmodified
- Added
- Removed
-
bdm/CMakeLists.txt
r322 r345 4 4 SET(BdmStat stat/libDS.cpp stat/libDS.h stat/libFN.cpp stat/libFN.h stat/libBM.cpp stat/libBM.h stat/libEF.cpp stat/libEF.h stat/loggers.cpp stat/loggers.h stat/emix.cpp stat/emix.h) 5 5 SET(BdmEstim estim/libKF.cpp estim/libKF.h estim/libPF.cpp estim/libPF.h estim/arx.cpp estim/arx.h estim/mixef.cpp estim/mixef.h estim/merger.cpp) 6 SET(BdmUI libconfig/libconfigcpp.cc libconfig/grammar.c libconfig/libconfig.c libconfig/scanner.c u ibuilder.cpp uibuilder.h )6 SET(BdmUI libconfig/libconfigcpp.cc libconfig/grammar.c libconfig/libconfig.c libconfig/scanner.c user_info.cpp user_info.h ) 7 7 8 8 # Normal BDM library -
bdm/stat/libBM.h
r344 r345 42 42 43 43 //! This method arrange instance properties according the data stored in the Setting structure 44 virtual void FromSetting( const Setting &root )44 virtual void from_setting( const Setting &root ) 45 45 { 46 46 } 47 47 48 48 //! This method save all the instance properties into the Setting structure 49 virtual void ToSetting( Setting &root )49 virtual void to_setting( Setting &root ) 50 50 { 51 51 } -
bdm/user_info.cpp
r344 r345 11 11 // 12 12 13 #include "u ibuilder.h"13 #include "user_info.h" 14 14 15 15 namespace bdm { 16 16 17 UI:: StringToUIMap::MappedString2UI& UI::StringToUIMap::privateMap()17 UI::Class_To_UI::Class_To_UI_Map& UI::Class_To_UI::private_map() 18 18 { 19 static MappedString2UIvar;19 static Class_To_UI_Map var; 20 20 return var; 21 21 } 22 22 23 void UI:: StringToUIMap::Add( const string &className, pUI pInstance)23 void UI::Class_To_UI::add_class( const string &class_name, UI* ui ) 24 24 { 25 private Map().insert( make_pair( className, pInstance) );25 private_map().insert( make_pair( class_name, ui ) ); 26 26 } 27 27 28 UI ::pUI UI::StringToUIMap::Retrieve( const string &className )28 UI* UI::Class_To_UI::retrieve_ui( const string &class_name ) 29 29 { 30 MappedString2UI::const_iterator iter = privateMap().find( className );31 if( iter == private Map().end()) return NULL;30 Class_To_UI_Map::const_iterator iter = private_map().find( class_name ); 31 if( iter == private_map().end()) return NULL; 32 32 else return iter->second; 33 33 } … … 36 36 37 37 38 UI File::UIFile ( const string &file_name ) : fileName( file_name )38 UI_File::UI_File ( const string &file_name ) : file_name( file_name ) 39 39 { 40 Config::setAutoConvert( true );40 setAutoConvert( true ); 41 41 } 42 42 43 43 //! loads root element from a file 44 void UI File::Load()44 void UI_File::load() 45 45 { 46 46 try 47 47 { 48 Config::readFile( fileName.c_str() );48 readFile( file_name.c_str() ); 49 49 } 50 50 catch ( FileIOException f ) 51 51 { 52 it_error ( "UI : file " + fileName + " not found." );52 it_error ( "UI error: file " + file_name + " not found." ); 53 53 } 54 54 catch ( ParseException& P ) 55 55 { 56 56 stringstream msg; 57 msg << "UI : parsing error """ << P.getError() << """ in file " << fileName << " on line " << P.getLine() << ".";57 msg << "UI error: parsing ui_error """ << P.getError() << """ in file " << file_name << " on line " << P.getLine() << "."; 58 58 it_error ( msg.str() ); 59 59 } … … 61 61 62 62 63 //! Save UserInfo to the file (typically with an XML extension)64 void UI File::Save()63 //! save UserInfo to the file (typically with an XML extension) 64 void UI_File::save() 65 65 { 66 66 try 67 67 { 68 Config::writeFile ( fileName.c_str() );68 writeFile ( file_name.c_str() ); 69 69 } 70 70 catch ( FileIOException f ) 71 71 { 72 it_error( "UI : file " + fileName + " is inacessible." );72 it_error( "UI error: file " + file_name + " is inacessible." ); 73 73 } 74 74 } 75 75 76 UI File::operator Setting&()76 UI_File::operator Setting&() 77 77 { 78 78 return getRoot(); … … 80 80 81 81 } 82 /*83 #include "uibuilder.h"84 82 85 namespace bdm {86 87 //! global map of UIbulder names to instances of UIbuilders. Created by UIREGISTER macro88 UIbuilder::UImap UIbuilder::__uimap__;89 90 bdmroot* UIexternal::build ( Setting &S ) const {91 Config C;92 bdmroot* tmp;93 try {94 C.readFile ( ( const char* ) S["filename"] );95 }96 catch ( ... ) {97 it_error ( "File " + string ( ( const char* ) S["filename"] ) + " not found or broken" );98 }99 try {100 Setting& remS=C.lookup ( ( const char* ) S["path"] );101 UIbuilder::build( remS,tmp );102 }103 catch ( ... ) {104 it_error ( "External field " + string ( S.getPath() ) + " not valid" );105 }106 return tmp;107 };108 UIREGISTER ( UIexternal );109 110 bdmroot* UIinternal::build ( Setting &S ) const {111 bdmroot* tmp;112 try {113 Setting* Stmp = &S;114 do {Stmp=& ( Stmp->getParent() );}115 while ( !Stmp->isRoot() );116 Setting& intS=Stmp->lookup ( ( const char* ) S["path"] );117 UIbuilder::build( intS,tmp );118 }119 catch ( ... ) {120 it_error ( "Internal field " + string ( S.getPath() ) + " not valid" );121 }122 return tmp;123 };124 UIREGISTER ( UIinternal );125 126 void UI_DBG ( Setting &S, const string &spc ) {127 const char *Name=S.getName();128 if ( Name!=NULL ) {cout << spc << std::string ( Name );};129 Setting::Type T=S.getType();130 switch ( T ) {131 case Setting::TypeArray:132 cout << endl;133 for ( int i=0;i<S.getLength();i++ ) {UI_DBG ( S[i], spc+" " );};134 break;135 case Setting::TypeList:136 cout << endl;137 for ( int i=0;i<S.getLength();i++ ) {UI_DBG ( S[i], spc+" " );};138 break;139 case Setting::TypeGroup:140 cout << endl;141 for ( int i=0;i<S.getLength();i++ ) {UI_DBG ( S[i], spc+" " );}142 break;143 case Setting::TypeInt:144 cout << " = ";145 cout << int ( S ) <<endl;146 break;147 case Setting::TypeFloat:148 cout << " = ";149 cout << double ( S ) <<endl;150 break;151 case Setting::TypeString:152 cout << " = ";153 cout << ( const char* ) ( S ) <<endl;154 break;155 case Setting::TypeBoolean:156 cout << " = ";157 cout << bool ( S ) <<endl;158 break;159 default: {cout << "?";};160 }161 }162 163 }164 //UIexternal* UIexternal_instance =new UIexternal();165 */ -
bdm/user_info.h
r344 r345 20 20 using namespace libconfig; 21 21 22 #define UIREGISTER(class Name) template<> ParticularUI<className>& ParticularUI<className>::ui = ParticularUI<className>(#className)22 #define UIREGISTER(class_name) template<> Particular_UI<class_name>& Particular_UI<class_name>::ui = Particular_UI<class_name>(#class_name) 23 23 24 24 #define ASSERT_UITYPE(S,Type) it_assert_debug(S.getType()==Setting::Type, string("Wrong setting type, see input path \"")+string(S.getPath())+string("\"")) … … 37 37 CAudi audi; 38 38 RootElement root("cars.xml"); 39 UserInfo:: Save( audi, root, "TT");40 root. Save();39 UserInfo::save( audi, root, "TT"); 40 root.save(); 41 41 \endcode 42 42 … … 44 44 \code 45 45 RootElement root("cars.xml"); 46 root. Load();47 UserInfo:: Build<T>(root,"TT");46 root.load(); 47 UserInfo::build<T>(root,"TT"); 48 48 \endcode 49 49 */ 50 50 51 class UI File : public Config51 class UI_File : public Config 52 52 { 53 53 private: 54 const string file Name;54 const string file_name; 55 55 56 56 public: 57 57 //! attach new RootElement instance to a file (typically with an XML extension) 58 UI File( const string &file_name );58 UI_File( const string &file_name ); 59 59 60 60 //! loads root element from a file 61 void Load();62 63 //! Save UserInfo to the file (typically with an XML extension)64 void Save();61 void load(); 62 63 //! save UserInfo to the file (typically with an XML extension) 64 void save(); 65 65 66 66 operator Setting&(); … … 69 69 70 70 /*! 71 @brief UserInfo is an abstract is for internal purposes only. Use CompoundUserInfo<T> or Particular UI<T> instead.71 @brief UserInfo is an abstract is for internal purposes only. Use CompoundUserInfo<T> or Particular_UI<T> instead. 72 72 The raison d'etre of this class is to allow pointers to its templated descendants. 73 73 74 74 Also, the main functions of the whole UserInfo library are included within this class, see 75 static methods ' Build' and 'Save'.75 static methods 'build' and 'save'. 76 76 77 77 … … 85 85 { 86 86 private: 87 //! just a typedef shortuct for a constant pointer to UI88 typedef UI* pUI;89 90 87 //! static class encalupsating map of names to related UserInfos 91 88 //! 92 89 //! The key property of this class is that it initilaized the internal map immediately 93 90 //! when it is used for a first time. 94 class StringToUIMap91 class Class_To_UI 95 92 { 96 93 private: 97 94 //! Type definition of mapping which transforms type names to the related user infors 98 typedef map< const string, pUI > MappedString2UI;99 100 //! immediately initialized instance of type MappedString2UI101 static MappedString2UI& privateMap();95 typedef map< const string, UI* > Class_To_UI_Map; 96 97 //! immediately initialized instance of type Class_To_UI_Map 98 static Class_To_UI_Map& private_map(); 102 99 103 100 public: 104 101 //! add a pair key-userinfo into the internal map 105 static void Add( const string &className, pUI pInstance);102 static void add_class( const string &class_name, UI* ui ); 106 103 107 104 //! search for an userinfo related to the passed key within the internal map 108 static pUI Retrieve( const string &className );105 static UI* retrieve_ui( const string &class_name ); 109 106 }; 110 107 111 108 //! internal method assembling a typeless instance from components obtained by the 'AssemblyComponentsFromSetting()' method 112 virtual bdmroot* New() = 0;109 virtual bdmroot* new_instance() = 0; 113 110 114 111 //! type name defined by user 115 const string className; 116 117 protected: 118 119 //! default constructor 120 UI( const string& className ) : className ( className ) 121 { 122 StringToUIMap::Add( className, this ); 123 } 124 125 //! Virtual destructor for future use; 126 virtual ~UI(){}; 127 128 private: 112 const string class_name; 129 113 130 114 //! This methods tries to save an instance of type T (or some of its descendant types) 131 //! and build DOM tree accordingly. Then, it creates a new DOMNode named according class Name115 //! and build DOM tree accordingly. Then, it creates a new DOMNode named according class_name 132 116 //! and connecti it to the passed Setting as a new child node. 133 template<class T> static void ToSetting( T &instance, Setting &element )134 { 135 string &className = ParticularUI<T>::ui.className;117 template<class T> static void to_setting( const T &instance, Setting &root ) 118 { 119 const string &class_name = Particular_UI<T>::ui.class_name; 136 120 137 121 // add attribute "class" 138 122 Setting &type = root.add( "class", Setting::TypeString ); 139 type = class Name;123 type = class_name; 140 124 141 125 try 142 126 { 143 127 // instance disassembling 144 instance. ToSetting( root );128 instance.to_setting( root ); 145 129 } 146 130 catch(SettingException xcptn) 147 131 { 148 it_error ( "UI : the method " + className + ".ToSetting(Setting&) has thrown an exception when filling the setting " + xcptn.getPath() + ". Try to correct this method." );132 it_error ( "UI error: the method " + class_name + ".to_setting(Setting&) has thrown an exception when filling the setting " + xcptn.getPath() + ". Try to correct this method." ); 149 133 } 150 134 } 151 135 152 template<class T> static T* FromSetting( const Setting &element )136 template<class T> static T* from_setting( const Setting &element ) 153 137 { 154 RevealIfLinked revealed_link( element );155 const Setting &root = revealed_link.root();138 const Link_Expander link_expander( element ); 139 const Setting &root = link_expander.root(); 156 140 157 141 ASSERT_UITYPE(root,TypeGroup); 158 142 159 143 // we get a velue stored in the "class" attribute 160 string className; 161 if( !root.lookupValue( "class", className ) ) 162 { 163 stringstream msg; 164 msg << "UI: the class identifier is missing within the setting """ << root.getPath() << """. Check line " 165 << root.getSourceLine() << "."; 166 it_error ( msg.str() ); 167 } 168 144 string class_name; 145 if( !root.lookupValue( "class", class_name ) ) 146 ui_error( "the obligatory ""class"" identifier is missing", root ); 169 147 170 148 // and finally we find a UserInfo related to this type 171 pUI pRelatedUI = StringToUIMap::Retrieve( className );172 if( ! pRelatedUI)173 it_error ( "UI : class " + className + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." );149 UI* related_UI = Class_To_UI::retrieve_ui( class_name ); 150 if( !related_UI) 151 it_error ( "UI error: class " + class_name + " was not properly registered. Use the macro ""UIREGISTER([class name]);"" within your code." ); 174 152 175 bdmroot* pTypelessInstance = pRelatedUI->New();176 177 T* pInstance= NULL;153 bdmroot* typeless_instance = related_UI->new_instance(); 154 155 T* ui = NULL; 178 156 try 179 157 { 180 pInstance = (T*) pTypelessInstance ;158 ui = (T*) typeless_instance ; 181 159 } 182 160 catch(...) 183 161 { 184 it_error ( "UI : class " + className + " is not a descendant of the desired output class. Try to call the UI::Build function with a different type parameter." );162 it_error ( "UI error: class " + class_name + " is not a descendant of the desired output class. Try to call the UI::build function with a different type parameter." ); 185 163 } 186 164 … … 188 166 { 189 167 // instance assembling 190 pInstance->FromSetting( root );168 ui->from_setting( root ); 191 169 } 192 170 catch(SettingException xcptn) 193 171 { 194 string msg = "UI: the method " + className + ".FromSetting(Setting&) has thrown an exception when parsing the setting " + xcptn.getPath() + ". Try to correct this method."; 195 it_error ( msg ); 196 } 197 return pInstance; 172 it_error ( "UI error: the method " + class_name + ".from_setting(Setting&) has thrown an exception when parsing the setting " + xcptn.getPath() + ". Try to correct this method." ); 173 } 174 return ui; 198 175 } 199 176 200 201 202 public:203 177 204 178 // vraci true, kdyz to byl platny link, jinak false.. v pripade chyby konci it_errorem.. 205 179 // do elementu vrati setting prislusny po rozbaleni linku, jinak ponecha beze zmeny 206 class RevealIfLinked180 class Link_Expander 207 181 { 208 182 private: 209 UI File *file;210 const Setting * 183 UI_File *file; 184 const Setting *result; 211 185 212 186 public: 213 187 214 RevealIfLinked( const Setting &element)188 Link_Expander( const Setting &potential_link ) 215 189 { 216 190 file = NULL; 217 result = & element;218 219 if( element.getType() != Setting::TypeString )191 result = &potential_link; 192 193 if( potential_link.getType() != Setting::TypeString ) 220 194 return; 221 195 222 string link = (string) element;196 string link = (string) potential_link; 223 197 size_t aerobase = link.find('@'); 224 198 if( aerobase != string::npos ) 225 199 { 226 200 string file_name = link.substr( aerobase + 1, link.length() ); 227 file = new UIFile( file_name ); 228 file->Load(); 229 // TODO OSETRIT FALSE, vyhodit iterr 230 201 file = new UI_File( file_name ); 202 file->load(); 231 203 result = &(Setting&)(*file); 232 233 204 link = link.substr( 0, aerobase ); 234 205 } … … 238 209 239 210 if( !result->exists( link ) ) 240 { 241 // vyhodi chybu v pripade chyby 242 printf(""); 243 } 211 ui_error( "linked Setting was not found", potential_link ); 212 244 213 result = &(*result)[link]; 245 return; 246 } 247 248 ~RevealIfLinked() 214 } 215 216 ~Link_Expander() 249 217 { 250 218 if( file ) delete file; 251 219 } 252 253 220 254 const Setting& root() 221 const Setting& root() const 255 222 { 256 223 return *result; … … 258 225 }; 259 226 260 private: 261 static const Setting* ToChildSetting( const Setting &element, const int index ) 227 static const Setting* to_child_setting( const Setting &element, const int index ) 262 228 { 263 229 if( !element.isAggregate()) … … 270 236 } 271 237 272 static const Setting* ToChildSetting( const Setting &element, const string &name )238 static const Setting* to_child_setting( const Setting &element, const string &name ) 273 239 { 274 240 if( !element.isGroup() ) … … 281 247 } 282 248 283 static Setting& ToChildSetting( Setting &element, const int index )249 static Setting& to_child_setting( Setting &element, const int index ) 284 250 { 285 251 if( !element.isAggregate()) 286 { 287 // TODO CO ZA TYP? ASI NE GROUP, COZ.. 288 } 252 ui_error( "it is not possible to index non-agregate element by integers", element ); 253 289 254 if( element.getLength() <= index ) 290 { 291 stringstream msg; 292 msg << "UI: there is not any child with index " << index << " in the parsed setting " 293 << element.getPath() << ", check line " << element.getSourceLine() << "."; 294 295 it_error ( msg.str() ); 296 } 255 ui_error( "there is not any child with index " + index, element ); 256 297 257 return element[index]; 298 258 } 299 259 300 static Setting& ToChildSetting( Setting &element, const string &name )260 static Setting& to_child_setting( Setting &element, const string &name ) 301 261 { 302 262 ASSERT_UITYPE(element,TypeGroup); 303 263 if( !element.exists( name ) ) 304 { 305 stringstream msg; 306 msg << "UI: there is not any child named """ << name << """ in the parsed setting " 307 << element.getPath() << ", check line " << element.getSourceLine() << "."; 308 309 it_error ( msg.str() ); 310 } 264 ui_error( "there is not any child named """ + name, element ); 311 265 return element[name]; 312 266 } 313 267 314 268 //! This methods tries to build a new double matrix 315 static bool FromSetting( mat& matrix, const Setting &root ) 316 { 317 ASSERT_UITYPE(root,TypeList); 318 if( root.getLength() != 3 ) 319 { 320 stringstream msg; 321 msg << "UI: the setting " << root.getPath() << """ supposed to represent a matrix element has wrong syntax"". Check line " 322 << root.getSourceLine() << "."; 323 it_error ( msg.str() ); 324 } 325 326 Setting &cols_setting = root[0]; 327 Setting &rows_setting = root[1]; 328 Setting &values = root[2]; 329 330 ASSERT_UITYPE(cols_setting,TypeInt); 331 ASSERT_UITYPE(rows_setting,TypeInt); 332 ASSERT_UITYPE(values,TypeArray); 333 334 int cols = cols_setting; 335 int rows = rows_setting; 336 337 if( values.getLength() != cols * rows ) 338 { 339 stringstream msg; 340 msg << "UI: the lenght of array containing matrix values within setting " << root.getPath() << """ is improper, check line " 341 << root.getSourceLine() << "."; 342 it_error ( msg.str() ); 343 } 344 345 matrix.set_size( rows, cols ); 346 347 if( cols == 0 || rows == 0 ) 348 return true; 349 350 if( !values[0].isNumber() ) 351 { 352 stringstream msg; 353 msg << "UI: the array containing matrix values within setting " << root.getPath() << """ has to contain numeric values only! Check line " 354 << root.getSourceLine() << "."; 355 it_error ( msg.str() ); 356 } 357 358 // Build matrix row-wise 359 int k = 0; 360 for( int i=0;i<rows;i++ ) 361 for( int j=0; j<cols; j++) 362 matrix(i,j) = values[k++]; 363 364 return true; 269 static void from_setting( mat& matrix, const Setting &element ) 270 { 271 const Link_Expander link_expander( element ); 272 const Setting &root = link_expander.root(); 273 274 if( root.isNumber() ) 275 { 276 matrix.set_size( 1, 1 ); 277 matrix(0,0) = root; 278 return; 279 } 280 281 if( root.isList() ) 282 { 283 if( root.getLength() != 3 ) 284 ui_error( "the setting supposed to represent a matrix element has wrong syntax", root ); 285 286 Setting &cols_setting = root[0]; 287 Setting &rows_setting = root[1]; 288 Setting &elements = root[2]; 289 290 ASSERT_UITYPE(cols_setting,TypeInt); 291 ASSERT_UITYPE(rows_setting,TypeInt); 292 ASSERT_UITYPE(elements,TypeArray); 293 294 int cols = cols_setting; 295 int rows = rows_setting; 296 297 if( cols < 0 | rows < 0 ) 298 ui_error( "the dimensions of a matrix has to be non-negative", root ); 299 300 if( elements.getLength() != cols * rows ) 301 ui_error( "the count of the matrix elements is incompatible with matrix dimension", elements ); 302 303 matrix.set_size( rows, cols ); 304 305 if( cols == 0 || rows == 0 ) 306 return; 307 308 if( !elements[0].isNumber() ) 309 ui_error( "matrix elements have to be numbers", elements[0] ); 310 311 // build matrix row-wise 312 int k = 0; 313 for( int i=0;i<rows;i++ ) 314 for( int j=0; j<cols; j++) 315 matrix(i,j) = elements[k++]; 316 return; 317 } 318 319 ui_error( "only numeric types or TypeList are supported as matrix values", root ); 365 320 } 366 321 367 322 //! This methods tries to save a double matrix 368 static void ToSetting(mat &matrix, Setting &root )323 static void to_setting( const mat &matrix, Setting &root ) 369 324 { 370 325 Setting &cols = root.add( Setting::TypeInt ); … … 374 329 rows = matrix.rows(); 375 330 376 Setting & values = root.add( Setting::TypeArray );377 378 // Build matrix row-wise331 Setting &elements = root.add( Setting::TypeArray ); 332 333 // build matrix row-wise 379 334 for( int i=0; i<matrix.rows(); i++ ) 380 335 for( int j=0; j<matrix.cols(); j++) 381 336 { 382 Setting &newField = values.add(Setting::TypeFloat);337 Setting &newField = elements.add(Setting::TypeFloat); 383 338 newField = matrix(i,j); 384 339 } … … 386 341 387 342 //! This methods tries to build a new integer vector 388 static bool FromSetting( ivec &vec, const Setting &root ) 389 { 390 ASSERT_UITYPE(root,TypeArray); 391 392 int len = root.getLength(); 393 vec.set_length( len ); 394 if( len == 0 ) return true; 395 ASSERT_UITYPE(root[0],TypeInt); 396 397 for( int i=0; i < len; i++ ) 398 vec(i) = root[i]; 399 400 return true; 343 static void from_setting( ivec &vec, const Setting &element ) 344 { 345 const Link_Expander link_expander( element ); 346 const Setting &root = link_expander.root(); 347 348 if( root.isNumber() ) 349 { 350 ASSERT_UITYPE(root,TypeInt); 351 vec.set_length( 1 ); 352 vec(0) = root; 353 return; 354 } 355 356 if( root.isList() ) 357 { 358 if( root.getLength() != 3 ) 359 ui_error( "the setting supposed to represent a matrix element has wrong syntax", root ); 360 361 Setting &cols_setting = root[0]; 362 Setting &rows_setting = root[1]; 363 Setting &elements = root[2]; 364 365 ASSERT_UITYPE(cols_setting,TypeInt); 366 ASSERT_UITYPE(rows_setting,TypeInt); 367 ASSERT_UITYPE(elements,TypeArray); 368 369 int cols = cols_setting; 370 int rows = rows_setting; 371 372 if( cols < 0 | rows < 0) 373 ui_error( "the dimensions of a matrix has to be non-negative", root ); 374 375 if( elements.getLength() != cols * rows ) 376 ui_error( "the count of the matrix elements is incompatible with matrix dimension", elements ); 377 378 if( cols != 1 & rows !=1) 379 ui_error( "the vector length is invalid, it seems to be rather a matrix", elements ); 380 381 int len = rows * cols; 382 vec.set_length ( len ); 383 if( len == 0 ) return; 384 385 ASSERT_UITYPE(elements[0],TypeInt); 386 for( int i=0; i<len; i++ ) 387 vec(i) = elements[i]; 388 return; 389 } 390 391 if( root.isArray() ) 392 { 393 int len = root.getLength(); 394 vec.set_length( len ); 395 if( len == 0 ) return; 396 397 ASSERT_UITYPE(root[0],TypeInt); 398 for( int i=0; i < len; i++ ) 399 vec(i) = root[i]; 400 return; 401 } 402 403 ui_error( "only numeric types, TypeArray or TypeList are supported as vector values", root ); 401 404 } 402 405 403 406 //! This methods tries to save an integer vector 404 static void ToSetting(ivec &vec, Setting &root )407 static void to_setting( const ivec &vec, Setting &root ) 405 408 { 406 409 for( int i=0; i<vec.length(); i++ ) … … 412 415 413 416 //! This methods tries to build a new array of strings 414 static bool FromSetting( Array<string> &string_array, const Setting &root) 415 { 416 ASSERT_UITYPE(root,TypeArray); 417 418 int len = root.getLength(); 419 string_array.set_length( len ); 420 if( len == 0 ) return true; 421 ASSERT_UITYPE(root[0],TypeString); 422 423 for( int i=0; i < len; i++ ) 424 string_array(i) = (string)root[i]; 425 426 return true; 417 static void from_setting( Array<string> &string_array, const Setting &element ) 418 { 419 const Link_Expander link_expander( element ); 420 const Setting &root = link_expander.root(); 421 422 if( root.getType() == Setting::TypeString ) 423 { 424 string_array.set_length( 1 ); 425 string_array(0) = (string)root; 426 return; 427 } 428 429 if( root.isArray() ) 430 { 431 int len = root.getLength(); 432 string_array.set_length( len ); 433 if( len == 0 ) return; 434 435 ASSERT_UITYPE(root[0],TypeString); 436 for( int i=0; i < len; i++ ) 437 string_array(i) = (string)root[i]; 438 return; 439 } 440 441 ui_error( "only TypeString or TypeArray are supported as vector of string values", root ); 427 442 } 428 443 429 444 //! This methods tries to save an array of strings 430 static void ToSetting(Array<string> &string_array, Setting &root )445 static void to_setting( const Array<string> &string_array, Setting &root ) 431 446 { 432 447 for( int i=0; i<string_array.length(); i++ ) … … 437 452 } 438 453 454 protected: 455 456 //! default constructor 457 UI( const string& class_name ) : class_name ( class_name ) 458 { 459 Class_To_UI::add_class( class_name, this ); 460 } 461 462 //! Virtual destructor for future use; 463 virtual ~UI(){}; 464 439 465 public: 440 466 467 static void ui_error( string message, const Setting &element ) 468 { 469 stringstream error_message; 470 error_message << "UI ui_error: " << message << "! Check path """ << element.getPath() << """, source line " << element.getSourceLine() << "."; 471 it_error ( error_message.str() ); 472 } 473 441 474 //! This methods tries to build a new instance of type T (or some of its descendant types) 442 //! according to a data stored in a DOMNode named class Name within a child nodes of the passed element.443 //! If an error occurs, it returns a NULL pointer.475 //! according to a data stored in a DOMNode named class_name within a child nodes of the passed element. 476 //! If an ui_error occurs, it returns a NULL pointer. 444 477 445 478 //! Prototype of a UI builder. Return value is by the second argument since it type checking via \c dynamic_cast. 446 template<class T> static T* Build( Setting &element, const int index )447 { 448 return FromSetting<T>( ToChildSetting( element, index ) );449 } 450 451 template<class T> static T* Build( Setting &element, const string &name )479 template<class T> static T* build( Setting &element, const int index ) 480 { 481 return from_setting<T>( to_child_setting( element, index ) ); 482 } 483 484 template<class T> static T* build( Setting &element, const string &name ) 452 485 { 453 return FromSetting<T>( ToChildSetting( element, name ) );486 return from_setting<T>( to_child_setting( element, name ) ); 454 487 } 455 488 456 489 //! This methods tries to save an instance of type T (or some of its descendant types) 457 //! and build DOM tree accordingly. Then, it creates a new DOMNode named according class Name490 //! and build DOM tree accordingly. Then, it creates a new DOMNode named according class_name 458 491 //! and connecti it to the passed Setting as a new child node. 459 template<class T> static void Save( T &instance, Setting &element ) 460 { 461 Setting &root = element.add( Setting::TypeGroup ); 462 ToSetting( instance, root ); 463 } 464 465 //! This methods tries to save an instance of type T (or some of its descendant types) 466 //! and build DOM tree accordingly. Then, it creates a new DOMNode named according className 467 //! and connecti it to the passed Setting as a new child node. 468 template<class T> static void Save( T &instance, Setting &element, const string &name ) 469 { 470 Setting &root = element.add( name, Setting::TypeGroup ); 471 ToSetting( instance, root ); 492 template<class T> static void save( T &instance, Setting &element, const string &name = "") 493 { 494 Setting &root = (name == "") ? element.add( Setting::TypeGroup ) 495 : element.add( name, Setting::TypeGroup ); 496 to_setting( instance, root ); 472 497 } 473 498 474 499 //! This methods tries to build a new double matrix 475 static bool Get( mat& matrix, const Setting &element, const string &name )476 { 477 const Setting *root = ToChildSetting( element, name );500 static bool get( mat& matrix, const Setting &element, const string &name ) 501 { 502 const Setting *root = to_child_setting( element, name ); 478 503 if( !root ) return false; 479 return FromSetting( matrix, *root ); 504 from_setting( matrix, *root ); 505 return true; 480 506 } 481 507 482 508 //! This methods tries to build a new double matrix 483 static bool Get( mat& matrix, const Setting &element, const int index )484 { 485 const Setting *root = ToChildSetting( element, index );509 static bool get( mat& matrix, const Setting &element, const int index ) 510 { 511 const Setting *root = to_child_setting( element, index ); 486 512 if( !root ) return false; 487 return FromSetting( matrix, *root ); 513 from_setting( matrix, *root ); 514 return true; 488 515 } 489 516 490 517 //! This methods tries to save a double matrix 491 static void Save( mat &matrix, Setting &element, const string &name ) 492 { 493 Setting &root = element.add( name, Setting::TypeList ); 494 ToSetting( matrix, root ); 495 } 496 497 //! This methods tries to save a double matrix 498 static void Save( mat &matrix, Setting &element ) 499 { 500 Setting &root = element.add( Setting::TypeList ); 501 ToSetting( matrix, root ); 502 } 503 518 static void save( mat &matrix, Setting &element, const string &name = "" ) 519 { 520 Setting &root = (name == "") ? element.add( Setting::TypeList ) 521 : element.add( name, Setting::TypeList ); 522 to_setting( matrix, root ); 523 } 504 524 505 525 //! This methods tries to build a new double vec 506 static bool Get( ivec& vec, const Setting &element, const string &name )507 { 508 const Setting *root = ToChildSetting( element, name );526 static bool get( ivec& vec, const Setting &element, const string &name ) 527 { 528 const Setting *root = to_child_setting( element, name ); 509 529 if( !root ) return false; 510 return FromSetting( vec, *root ); 530 from_setting( vec, *root ); 531 return true; 511 532 } 512 533 513 534 //! This methods tries to build a new double vec 514 static bool Get( ivec& vec, const Setting &element, const int index )515 { 516 const Setting *root = ToChildSetting( element, index );535 static bool get( ivec& vec, const Setting &element, const int index ) 536 { 537 const Setting *root = to_child_setting( element, index ); 517 538 if( !root ) return false; 518 return FromSetting( vec, *root ); 539 from_setting( vec, *root ); 540 return true; 519 541 } 520 542 521 543 //! This methods tries to save a double vec 522 static void Save( ivec &vec, Setting &element, const string &name ) 523 { 524 Setting &root = element.add( name, Setting::TypeArray ); 525 ToSetting( vec, root ); 526 } 527 528 //! This methods tries to save a double vec 529 static void Save( ivec &vec, Setting &element) 530 { 531 Setting &root = element.add( Setting::TypeArray ); 532 ToSetting( vec, root ); 533 } 534 544 static void save( ivec &vec, Setting &element, const string &name = "" ) 545 { 546 Setting &root = (name == "") ? element.add( Setting::TypeArray ) 547 : element.add( name, Setting::TypeArray ); 548 to_setting( vec, root ); 549 } 535 550 536 551 //! This methods tries to build a new double string_array 537 static bool Get( Array<string> &string_array, const Setting &element, const string &name )538 { 539 const Setting *root = ToChildSetting( element, name );552 static bool get( Array<string> &string_array, const Setting &element, const string &name ) 553 { 554 const Setting *root = to_child_setting( element, name ); 540 555 if( !root ) return false; 541 return FromSetting( string_array, *root ); 556 from_setting( string_array, *root ); 557 return true; 542 558 } 543 559 544 560 //! This methods tries to build a new double string_array 545 static bool Get( Array<string> &string_array, const Setting &element, const int index )546 { 547 const Setting *root = ToChildSetting( element, index );561 static bool get( Array<string> &string_array, const Setting &element, const int index ) 562 { 563 const Setting *root = to_child_setting( element, index ); 548 564 if( !root ) return false; 549 return FromSetting( string_array, *root ); 565 from_setting( string_array, *root ); 566 return true; 550 567 } 551 568 552 569 //! This methods tries to save a double string_array 553 static void Save( Array<string> &string_array, Setting &element, const string &name ) 554 { 555 Setting &root = element.add( name, Setting::TypeArray ); 556 ToSetting( string_array, root ); 557 } 558 559 //! This methods tries to save a double string_array 560 static void Save( Array<string> &string_array, Setting &element ) 561 { 562 Setting &root = element.add( Setting::TypeArray ); 563 ToSetting( string_array, root ); 564 } 565 566 570 static void save( Array<string> &string_array, Setting &element, const string &name = "" ) 571 { 572 Setting &root = (name == "") ? element.add( Setting::TypeArray ) 573 : element.add( name, Setting::TypeArray ); 574 to_setting( string_array, root ); 575 } 567 576 }; 568 577 … … 573 582 own userinfo class prepared). 574 583 */ 575 template<typename T> class Particular UI : private UI584 template<typename T> class Particular_UI : private UI 576 585 { 577 // to permit acces to the Particular UI<T>::ui to the UI class586 // to permit acces to the Particular_UI<T>::ui to the UI class 578 587 friend UI; 579 588 580 589 //! default constructor, which is intentionally declared as private 581 Particular UI<T>( const string &className) : UI( className )590 Particular_UI<T>( const string &class_name) : UI( class_name ) 582 591 { 583 592 }; … … 585 594 //! the only instance of this class (each type T has its own instance) 586 595 //! which is used as a factory for processing related UI 587 static Particular UI<T>& ui;588 589 bdmroot* New()596 static Particular_UI<T>& ui; 597 598 bdmroot* new_instance() 590 599 { 591 600 return new T(); … … 617 626 / 618 627 619 620 //! [Debugging] Print values in current S to cout621 void UI_DBG ( Setting &S, const string &spc );622 623 624 /*625 //! Auxiliary function allowing recursivity in S (too complex, remove?)626 template<class T>627 void UIcall ( Setting &S, void ( *func ) ( Setting&, T ), T Tmp ) {628 ASSERT_UITYPE ( S,TypeGroup );629 // Check if field "class" is present, if not it is not a valid UI630 it_assert_debug ( S.exists ( "class" ), string ( S.getPath() ) +" is not a valid UI!" );631 632 const string typ=S["class"];633 if ( typ=="internal" ) {634 try {635 Setting* Stmp = &S;636 do {Stmp=& ( Stmp->getParent() );}637 while ( !Stmp->isRoot() );638 Setting& intS=Stmp->lookup ( ( const char* ) S["path"] );639 func ( intS, Tmp ); // <======== calling func640 return;641 }642 catch ( ... ) {643 it_error ( "Internal field " + string ( S.getPath() ) + " not valid" );644 }645 }646 if ( typ=="external" ) {647 UIFile C ( S["filename"] );648 try {649 func ( C.lookup ( ( const char* ) S["path"] ), Tmp );650 }651 catch ( ... ) {652 it_error ( "External field " + string ( S.getPath() ) + " not valid" );653 }654 return;655 }656 657 // v======================= calling final func658 func ( S, Tmp );659 };660 661 }662 663 628 */ 664 629 -
library/mex/mexparse.h
r331 r345 27 27 child = val(0,0); 28 28 } else { 29 Setting &child = (key=="") ? setting.add(Setting::TypeGroup) 30 : setting.add(key, Setting::TypeGroup); 31 Setting &elements = child.add("elements", Setting::TypeArray); 32 Setting &cols = child.add("cols", Setting::TypeInt); 29 Setting &child = (key=="") ? setting.add(Setting::TypeList) 30 : setting.add(key, Setting::TypeList); 31 Setting &cols = child.add( Setting::TypeInt ); 32 Setting &rows = child.add( Setting::TypeInt ); 33 Setting &elements = child.add( Setting::TypeArray ); 33 34 cols = val.cols(); 35 rows = val.rows(); 34 36 for (int i=0; i<val.rows(); i++) { 35 37 for (int j=0; j<val.cols(); j++) { -
tests/UI/CMakeLists.txt
r278 r345 1 EXEC( UIbuilder_test)1 EXEC(testUI) -
tests/UI/testUI.cfg
r344 r345 23 23 }; 24 24 elisky : "jardovo"; 25 kati : "skubankovo@ UIbuilder_test_ex.cfg";25 kati : "skubankovo@testUI_ex.cfg"; -
tests/UI/testUI.cpp
r344 r345 1 #include <u ibuilder.h>1 #include <user_info.h> 2 2 #include <string> 3 3 … … 23 23 } 24 24 25 virtual void FromSetting( const Setting &root )25 virtual void from_setting( const Setting &root ) 26 26 { 27 27 root.lookupValue( "year", year ); … … 29 29 } 30 30 31 virtual void ToSetting( Setting &root )31 virtual void to_setting( Setting &root ) 32 32 { 33 33 Setting &year_setting = root.add("year", Setting::TypeInt ); … … 55 55 } 56 56 57 virtual void FromSetting( const Setting &root )57 virtual void from_setting( const Setting &root ) 58 58 { 59 Transport:: FromSetting( root );59 Transport::from_setting( root ); 60 60 61 61 root.lookupValue( "kilometers", kilometers ); 62 62 } 63 63 64 virtual void ToSetting( Setting &root )64 virtual void to_setting( Setting &root ) 65 65 { 66 Transport:: ToSetting( root );66 Transport::to_setting( root ); 67 67 68 68 Setting &kilometers_setting = root.add("kilometers", Setting::TypeInt ); … … 100 100 } 101 101 102 void FromSetting( const Setting &root )102 void from_setting( const Setting &root ) 103 103 { 104 Transport:: FromSetting( root );104 Transport::from_setting( root ); 105 105 106 106 root.lookupValue( "electricLights", electricLights ); 107 107 108 UI:: Get( matr, root, "matr" );108 UI::get( matr, root, "matr" ); 109 109 } 110 110 111 void ToSetting( Setting &root )111 void to_setting( Setting &root ) 112 112 { 113 Transport:: ToSetting( root );113 Transport::to_setting( root ); 114 114 115 115 Setting &electricLights_setting = root.add("electricLights", Setting::TypeBoolean ); 116 116 electricLights_setting = electricLights; 117 117 118 UI:: Save( matr, root, "matr" );118 UI::save( matr, root, "matr" ); 119 119 } 120 120 … … 138 138 Bike author( 1996, "author", true ); 139 139 140 UI File root("UIbuilder_test.cfg");141 UI:: Save( audi, root, "pepikovo");142 UI:: Save( liaz, root, "jardovo");143 UI:: Save( author, root, "ondrejovo");144 root. Save();140 UI_File root("testUI.cfg"); 141 UI::save( audi, root, "pepikovo"); 142 UI::save( liaz, root, "jardovo"); 143 UI::save( author, root, "ondrejovo"); 144 root.save(); 145 145 146 146 cout << "all the transport means were saved correctly" << endl; … … 149 149 150 150 //////////////////////////////////// LOADING //////////////////////////////// 151 UI File root("UIbuilder_test.cfg");152 root. Load();153 Transport *pepikovo = UI:: Build<Transport>( root, "pepikovo");151 UI_File root("testUI.cfg"); 152 root.load(); 153 Transport *pepikovo = UI::build<Transport>( root, "pepikovo"); 154 154 cout << "pepikovo: " << pepikovo->ToString() << endl; 155 Transport *jardovo = UI:: Build<Transport>( root, "jardovo");155 Transport *jardovo = UI::build<Transport>( root, "jardovo"); 156 156 cout << "jardovo: " << jardovo->ToString() << endl; 157 Transport *ondrejovo = UI:: Build<Transport>( root, "ondrejovo");157 Transport *ondrejovo = UI::build<Transport>( root, "ondrejovo"); 158 158 cout << "ondrejovo: " << ondrejovo->ToString() << endl; 159 Transport *elisky = UI:: Build<Transport>( root, "elisky");159 Transport *elisky = UI::build<Transport>( root, "elisky"); 160 160 cout << "elisky: " << elisky->ToString() << endl; 161 Transport *kati = UI:: Build<Transport>( root, "kati");161 Transport *kati = UI::build<Transport>( root, "kati"); 162 162 cout << "kati: " << kati->ToString() << endl; 163 163 getchar();