Changeset 151 for tests/testUI.cpp

Show
Ignore:
Timestamp:
08/22/08 20:57:40 (16 years ago)
Author:
mido
Message:

UserInfo?, finalni verze, cas vznaset pripominky ( krom komentaru, ty zatim nejsou hotove, a netreba to pripominat:)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • tests/testUI.cpp

    r150 r151  
    11#include "userinfo.h" 
    22 
     3////////////////////////////////////////////////////////////////////////////////////////////// 
     4////////////////////////////////////////// BASIC VALUED USER INFOS /////////////////////////// 
     5////////////////////////////////////////////////////////////////////////////////////////////// 
     6 
     7class BoolUI: public ValuedUserInfo<bool> 
     8{ 
     9private: 
     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 
     28public: 
     29 
     30        BoolUI() 
     31                : ValuedUserInfo<bool>("bool") 
     32        { 
     33        } 
     34}; 
     35 
     36const TypedUserInfo<bool>& TypedUserInfo<bool>::instance = BoolUI(); 
     37 
     38 
    339class IntUI: public ValuedUserInfo<int> 
    440{ 
     41private: 
     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 
    556public: 
    657        IntUI():ValuedUserInfo<int>("int") 
    758        { 
    859        } 
    9  
    10         void Build(int* &pInstance) 
    11         { 
    12                 pInstance = new int( atoi( value.c_str()) ); 
    13         } 
    14  
    15         void Absorb(int* &pInstance) 
     60}; 
     61 
     62const TypedUserInfo<int>& TypedUserInfo<int>::instance = IntUI(); 
     63 
     64 
     65class DoubleUI: public ValuedUserInfo<double> 
     66{ 
     67private: 
     68 
     69        double* AssemblyInstance() 
     70        { 
     71                return new double( atof( value.c_str()) ); 
     72        } 
     73 
     74        bool DisassemblyInstance(double &instance) 
    1675        { 
    1776                char buff[30]; 
    18                 sprintf(buff, "%d", *pInstance ); 
     77                sprintf(buff, "%f", instance ); 
    1978                value = buff; 
    20         } 
    21 }; 
    22  
    23 const TypedUserInfo<int>* TypedUserInfo<int>::pInstance = new IntUI(); 
     79                return true;  
     80        } 
     81 
     82public: 
     83        DoubleUI():ValuedUserInfo<double>("double") 
     84        { 
     85        } 
     86}; 
     87 
     88const TypedUserInfo<double>& TypedUserInfo<double>::instance = DoubleUI(); 
    2489 
    2590 
    2691class StringUI: public ValuedUserInfo<string> 
    2792{ 
     93private: 
     94        string* AssemblyInstance() 
     95        { 
     96                return new string( value ); 
     97        } 
     98 
     99        bool DisassemblyInstance(string &instance) 
     100        { 
     101                value = instance; 
     102                return true; 
     103        } 
     104 
    28105public: 
    29106        StringUI():ValuedUserInfo<string>("string") 
    30107        { 
    31108        } 
    32  
    33         void Build(string* &pInstance) 
    34         { 
    35                 pInstance  = new string( value ); 
    36         } 
    37  
    38         void Absorb(string* &pInstance) 
    39         { 
    40                 value = *pInstance; 
    41         } 
    42 }; 
    43  
    44 const TypedUserInfo<string>* TypedUserInfo<string>::pInstance = new StringUI(); 
    45  
    46 class Car 
    47 { 
    48 public: 
    49         int age; 
    50         string brand; 
    51  
    52         Car( int age, string brand) 
    53                 : age( age), brand( brand) 
    54         { 
    55         } 
    56 }; 
     109}; 
     110 
     111const TypedUserInfo<string>& TypedUserInfo<string>::instance = StringUI(); 
     112 
     113////////////////////////////////////////////////////////////////////////////////////////////// 
     114////////////////////////////////////////// EXAMPLE CLASSES DEFINITION //////////////////////// 
     115////////////////////////////////////////////////////////////////////////////////////////////// 
     116class Transport 
     117{ 
     118public: 
     119        const int year; 
     120        const string manufacturer; 
     121 
     122        Transport( int year, string manufacturer ) 
     123                : year( year ), manufacturer( manufacturer ) 
     124        { 
     125        } 
     126 
     127        virtual public void ToString() = 0; 
     128}; 
     129 
     130class Car : public Transport 
     131{ 
     132public: 
     133        const int kilometers; 
     134 
     135        Car( int year, string manufacturer, int kilometers ) 
     136                : Transport( year, manufacturer ), kilometers( kilometers ) 
     137        { 
     138        } 
     139 
     140        void ToString() 
     141        { 
     142                cout << "a car made in " << year << " by " << manufacturer << ", having " << kilometers << " kilometers on the clock." << endl; 
     143        } 
     144}; 
     145 
     146class Bike : public Transport 
     147{ 
     148public: 
     149        const bool electricLights; 
     150 
     151        Bike( int age, string manufacturer, bool electricLights ) 
     152                : Transport( age, manufacturer ), electricLights( electricLights ) 
     153        { 
     154        } 
     155 
     156        void ToString() 
     157        { 
     158                cout << "a bike made in " << year << " by " << manufacturer; 
     159                if( electricLights ) cout << " with electric lights included";                                           
     160                cout << endl;            
     161        } 
     162}; 
     163 
     164////////////////////////////////////////////////////////////////////////////////////////////// 
     165////////////////////////////////////////// AND RELATED USER INFOS //////////////////////////// 
     166////////////////////////////////////////////////////////////////////////////////////////////// 
     167 
    57168 
    58169class CarUI: public CompoundUserInfo<Car> 
    59170{ 
    60 public: 
    61         BindedType<int> age; 
    62         BindedType<string> brand; 
    63  
     171private: 
     172        BindedElement<int> year;  
     173        BindedElement<int> kilometers;  
     174        BindedElement<string> manufacturer;  
     175public: 
    64176        CarUI() 
    65177                :CompoundUserInfo<Car>("car"), 
    66                 age( this, "age" ), 
    67                 brand( this, "brand") 
    68         { 
    69         } 
    70  
    71         void Build(Car* &pInstance) 
    72         { 
    73                 if( age.pInstance == NULL ) age.pInstance = new int (0); 
    74                 if( brand.pInstance == NULL ) brand.pInstance = new string("noname"); 
    75                 pInstance  = new Car( *age.pInstance, *brand.pInstance ); 
    76         } 
    77  
    78         void Absorb(Car* &pInstance) 
    79         { 
    80                 age.pInstance = &pInstance->age; 
    81                 brand.pInstance = &pInstance->brand; 
    82         } 
    83 }; 
    84  
    85 const TypedUserInfo<Car>* TypedUserInfo<Car>::pInstance = new CarUI( ); 
     178                year( this, "year", 0 ), 
     179                kilometers( this, "kilometers", 0 ), 
     180                manufacturer( this, "manufacturer", "unknown") 
     181        { 
     182        } 
     183private: 
     184 
     185        Car* AssemblyInstance() 
     186        { 
     187                // assembly new instance 
     188                return new Car( year.value, manufacturer.value, kilometers.value ); 
     189        } 
     190 
     191        bool DisassemblyInstance(Car& instance) 
     192        { 
     193                year.value = instance.year; 
     194                manufacturer.value = instance.manufacturer; 
     195                kilometers.value = instance.kilometers; 
     196                return true; 
     197        } 
     198}; 
     199 
     200const TypedUserInfo<Car>& TypedUserInfo<Car>::instance = CarUI( ); 
     201 
     202 
     203class BikeUI: public CompoundUserInfo<Bike> 
     204{ 
     205private: 
     206        BindedElement<int> year;  
     207        BindedElement<bool> lights;  
     208        BindedElement<string> manufacturer;  
     209public: 
     210        BikeUI() 
     211                :CompoundUserInfo<Bike>("bike"), 
     212                year( this, "year", 0 ), 
     213                lights( this, "electric_lights", false ), // jen jedno slovo! 
     214                manufacturer( this, "manufacturer", "unknown") 
     215        { 
     216        } 
     217private: 
     218 
     219        Bike* AssemblyInstance() 
     220        { 
     221                // assembly new instance 
     222                return new Bike( year.value, manufacturer.value, lights.value ); 
     223        } 
     224 
     225        bool DisassemblyInstance(Bike& instance) 
     226        { 
     227                year.value = instance.year; 
     228                manufacturer.value = instance.manufacturer; 
     229                lights.value = instance.electricLights; 
     230                return true; 
     231        } 
     232}; 
     233 
     234const TypedUserInfo<Bike>& TypedUserInfo<Bike>::instance = BikeUI( ); 
    86235 
    87236 
    88237int main() 
    89238{ 
     239        Car audi( 1998, "audi", 25000); 
     240        Car liaz( 1992, "liaz", 1555000); 
     241        Bike author( 1996, "author", true ); 
     242 
    90243        /////////////////////////////////// SAVING /////////////////////////// 
    91         Car newCar( 20, "audina" ); 
    92  
    93         RootElement root("car.xml"); 
    94         // NESLO BY TAM MISTO POINTERU PRIMO REFERENCI, 
    95         // NEBO TAK NECO?!?! ASI JO..! 
    96         if( !UserInfo::Disassembly( &newCar,root,"kara") ) 
     244 
     245        RootElement root("transport.xml"); 
     246 
     247        if( !UserInfo::Disassembly( audi, root, "pepikovo")  
     248                || !UserInfo::Disassembly( liaz, root, "jardovo")  
     249                || !UserInfo::Disassembly( author, root, "ondrejovo") ) 
    97250        { 
    98251                cout << "there was some error!" << endl; 
     
    102255 
    103256        root.Save(); 
    104         cout << "the new car was saved correctly!" << endl;                              
     257        cout << "all the transport means were saved correctly" << endl;                          
    105258        getchar(); 
    106259 
    107260        //////////////////////////////////// LOADING //////////////////////////////// 
    108261 
    109  
    110         Car *loadedCar; 
     262        string whichone = "pepikovo"; 
     263//      whichone = "jardovo"; 
     264        whichone = "ondrejovo"; 
     265 
    111266        root.Load(); 
    112         UserInfo::Assembly(loadedCar,root,"kara"); 
     267        Transport *loaded = UserInfo::Assembly<Transport>( root,whichone); 
    113268         
    114         if( loadedCar ) 
    115                 cout << "loaded car: age=" << loadedCar->age << ", brand=" << loadedCar->brand << endl; 
     269        if( loaded ) 
     270                loaded->ToString(); 
    116271        else 
    117                 cout << "there was some error during car loading!" << endl; 
     272                cout << "there was some error during loading!" << endl; 
    118273                 
    119274        getchar();