root/tests/testUI.cpp @ 150

Revision 150, 2.2 kB (checked in by mido, 16 years ago)
  • Property svn:eol-style set to native
Line 
1#include "userinfo.h"
2
3class IntUI: public ValuedUserInfo<int>
4{
5public:
6        IntUI():ValuedUserInfo<int>("int")
7        {
8        }
9
10        void Build(int* &pInstance)
11        {
12                pInstance = new int( atoi( value.c_str()) );
13        }
14
15        void Absorb(int* &pInstance)
16        {
17                char buff[30];
18                sprintf(buff, "%d", *pInstance );
19                value = buff;
20        }
21};
22
23const TypedUserInfo<int>* TypedUserInfo<int>::pInstance = new IntUI();
24
25
26class StringUI: public ValuedUserInfo<string>
27{
28public:
29        StringUI():ValuedUserInfo<string>("string")
30        {
31        }
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
44const TypedUserInfo<string>* TypedUserInfo<string>::pInstance = new StringUI();
45
46class Car
47{
48public:
49        int age;
50        string brand;
51
52        Car( int age, string brand)
53                : age( age), brand( brand)
54        {
55        }
56};
57
58class CarUI: public CompoundUserInfo<Car>
59{
60public:
61        BindedType<int> age;
62        BindedType<string> brand;
63
64        CarUI()
65                :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
85const TypedUserInfo<Car>* TypedUserInfo<Car>::pInstance = new CarUI( );
86
87
88int main()
89{
90        /////////////////////////////////// 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") )
97        {
98                cout << "there was some error!" << endl;
99                getchar();
100                return 0;                       
101        }
102
103        root.Save();
104        cout << "the new car was saved correctly!" << endl;                             
105        getchar();
106
107        //////////////////////////////////// LOADING ////////////////////////////////
108
109
110        Car *loadedCar;
111        root.Load();
112        UserInfo::Assembly(loadedCar,root,"kara");
113       
114        if( loadedCar )
115                cout << "loaded car: age=" << loadedCar->age << ", brand=" << loadedCar->brand << endl;
116        else
117                cout << "there was some error during car loading!" << endl;
118               
119        getchar(); 
120        return 0;
121}
Note: See TracBrowser for help on using the browser.