Changeset 156

Show
Ignore:
Timestamp:
09/01/08 15:08:50 (16 years ago)
Author:
mido
Message:

okomentovani UI

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • bdm/userinfo.h

    r152 r156  
    2727 
    2828/*! 
    29 @brief Xerces interface class 
    30  
    31 This class is used to interact with the Xerces library. 
     29@brief Class initializing Xerces library 
     30 
     31This class is used to initialize the Xerces library. Namely 
     32to call static method XMLPlatformUtils::Initialize(). It should 
     33be called just once, at previously to any other usage of the Xerces 
     34library. This behaviour is assured by a quite complicated design 
     35based on the use of an inner singleton class named 'XercesConnector'. 
     36The point is to be the first initialized class even within the set of all 
     37(global) static variables. For better understanding, find instances of 
     38this class and look at their context. 
    3239*/ 
    33 /// TODO slo by i bez vnorene tridy? 
    3440class AssertXercesIsAlive 
    3541{ 
    3642private: 
     43        //! Singleton class implementing the key property of the 'AssertXercesIsAlive' class  
    3744        class XercesConnector 
    3845        { 
    3946        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! 
    4149                XercesConnector() 
    4250                { 
     
    5260 
    5361        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. 
    5668                static void StayAlive() 
    5769                { 
     
    6274 
    6375public: 
     76        //!default constructor 
    6477        AssertXercesIsAlive() 
    6578        { 
     
    6881}; 
    6982 
    70  
     83/*! 
     84@brief Abstract class declaring general properties of a frame for data binding  
     85*/ 
    7186class BindingFrame 
    7287{ 
    7388private: 
    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 
    7592        AssertXercesIsAlive dummy; 
     93 
    7694protected: 
     95        //!default constructor 
    7796        BindingFrame(); 
    7897 
    79         //! function transcodes Xerces' XMLCh-based strings into C++ strings 
     98        //! function which transcodes Xerces' XMLCh-based strings into C++ strings 
    8099        string XMLCh2str( const XMLCh* const  XMLCh_str ); 
    81100 
     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 
    82110public:  
    83         // nacpe na prislusne pointery, kdyztak da NULL 
    84         virtual void AssemblyFromXML( DOMElement &element ) = 0; 
    85  
    86         // uvolneni pameti po slozeni objektu, tj. lze jedine po AssemblyBD 
     111        //! 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 
    87115        virtual void ReleaseMemory() {} 
    88116 
    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*/ 
    94125class Attribute  
    95126{                
    96127private: 
    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 
    98131        AssertXercesIsAlive dummy; 
    99132 
     133        //! an attribute name 
    100134        const XMLCh* const transcodedAttributeName; 
    101135 
    102136public: 
     137        //! Default constructor fixing a name of the related attribute 
    103138        Attribute( string attributeName ); 
    104139 
    105140        ~Attribute(); 
    106141 
    107         // nacpe na prislusne pointery, kdyztak da NULL 
     142        //! This method parse DOMElement, finds proper attribute and returns its value 
    108143        string& Get( DOMElement &element ) const; 
    109144 
     145        //! This method adds the passed string as an new attribute into the passed DOMElement 
    110146        void Set( DOMElement &element, const string &str ) const;        
    111147 
     148        //! Static member, an instance related to an attribute named 'help' 
    112149        static const Attribute help; 
    113150 
     151        //! Static member, a constant instance related to an attribute named 'type' 
    114152        static const Attribute type; 
    115153 
     154        //! Static member, a constant instance related to the attribute named 'value' 
    116155        static const Attribute value; 
    117156}; 
     
    120159 
    121160/*! 
    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. 
     162The raison d'etre of this class is to allow pointers to its templated descendants.  
     163 
     164Also, the main functions of the whole UserInfo library are included within this class, see 
     165static methods 'Assembly' and 'Disassembly'. 
    126166*/ 
    127167class UserInfo : protected BindingFrame 
    128168{ 
    129169private: 
     170        //! just a typedef shortuct for a constant pointer to UserInfo 
    130171        typedef UserInfo* const pUserInfo; 
    131172 
    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. 
    133177        class StringToUIMap 
    134178        { 
     
    137181                typedef map< const string, pUserInfo > MappedString2UI; 
    138182 
     183                //! immediately initialized instance of type MappedString2UI 
    139184                static MappedString2UI& privateMap(); 
    140185 
    141186        public: 
     187                //! add a pair key-userinfo into the internal map 
    142188                static void Add( string key, pUserInfo pInstance ); 
    143189 
     190                //! search for an userinfo related to the passed key within the internal map 
    144191                static pUserInfo Retrieve( string key ); 
    145192        }; 
    146193                                 
     194        //! internal method assembling a typeless instance from components obtained by the 'AssemblyComponentsFromXML()' method 
    147195        virtual void* AssemblyTypelessInstance() = 0; 
    148196         
     197        //! internal method disassembling a typeless instance to components which are processed by the 'DisassemblyComponentsToXML()' method 
    149198        virtual bool DisassemblyTypelessInstance(void* pInstance) = 0; 
    150199 
     200        //! an user-friendly type name 
    151201        const string userFriendlyTypeName; 
    152202 
     203        //! a type name obtained by RTTI 
    153204        const string typeNameByRTTI; 
    154205 
    155206protected: 
    156207 
    157         //! The only constructor which fills both the transcodedTypeName and the help attribute 
     208        //! default constructor  
    158209        UserInfo( const string& userFriendlyTypeName, const string& typeNameByRTTI ) 
    159                 : userFriendlyTypeName ( userFriendlyTypeName ), typeNameByRTTI( typeNameByRTTI ) 
     210                : userFriendlyTypeName ( removeSpaces( userFriendlyTypeName ) ),  
     211                  typeNameByRTTI( typeNameByRTTI ) 
    160212        {        
    161213                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 
    164215                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 
    165218                        StringToUIMap::Add( typeNameByRTTI, this ); 
    166219        } 
    167220 
    168221public:  
    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. 
    171225        template<class T> 
    172226        static T* Assembly( DOMElement &element, const string tagName ) 
     
    175229                XMLString::upperCase( transcodedTagName ); 
    176230 
    177  
    178231                DOMNodeList* const nodeList = element.getElementsByTagName( transcodedTagName ); 
    179232                XMLString::release( (XMLCh**)&transcodedTagName ); 
    180233                if( !nodeList || nodeList->getLength() == 0 ) 
    181234                { 
    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!"; 
    183236                        return NULL; 
    184237                } 
     
    186239                if( nodeList->getLength() > 1 ) 
    187240                { 
    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!"; 
    189242                        return NULL; 
    190243                } 
    191244 
    192                 // TAKZE MAME V RUCE ELEMENT SE JMENEM "tagName" 
     245                // this time we hold an element with the same name as the tagName is 
    193246                DOMElement* pTheOnlyElement = (DOMElement*) nodeList->item(0); 
    194247 
    195                 // TED MAME V RUCE JMENO TYPU Z ATRIBUTU TYPE 
     248                // we get a velue stored in the "type" attribute  
    196249                string userFriendlyTypeName = Attribute::type.Get( *pTheOnlyElement ); 
    197250         
    198                 // A TED PRISLUSNE UI 
     251                // and finally we find a UserInfo related to this type 
    199252                pUserInfo pRelatedUI = StringToUIMap::Retrieve( userFriendlyTypeName ); 
    200253                if( !pRelatedUI ) 
    201254                { 
    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!"; 
    203256                        return NULL; 
    204257                } 
    205258 
    206                 pRelatedUI->AssemblyFromXML( *pTheOnlyElement );                 
     259                // prepare all components necessary for an instance assembling 
     260                pRelatedUI->AssemblyComponentsFromXML( *pTheOnlyElement );               
     261 
     262                // instance assembling  
    207263                void* pTypelessInstance = pRelatedUI->AssemblyTypelessInstance(); 
     264 
     265                // cleaning up 
    208266                pRelatedUI->ReleaseMemory(); 
    209267 
    210268                if( pTypelessInstance == NULL ) 
    211269                { 
    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!"; 
    214271                        return NULL; 
    215272                } 
     
    218275                try 
    219276                { 
    220                         // typova kontrola "do it yourself":) 
     277                        // a "do it yourself" type check:) 
    221278                        pInstance = (T*) pTypelessInstance; 
    222279                        string resultingTypeNameByRTTI = typeid( *pInstance ).name(); 
    223280                        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; 
    228282                } 
    229283                catch(...) 
    230284                { 
    231                         pInstance = NULL; // TODO chybovou hlasku 
     285                        pInstance = NULL; 
    232286                }                
     287                if( pInstance == NULL ) 
     288                        cerr << "Warning: UserInfo related to type """ << userFriendlyTypeName << """ have just returned instance of a different type! Instance assembling terminated!"; 
    233289 
    234290                return pInstance; 
    235291        }        
    236292 
     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). 
    237296        template<class T> 
    238297        static bool Disassembly( T& instance, DOMElement &element, const string tagName, const string help) 
     
    253312                Attribute::type.Set( *pCreatedElement, pRelatedUI->userFriendlyTypeName );               
    254313                Attribute::help.Set( *pCreatedElement, help ); 
    255  
    256                 // NASMERUJE UKAZATELE BindingFrames.. rozloz na pointery..  
     314                 
     315                // disassembly instance itself into its components 
    257316                bool result =  pRelatedUI->DisassemblyTypelessInstance( (void*) &instance ); 
    258317                if( result ) 
    259                         result = pRelatedUI->DisassamblyToXML( *pCreatedElement );               
     318                        // disassembly all components of an instance     
     319                        result = pRelatedUI->DisassemblyComponentsToXML( *pCreatedElement );             
    260320                return result; 
    261321        }        
    262322 
     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. 
    263326        template<class T> 
    264327        static bool Disassembly( T &instance, DOMElement &element, const string tagName ) 
     
    268331}; 
    269332 
     333/*! 
     334@brief TypeUserInfo is still an abstract class, but contrary to the UserInfo class it is already  
     335templated. It serves as a bridge to non-abstract classes CompoundUserInfo<T> or ValuedUserInfo<T>. 
     336 
     337There are two important features of this class. The first is a primitive mechanism bounding  
     338typeless methods DisassemblyTypelessInstance, resp. AssemblyTypelessInstance, to their typed  
     339virtual versions DisassemblyInstance, resp. AssemblyInstance. And the other is the only public  
     340attribute of this class, called 'instance', which is to be filled by the only instance of this  
     341class. Indeed, it is not  possible to create any other instance outside this class (or its  
     342descendant classes), as the constructor is intentionally protected. 
     343*/ 
    270344template<typename T> class TypedUserInfo : public UserInfo 
    271345{ 
     
    289363        } 
    290364 
     365        //! abstract method assembling a typed instance from components obtained by the 'AssemblyComponentsFromXML()' method 
    291366        virtual T* AssemblyInstance() = 0; 
    292367 
     368        //! abstract method disassembling a typed instance to components which are processed by the 'DisassemblyComponentsToXML()' method 
    293369        virtual bool DisassemblyInstance(T& instance) = 0; 
    294370 
    295371protected: 
    296372 
    297         //! The only constructor which fills  
     373        //! default constructor, which is intentionally declared as protected 
    298374        TypedUserInfo<T>( const string &userFriendlyTypeName)  
    299375                : UserInfo( userFriendlyTypeName, typeid(T).name() )  
    300376        {        
     377 
    301378        }; 
    302379 
    303         //! Destructor 
     380        //! destructor 
    304381        ~TypedUserInfo<T>() 
    305382        { 
     
    307384 
    308385public: 
     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 
    309388        static const TypedUserInfo<T>& instance; 
    310  
    311389}; 
    312390 
    313391 
    314392/*! 
    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  
     394a new userinfo of a class which is compound from smaller elements (all having its 
     395own userinfo class prepared). 
     396 
     397To bind some inner element to its own userinfo class, and also to automate its assembling and  
     398disassembling, it is necessary to create a instance of an inner templated class BindedElement<T>. 
     399Those attributes have to be initialized in constructor of a new compound userinfo this way: 
     400 
     401\code 
     402class BikeUI: public CompoundUserInfo<Bike> 
     403{ 
     404private: 
     405        BindedElement<int> year;  
     406        BindedElement<bool> lights;  
     407        BindedElement<string> manufacturer;  
     408public: 
     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 
    318420*/ 
    319421template<typename T> class CompoundUserInfo : public TypedUserInfo<T> 
    320422{ 
    321423private: 
    322         //! Mapped elements, i.e., descendant html tags 
     424        //! Elements binding inner XML tags to related userinfos 
    323425        vector<BindingFrame*> bindedElements; 
    324426 
    325427protected: 
     428 
     429        /*! 
     430        @brief Templated class binding inner element with its XML tag and automating data transfers 
     431        in both directions. 
     432        */ 
    326433        template<typename U> class BindedElement: public BindingFrame 
    327434        { 
     
    335442                const U defaultValue; 
    336443                 
     444  
    337445        public: 
    338446                U value; 
    339447 
    340448                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 ) 
    342450                { 
    343451                        parent->bindedElements.push_back( this ); 
     
    347455 
    348456                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) 
    350458                { 
    351459                        parent->bindedElements.push_back( this ); 
     
    358466                } 
    359467 
    360                 void AssemblyFromXML( DOMElement &element ) 
     468                void AssemblyComponentsFromXML( DOMElement &element ) 
    361469                { 
    362470                        pValue = UserInfo::Assembly<U>( element, name ); 
     
    370478                } 
    371479 
    372                 bool DisassamblyToXML( DOMElement &element ) 
     480                bool DisassemblyComponentsToXML( DOMElement &element ) 
    373481                { 
    374482                        return UserInfo::Disassembly( value, element, name, help ); 
     
    383491public: 
    384492 
    385         void AssemblyFromXML( DOMElement &element ) 
     493        void AssemblyComponentsFromXML( DOMElement &element ) 
    386494        { 
    387495                for( unsigned int ind = 0; ind < bindedElements.size(); ind++ ) 
    388                         bindedElements[ind]->AssemblyFromXML( element ); 
     496                        bindedElements[ind]->AssemblyComponentsFromXML( element ); 
    389497        } 
    390498 
     
    395503        } 
    396504 
    397         bool DisassamblyToXML( DOMElement &element ) 
     505        bool DisassemblyComponentsToXML( DOMElement &element ) 
    398506        { 
    399507                for( unsigned int ind = 0; ind < bindedElements.size(); ind++ ) 
    400                         if( !bindedElements[ind]->DisassamblyToXML( element ) ) 
     508                        if( !bindedElements[ind]->DisassemblyComponentsToXML( element ) ) 
    401509                                return false; 
    402510                return true; 
     
    405513 
    406514 
     515/*! 
     516@brief The main userinfo template class. It should be derived whenever you need  
     517a new userinfo of a class which does not contain any subelements. It is the case of 
     518basic classes(or types) like int, string, double, etc. 
     519 
     520The only thing left is to translate its public string attribute 'value' into a value  
     521of type T and also implement conversion in the other way round. For that, an overloading 
     522of methods T* AssemblyInstance() /  bool DisassemblyInstance(T  &instance) is fruitful. 
     523See some valued userinfo below as an example. 
     524*/ 
    407525template<typename T> class ValuedUserInfo : public TypedUserInfo<T> 
    408526{ 
     
    418536 
    419537public: 
    420         // zde nemusi byt pointer 
     538        //! public attribute which is automatically binded to a proper DOMElement attribute 
    421539        string value; 
    422540 
    423         void AssemblyFromXML( DOMElement &element ) 
     541        void AssemblyComponentsFromXML( DOMElement &element ) 
    424542        {                
    425543                value = Attribute::value.Get( element ); 
    426544        } 
    427545 
    428         bool DisassamblyToXML( DOMElement &element ) 
     546        bool DisassemblyComponentsToXML( DOMElement &element ) 
    429547        { 
    430548                Attribute::value.Set( element, value ); 
     
    433551}; 
    434552 
    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 
     555stored on a hard-disk. 
     556 
     557Firstly, you associate new RootElement instance with some filename during a time of its  
     558construtcion. Then, you disassembly some object into the new RootElement instance, 
     559and 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 
     567In 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*/ 
    438574class RootElement  
    439575{ 
    440  
    441 private: 
    442         // TODO musi byt prvni!! zkusit schvalne prehodit, 
    443         // za jmeno, a zarucit, ze Xerces jeste nebezi.. hraje to fakt roli?? 
     576private: 
     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 
    444580        const AssertXercesIsAlive dummy; 
    445581 
     582        //! DOMDocument containing the root element this instance is associated to 
    446583        DOMDocument* pDoc; 
    447584 
     
    451588        DOMImplementation *pImplementation; 
    452589 
    453         //! This DOMWriter is used to export internal data into xml files 
     590        //! This DOMWriter is used to export internal data into xml file 
    454591        DOMWriter *pSerializer; 
    455592 
    456 public: 
     593        void Clean(); 
     594 
     595public: 
     596        //! attach new RootElement instance to a file (typically with an XML extension) 
    457597        RootElement( char* fileName ); 
    458598 
    459599        ~RootElement(); 
    460600 
    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 
    464602        bool Load( void ) ; 
    465603 
    466         //! Save UserInfo to the file (typically with an XML extension) 
     604        //! this method saves all the previsoulsy attached DOMElements into the file  
    467605        void Save ( void ); 
    468606 
     607        //! this operator allows to use a RootElement instance whenever a DOMElement variable is accepted 
    469608        operator DOMElement&(); 
    470609}; 
    471610 
    472611 
     612////////////////////////////////////////////////////////////////////////////////////////////// 
     613////////////////////////////////////////// BASIC VALUED USER INFOS /////////////////////////// 
     614////////////////////////////////////////////////////////////////////////////////////////////// 
     615 
     616class BoolUI: public ValuedUserInfo<bool> 
     617{ 
     618private: 
     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 
     638public: 
     639 
     640        BoolUI() 
     641                : ValuedUserInfo<bool>("bool") 
     642        { 
     643        } 
     644}; 
     645 
     646template<> const TypedUserInfo<bool>& TypedUserInfo<bool>::instance = BoolUI(); 
     647 
     648 
     649class IntUI: public ValuedUserInfo<int> 
     650{ 
     651private: 
     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 
     666public: 
     667        IntUI():ValuedUserInfo<int>("int") 
     668        { 
     669        } 
     670}; 
     671 
     672template<> const TypedUserInfo<int>& TypedUserInfo<int>::instance = IntUI(); 
     673 
     674 
     675class DoubleUI: public ValuedUserInfo<double> 
     676{ 
     677private: 
     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 
     692public: 
     693        DoubleUI():ValuedUserInfo<double>("double") 
     694        { 
     695        } 
     696}; 
     697 
     698template<> const TypedUserInfo<double>& TypedUserInfo<double>::instance = DoubleUI(); 
     699 
     700class StringUI: public ValuedUserInfo<string> 
     701{ 
     702private: 
     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 
     714public: 
     715        StringUI():ValuedUserInfo<string>("string") 
     716        { 
     717        } 
     718}; 
     719 
     720template<> const TypedUserInfo<string>& TypedUserInfo<string>::instance = StringUI(); 
     721 
     722 
    473723#endif // #ifndef UI_H 
  • tests/testUI.cpp

    r154 r156  
    11#include "userinfo.h" 
    22 
    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                 else 
    16                         return new bool( false ); 
    17         } 
    18  
    19         bool DisassemblyInstance(bool &instance) 
    20         { 
    21                 if( instance ) 
    22                         value = "true"; 
    23                 else 
    24                         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(); 
    1113 
    1124////////////////////////////////////////////////////////////////////////////////////////////// 
    113 ////////////////////////////////////////// EXAMPLE CLASSES DEFINITION //////////////////////// 
     5////////////////////////////////////////// CLASSES DEFINITION //////////////////////// 
    1146////////////////////////////////////////////////////////////////////////////////////////////// 
    1157class Transport 
     
    210102                :CompoundUserInfo<Bike>("bike"), 
    211103                year( this, "year", 0 ), 
    212                 lights( this, "electric_lights", false ), // jen jedno slovo! 
     104                lights( this, "electric lights", false ),  
    213105                manufacturer( this, "manufacturer", "unknown") 
    214106        {