Changeset 156
- Timestamp:
- 09/01/08 15:08:50 (16 years ago)
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/userinfo.h
r152 r156 27 27 28 28 /*! 29 @brief Xerces interface class 30 31 This class is used to interact with the Xerces library. 29 @brief Class initializing Xerces library 30 31 This class is used to initialize the Xerces library. Namely 32 to call static method XMLPlatformUtils::Initialize(). It should 33 be called just once, at previously to any other usage of the Xerces 34 library. This behaviour is assured by a quite complicated design 35 based on the use of an inner singleton class named 'XercesConnector'. 36 The point is to be the first initialized class even within the set of all 37 (global) static variables. For better understanding, find instances of 38 this class and look at their context. 32 39 */ 33 /// TODO slo by i bez vnorene tridy?34 40 class AssertXercesIsAlive 35 41 { 36 42 private: 43 //! Singleton class implementing the key property of the 'AssertXercesIsAlive' class 37 44 class XercesConnector 38 45 { 39 46 private: 40 //!default constructor 47 //! Default constructor, which is intentionally declared as private and called only from static 48 //! method 'StayAlive()'. Therfore, it is called only once in the run of a application! 41 49 XercesConnector() 42 50 { … … 52 60 53 61 public: 54 //! The only global instance of the XercesConnector class 55 // potrebujeme inicializaci hned v okamziku zavolani!! 62 //! This method just touches the only instance of the XercesConnector class 63 //! which is declared as a static local variable. 64 //! 65 //! This way we know that Xerces was initialized properly when returning from StayAlive(). 66 //! And what is more, the local nature of the inner variable prevents us before multiple 67 //! initializations. 56 68 static void StayAlive() 57 69 { … … 62 74 63 75 public: 76 //!default constructor 64 77 AssertXercesIsAlive() 65 78 { … … 68 81 }; 69 82 70 83 /*! 84 @brief Abstract class declaring general properties of a frame for data binding 85 */ 71 86 class BindingFrame 72 87 { 73 88 private: 74 // okomentovat, ze musi byt prvni 89 //! This private attribute has to be declared as the first attribute in the class. 90 //! Only this way we can be sure it's constructor is called as the first 91 //! and thus Xerces is initialized properly and right on time 75 92 AssertXercesIsAlive dummy; 93 76 94 protected: 95 //!default constructor 77 96 BindingFrame(); 78 97 79 //! function transcodes Xerces' XMLCh-based strings into C++ strings98 //! function which transcodes Xerces' XMLCh-based strings into C++ strings 80 99 string XMLCh2str( const XMLCh* const XMLCh_str ); 81 100 101 string removeSpaces(const string &str) 102 { 103 std::string temp; 104 for (unsigned int i = 0; i < str.length(); i++) 105 if (str[i] == ' ') temp += '_'; 106 else temp += str[i]; 107 return temp; 108 } 109 82 110 public: 83 // nacpe na prislusne pointery, kdyztak da NULL84 virtual void Assembly FromXML( DOMElement &element ) = 0;85 86 // uvolneni pameti po slozeni objektu, tj. lze jedine po AssemblyBD111 //! This method parse DOMElement, finds proper DOMNode and fills binded data accordingly 112 virtual void AssemblyComponentsFromXML( DOMElement &element ) = 0; 113 114 //! A method for releasing memory allocated within the 'AssemblyComponentsFromXML()' method 87 115 virtual void ReleaseMemory() {} 88 116 89 // vrati bool, pokud se povedlo rozebrat a naplnit element 90 // nebude tu nahodou jeste ten help - string?? 91 virtual bool DisassamblyToXML( DOMElement &element ) = 0; 92 }; 93 117 //! This method reads binded data, fill them into a new DOMNode, which then 118 //! appends to the passed DOMElement 119 virtual bool DisassemblyComponentsToXML( DOMElement &element ) = 0; 120 }; 121 122 /*! 123 @brief Abstract class declaring general properties of a frame for data binding 124 */ 94 125 class Attribute 95 126 { 96 127 private: 97 // okomentovat, ze musi byt prvni 128 //! This private attribute has to be declared as the first attribute in the class. 129 //! Only this way we can be sure it's constructor is called as the first 130 //! and thus Xerces is initialized properly and right on time 98 131 AssertXercesIsAlive dummy; 99 132 133 //! an attribute name 100 134 const XMLCh* const transcodedAttributeName; 101 135 102 136 public: 137 //! Default constructor fixing a name of the related attribute 103 138 Attribute( string attributeName ); 104 139 105 140 ~Attribute(); 106 141 107 // nacpe na prislusne pointery, kdyztak da NULL142 //! This method parse DOMElement, finds proper attribute and returns its value 108 143 string& Get( DOMElement &element ) const; 109 144 145 //! This method adds the passed string as an new attribute into the passed DOMElement 110 146 void Set( DOMElement &element, const string &str ) const; 111 147 148 //! Static member, an instance related to an attribute named 'help' 112 149 static const Attribute help; 113 150 151 //! Static member, a constant instance related to an attribute named 'type' 114 152 static const Attribute type; 115 153 154 //! Static member, a constant instance related to the attribute named 'value' 116 155 static const Attribute value; 117 156 }; … … 120 159 121 160 /*! 122 @brief UserInfo class is for internal purposes only. Use CompoundUserInfo<T> instead. 123 124 The raison d'etre of this class is to allow pointers to (the main part of) 125 CompoundUserInfo<T> objects even for different generic types. 161 @brief UserInfo is an abstract is for internal purposes only. Use CompoundUserInfo<T> or ValuedUserInfo<T> instead. 162 The raison d'etre of this class is to allow pointers to its templated descendants. 163 164 Also, the main functions of the whole UserInfo library are included within this class, see 165 static methods 'Assembly' and 'Disassembly'. 126 166 */ 127 167 class UserInfo : protected BindingFrame 128 168 { 129 169 private: 170 //! just a typedef shortuct for a constant pointer to UserInfo 130 171 typedef UserInfo* const pUserInfo; 131 172 132 // immediately initialized map..!! 173 //! static class encalupsating map of names to related UserInfos 174 //! 175 //! The key property of this class is that it initilaized the internal map immediately 176 //! when it is used for a first time. 133 177 class StringToUIMap 134 178 { … … 137 181 typedef map< const string, pUserInfo > MappedString2UI; 138 182 183 //! immediately initialized instance of type MappedString2UI 139 184 static MappedString2UI& privateMap(); 140 185 141 186 public: 187 //! add a pair key-userinfo into the internal map 142 188 static void Add( string key, pUserInfo pInstance ); 143 189 190 //! search for an userinfo related to the passed key within the internal map 144 191 static pUserInfo Retrieve( string key ); 145 192 }; 146 193 194 //! internal method assembling a typeless instance from components obtained by the 'AssemblyComponentsFromXML()' method 147 195 virtual void* AssemblyTypelessInstance() = 0; 148 196 197 //! internal method disassembling a typeless instance to components which are processed by the 'DisassemblyComponentsToXML()' method 149 198 virtual bool DisassemblyTypelessInstance(void* pInstance) = 0; 150 199 200 //! an user-friendly type name 151 201 const string userFriendlyTypeName; 152 202 203 //! a type name obtained by RTTI 153 204 const string typeNameByRTTI; 154 205 155 206 protected: 156 207 157 //! The only constructor which fills both the transcodedTypeName and the help attribute208 //! default constructor 158 209 UserInfo( const string& userFriendlyTypeName, const string& typeNameByRTTI ) 159 : userFriendlyTypeName ( userFriendlyTypeName ), typeNameByRTTI( typeNameByRTTI ) 210 : userFriendlyTypeName ( removeSpaces( userFriendlyTypeName ) ), 211 typeNameByRTTI( typeNameByRTTI ) 160 212 { 161 213 StringToUIMap::Add( userFriendlyTypeName, this ); 162 // we do not care which name it is, therfore we have common map, 163 // and it is no use to pass the same string again 214 164 215 if( userFriendlyTypeName != typeNameByRTTI ) 216 // we have a common map for both groups of names, 217 // therefore, it is no use to add the same pair again 165 218 StringToUIMap::Add( typeNameByRTTI, this ); 166 219 } 167 220 168 221 public: 169 //! returns object of templated type filled with data stored in this CompoundUserInfo instance 170 // NULL if error.. TODO nekam dat error message..! 222 //! This methods tries to assembly a new instance of type T (or some of its descendant types) 223 //! according to a data stored in a DOMNode named tagName within a child nodes of the passed element. 224 //! If an error occurs, it returns a NULL pointer. 171 225 template<class T> 172 226 static T* Assembly( DOMElement &element, const string tagName ) … … 175 229 XMLString::upperCase( transcodedTagName ); 176 230 177 178 231 DOMNodeList* const nodeList = element.getElementsByTagName( transcodedTagName ); 179 232 XMLString::release( (XMLCh**)&transcodedTagName ); 180 233 if( !nodeList || nodeList->getLength() == 0 ) 181 234 { 182 cerr << " There is not any tag named """ << tagName << """ in the passed DOM element of a XML docmument!";235 cerr << "Warning: there is not any tag named """ << tagName << """ in the passed DOM element of a XML docmument!"; 183 236 return NULL; 184 237 } … … 186 239 if( nodeList->getLength() > 1 ) 187 240 { 188 cerr << " There is to many elements named """ << tagName << """ in the passed DOM element of a XML docmument. But the tag name has to be unique!";241 cerr << "Warning: there is to many elements named """ << tagName << """ in the passed DOM element of a XML docmument. But the tag name has to be unique!"; 189 242 return NULL; 190 243 } 191 244 192 // TAKZE MAME V RUCE ELEMENT SE JMENEM "tagName"245 // this time we hold an element with the same name as the tagName is 193 246 DOMElement* pTheOnlyElement = (DOMElement*) nodeList->item(0); 194 247 195 // TED MAME V RUCE JMENO TYPU Z ATRIBUTU TYPE248 // we get a velue stored in the "type" attribute 196 249 string userFriendlyTypeName = Attribute::type.Get( *pTheOnlyElement ); 197 250 198 // A TED PRISLUSNE UI251 // and finally we find a UserInfo related to this type 199 252 pUserInfo pRelatedUI = StringToUIMap::Retrieve( userFriendlyTypeName ); 200 253 if( !pRelatedUI ) 201 254 { 202 cerr << " There is not any UserInfo related to type named """ << userFriendlyTypeName << """, instance assembling terminated!";255 cerr << "Warning: there is not any UserInfo related to type named """ << userFriendlyTypeName << """, instance assembling terminated!"; 203 256 return NULL; 204 257 } 205 258 206 pRelatedUI->AssemblyFromXML( *pTheOnlyElement ); 259 // prepare all components necessary for an instance assembling 260 pRelatedUI->AssemblyComponentsFromXML( *pTheOnlyElement ); 261 262 // instance assembling 207 263 void* pTypelessInstance = pRelatedUI->AssemblyTypelessInstance(); 264 265 // cleaning up 208 266 pRelatedUI->ReleaseMemory(); 209 267 210 268 if( pTypelessInstance == NULL ) 211 269 { 212 // TODO lepe specifikovat 213 cerr << "Unknown error, instance assembling terminated!"; 270 cerr << "Warning: there was some error when parsing a XML document, instance assembling terminated!"; 214 271 return NULL; 215 272 } … … 218 275 try 219 276 { 220 // typova kontrola "do it yourself":)277 // a "do it yourself" type check:) 221 278 pInstance = (T*) pTypelessInstance; 222 279 string resultingTypeNameByRTTI = typeid( *pInstance ).name(); 223 280 if( resultingTypeNameByRTTI != pRelatedUI->typeNameByRTTI ) 224 { 225 cerr << "Fatal error, UserInfo related to type """ << userFriendlyTypeName << """ have just returned instance of a different type!"; 226 return NULL; // TODO hlaska OK?? 227 } 281 pInstance = NULL; 228 282 } 229 283 catch(...) 230 284 { 231 pInstance = NULL; // TODO chybovou hlasku285 pInstance = NULL; 232 286 } 287 if( pInstance == NULL ) 288 cerr << "Warning: UserInfo related to type """ << userFriendlyTypeName << """ have just returned instance of a different type! Instance assembling terminated!"; 233 289 234 290 return pInstance; 235 291 } 236 292 293 //! This methods tries to disassembly an instance of type T (or some of its descendant types) 294 //! and build DOM tree accordingly. Then, it creates a new DOMNode named according tagName 295 //! and connecti it to the passed DOMElement as a new child node (with a help attribute filled). 237 296 template<class T> 238 297 static bool Disassembly( T& instance, DOMElement &element, const string tagName, const string help) … … 253 312 Attribute::type.Set( *pCreatedElement, pRelatedUI->userFriendlyTypeName ); 254 313 Attribute::help.Set( *pCreatedElement, help ); 255 256 // NASMERUJE UKAZATELE BindingFrames.. rozloz na pointery..314 315 // disassembly instance itself into its components 257 316 bool result = pRelatedUI->DisassemblyTypelessInstance( (void*) &instance ); 258 317 if( result ) 259 result = pRelatedUI->DisassamblyToXML( *pCreatedElement ); 318 // disassembly all components of an instance 319 result = pRelatedUI->DisassemblyComponentsToXML( *pCreatedElement ); 260 320 return result; 261 321 } 262 322 323 //! This methods tries to disassembly an instance of type T (or some of its descendant types) 324 //! and build DOM tree accordingly. Then, it creates a new DOMNode named according tagName 325 //! and connecti it to the passed DOMElement as a new child node. 263 326 template<class T> 264 327 static bool Disassembly( T &instance, DOMElement &element, const string tagName ) … … 268 331 }; 269 332 333 /*! 334 @brief TypeUserInfo is still an abstract class, but contrary to the UserInfo class it is already 335 templated. It serves as a bridge to non-abstract classes CompoundUserInfo<T> or ValuedUserInfo<T>. 336 337 There are two important features of this class. The first is a primitive mechanism bounding 338 typeless methods DisassemblyTypelessInstance, resp. AssemblyTypelessInstance, to their typed 339 virtual versions DisassemblyInstance, resp. AssemblyInstance. And the other is the only public 340 attribute of this class, called 'instance', which is to be filled by the only instance of this 341 class. Indeed, it is not possible to create any other instance outside this class (or its 342 descendant classes), as the constructor is intentionally protected. 343 */ 270 344 template<typename T> class TypedUserInfo : public UserInfo 271 345 { … … 289 363 } 290 364 365 //! abstract method assembling a typed instance from components obtained by the 'AssemblyComponentsFromXML()' method 291 366 virtual T* AssemblyInstance() = 0; 292 367 368 //! abstract method disassembling a typed instance to components which are processed by the 'DisassemblyComponentsToXML()' method 293 369 virtual bool DisassemblyInstance(T& instance) = 0; 294 370 295 371 protected: 296 372 297 //! The only constructor which fills373 //! default constructor, which is intentionally declared as protected 298 374 TypedUserInfo<T>( const string &userFriendlyTypeName) 299 375 : UserInfo( userFriendlyTypeName, typeid(T).name() ) 300 376 { 377 301 378 }; 302 379 303 //! Destructor380 //! destructor 304 381 ~TypedUserInfo<T>() 305 382 { … … 307 384 308 385 public: 386 //! the only instance of this class (each type T has its own instance) 387 //! which is used as a factory for processing related userinfos 309 388 static const TypedUserInfo<T>& instance; 310 311 389 }; 312 390 313 391 314 392 /*! 315 @brief The main user info template class 316 317 You should derive this class whenever you need new CompoundUserInfo. 393 @brief The main userinfo template class. You should derive this class whenever you need 394 a new userinfo of a class which is compound from smaller elements (all having its 395 own userinfo class prepared). 396 397 To bind some inner element to its own userinfo class, and also to automate its assembling and 398 disassembling, it is necessary to create a instance of an inner templated class BindedElement<T>. 399 Those attributes have to be initialized in constructor of a new compound userinfo this way: 400 401 \code 402 class BikeUI: public CompoundUserInfo<Bike> 403 { 404 private: 405 BindedElement<int> year; 406 BindedElement<bool> lights; 407 BindedElement<string> manufacturer; 408 public: 409 BikeUI() 410 :CompoundUserInfo<Bike>("bike"), 411 year( this, "year", 0 ), 412 lights( this, "electric lights", false ), 413 manufacturer( this, "manufacturer", "unknown") 414 { 415 } 416 417 ... 418 } 419 \endcode 318 420 */ 319 421 template<typename T> class CompoundUserInfo : public TypedUserInfo<T> 320 422 { 321 423 private: 322 //! Mapped elements, i.e., descendant html tags424 //! Elements binding inner XML tags to related userinfos 323 425 vector<BindingFrame*> bindedElements; 324 426 325 427 protected: 428 429 /*! 430 @brief Templated class binding inner element with its XML tag and automating data transfers 431 in both directions. 432 */ 326 433 template<typename U> class BindedElement: public BindingFrame 327 434 { … … 335 442 const U defaultValue; 336 443 444 337 445 public: 338 446 U value; 339 447 340 448 BindedElement<U>( CompoundUserInfo<T> *parent, string name, U defaultValue, string help ) 341 : name( name), help(help), defaultValue( defaultValue )449 : name( removeSpaces( name )), help(help), defaultValue( defaultValue ) 342 450 { 343 451 parent->bindedElements.push_back( this ); … … 347 455 348 456 BindedElement<U>( CompoundUserInfo<T> *parent, string name, U defaultValue ) 349 : name( name), help(""), defaultValue( defaultValue ), value( defaultValue)457 : name( removeSpaces( name )), help(""), defaultValue( defaultValue ), value( defaultValue) 350 458 { 351 459 parent->bindedElements.push_back( this ); … … 358 466 } 359 467 360 void Assembly FromXML( DOMElement &element )468 void AssemblyComponentsFromXML( DOMElement &element ) 361 469 { 362 470 pValue = UserInfo::Assembly<U>( element, name ); … … 370 478 } 371 479 372 bool Disass amblyToXML( DOMElement &element )480 bool DisassemblyComponentsToXML( DOMElement &element ) 373 481 { 374 482 return UserInfo::Disassembly( value, element, name, help ); … … 383 491 public: 384 492 385 void Assembly FromXML( DOMElement &element )493 void AssemblyComponentsFromXML( DOMElement &element ) 386 494 { 387 495 for( unsigned int ind = 0; ind < bindedElements.size(); ind++ ) 388 bindedElements[ind]->Assembly FromXML( element );496 bindedElements[ind]->AssemblyComponentsFromXML( element ); 389 497 } 390 498 … … 395 503 } 396 504 397 bool Disass amblyToXML( DOMElement &element )505 bool DisassemblyComponentsToXML( DOMElement &element ) 398 506 { 399 507 for( unsigned int ind = 0; ind < bindedElements.size(); ind++ ) 400 if( !bindedElements[ind]->Disass amblyToXML( element ) )508 if( !bindedElements[ind]->DisassemblyComponentsToXML( element ) ) 401 509 return false; 402 510 return true; … … 405 513 406 514 515 /*! 516 @brief The main userinfo template class. It should be derived whenever you need 517 a new userinfo of a class which does not contain any subelements. It is the case of 518 basic classes(or types) like int, string, double, etc. 519 520 The only thing left is to translate its public string attribute 'value' into a value 521 of type T and also implement conversion in the other way round. For that, an overloading 522 of methods T* AssemblyInstance() / bool DisassemblyInstance(T &instance) is fruitful. 523 See some valued userinfo below as an example. 524 */ 407 525 template<typename T> class ValuedUserInfo : public TypedUserInfo<T> 408 526 { … … 418 536 419 537 public: 420 // zde nemusi byt pointer538 //! public attribute which is automatically binded to a proper DOMElement attribute 421 539 string value; 422 540 423 void Assembly FromXML( DOMElement &element )541 void AssemblyComponentsFromXML( DOMElement &element ) 424 542 { 425 543 value = Attribute::value.Get( element ); 426 544 } 427 545 428 bool Disass amblyToXML( DOMElement &element )546 bool DisassemblyComponentsToXML( DOMElement &element ) 429 547 { 430 548 Attribute::value.Set( element, value ); … … 433 551 }; 434 552 435 436 // umi se pretypovat na DOMElement &element 437 // a to vzdy, save a load se muze udelat az pak!! 553 /*! 554 @brief This class serves to load and/or save DOMElements into/from files 555 stored on a hard-disk. 556 557 Firstly, you associate new RootElement instance with some filename during a time of its 558 construtcion. Then, you disassembly some object into the new RootElement instance, 559 and save it into the file this way: 560 \code 561 CAudi audi; 562 RootElement root("cars.xml"); 563 UserInfo::Disassembly( audi, root, "TT"); 564 root.Save(); 565 \endcode 566 567 In the other way round, when loading object from a XML file, the appropriate code looks like this: 568 \code 569 RootElement root("cars.xml"); 570 root.Load(); 571 UserInfo::Assembly<T>(root,"TT"); 572 \endcode 573 */ 438 574 class RootElement 439 575 { 440 441 private: 442 // TODO musi byt prvni!! zkusit schvalne prehodit,443 // za jmeno, a zarucit, ze Xerces jeste nebezi.. hraje to fakt roli??576 private: 577 //! This private attribute has to be declared as the first attribute in the class. 578 //! Only this way we can be sure it's constructor is called as the first 579 //! and thus Xerces is initialized properly and right on time 444 580 const AssertXercesIsAlive dummy; 445 581 582 //! DOMDocument containing the root element this instance is associated to 446 583 DOMDocument* pDoc; 447 584 … … 451 588 DOMImplementation *pImplementation; 452 589 453 //! This DOMWriter is used to export internal data into xml file s590 //! This DOMWriter is used to export internal data into xml file 454 591 DOMWriter *pSerializer; 455 592 456 public: 593 void Clean(); 594 595 public: 596 //! attach new RootElement instance to a file (typically with an XML extension) 457 597 RootElement( char* fileName ); 458 598 459 599 ~RootElement(); 460 600 461 void Clean(); 462 463 //! loads root element from a file 601 //! this method loads root element and all its subnodes from the attached file 464 602 bool Load( void ) ; 465 603 466 //! Save UserInfo to the file (typically with an XML extension)604 //! this method saves all the previsoulsy attached DOMElements into the file 467 605 void Save ( void ); 468 606 607 //! this operator allows to use a RootElement instance whenever a DOMElement variable is accepted 469 608 operator DOMElement&(); 470 609 }; 471 610 472 611 612 ////////////////////////////////////////////////////////////////////////////////////////////// 613 ////////////////////////////////////////// BASIC VALUED USER INFOS /////////////////////////// 614 ////////////////////////////////////////////////////////////////////////////////////////////// 615 616 class BoolUI: public ValuedUserInfo<bool> 617 { 618 private: 619 620 bool* AssemblyInstance() 621 { 622 if( value == "true" ) 623 return new bool( true ); 624 else if( value == "false" ) 625 return new bool( false ); 626 else return NULL; 627 } 628 629 bool DisassemblyInstance(bool &instance) 630 { 631 if( instance ) 632 value = "true"; 633 else 634 value = "false"; 635 return true; 636 } 637 638 public: 639 640 BoolUI() 641 : ValuedUserInfo<bool>("bool") 642 { 643 } 644 }; 645 646 template<> const TypedUserInfo<bool>& TypedUserInfo<bool>::instance = BoolUI(); 647 648 649 class IntUI: public ValuedUserInfo<int> 650 { 651 private: 652 653 int* AssemblyInstance() 654 { 655 return new int( atoi( value.c_str()) ); 656 } 657 658 bool DisassemblyInstance(int &instance) 659 { 660 char buff[30]; 661 sprintf(buff, "%d", instance ); 662 value = buff; 663 return true; 664 } 665 666 public: 667 IntUI():ValuedUserInfo<int>("int") 668 { 669 } 670 }; 671 672 template<> const TypedUserInfo<int>& TypedUserInfo<int>::instance = IntUI(); 673 674 675 class DoubleUI: public ValuedUserInfo<double> 676 { 677 private: 678 679 double* AssemblyInstance() 680 { 681 return new double( atof( value.c_str()) ); 682 } 683 684 bool DisassemblyInstance(double &instance) 685 { 686 char buff[30]; 687 sprintf(buff, "%f", instance ); 688 value = buff; 689 return true; 690 } 691 692 public: 693 DoubleUI():ValuedUserInfo<double>("double") 694 { 695 } 696 }; 697 698 template<> const TypedUserInfo<double>& TypedUserInfo<double>::instance = DoubleUI(); 699 700 class StringUI: public ValuedUserInfo<string> 701 { 702 private: 703 string* AssemblyInstance() 704 { 705 return new string( value ); 706 } 707 708 bool DisassemblyInstance(string &instance) 709 { 710 value = instance; 711 return true; 712 } 713 714 public: 715 StringUI():ValuedUserInfo<string>("string") 716 { 717 } 718 }; 719 720 template<> const TypedUserInfo<string>& TypedUserInfo<string>::instance = StringUI(); 721 722 473 723 #endif // #ifndef UI_H -
tests/testUI.cpp
r154 r156 1 1 #include "userinfo.h" 2 2 3 //////////////////////////////////////////////////////////////////////////////////////////////4 ////////////////////////////////////////// BASIC VALUED USER INFOS ///////////////////////////5 //////////////////////////////////////////////////////////////////////////////////////////////6 7 class BoolUI: public ValuedUserInfo<bool>8 {9 private:10 11 bool* AssemblyInstance()12 {13 if( value == "true" )14 return new bool( true );15 else16 return new bool( false );17 }18 19 bool DisassemblyInstance(bool &instance)20 {21 if( instance )22 value = "true";23 else24 value = "false";25 return true;26 }27 28 public:29 30 BoolUI()31 : ValuedUserInfo<bool>("bool")32 {33 }34 };35 36 template<> const TypedUserInfo<bool>& TypedUserInfo<bool>::instance = BoolUI();37 38 39 class IntUI: public ValuedUserInfo<int>40 {41 private:42 43 int* AssemblyInstance()44 {45 return new int( atoi( value.c_str()) );46 }47 48 bool DisassemblyInstance(int &instance)49 {50 char buff[30];51 sprintf(buff, "%d", instance );52 value = buff;53 return true;54 }55 56 public:57 IntUI():ValuedUserInfo<int>("int")58 {59 }60 };61 62 template<> const TypedUserInfo<int>& TypedUserInfo<int>::instance = IntUI();63 64 65 class DoubleUI: public ValuedUserInfo<double>66 {67 private:68 69 double* AssemblyInstance()70 {71 return new double( atof( value.c_str()) );72 }73 74 bool DisassemblyInstance(double &instance)75 {76 char buff[30];77 sprintf(buff, "%f", instance );78 value = buff;79 return true;80 }81 82 public:83 DoubleUI():ValuedUserInfo<double>("double")84 {85 }86 };87 88 template<> const TypedUserInfo<double>& TypedUserInfo<double>::instance = DoubleUI();89 90 class StringUI: public ValuedUserInfo<string>91 {92 private:93 string* AssemblyInstance()94 {95 return new string( value );96 }97 98 bool DisassemblyInstance(string &instance)99 {100 value = instance;101 return true;102 }103 104 public:105 StringUI():ValuedUserInfo<string>("string")106 {107 }108 };109 110 template<> const TypedUserInfo<string>& TypedUserInfo<string>::instance = StringUI();111 3 112 4 ////////////////////////////////////////////////////////////////////////////////////////////// 113 ////////////////////////////////////////// EXAMPLECLASSES DEFINITION ////////////////////////5 ////////////////////////////////////////// CLASSES DEFINITION //////////////////////// 114 6 ////////////////////////////////////////////////////////////////////////////////////////////// 115 7 class Transport … … 210 102 :CompoundUserInfo<Bike>("bike"), 211 103 year( this, "year", 0 ), 212 lights( this, "electric _lights", false ), // jen jedno slovo!104 lights( this, "electric lights", false ), 213 105 manufacturer( this, "manufacturer", "unknown") 214 106 {