root/tests/testUI.cpp @ 158

Revision 156, 3.8 kB (checked in by mido, 16 years ago)

okomentovani UI

  • Property svn:eol-style set to native
Line 
1#include "userinfo.h"
2
3
4//////////////////////////////////////////////////////////////////////////////////////////////
5////////////////////////////////////////// CLASSES DEFINITION ////////////////////////
6//////////////////////////////////////////////////////////////////////////////////////////////
7class Transport
8{
9public:
10        const int year;
11        const string manufacturer;
12
13        Transport( int year, string manufacturer )
14                : year( year ), manufacturer( manufacturer )
15        {
16        }
17
18        virtual void ToString() = 0;
19};
20
21class Car : public Transport
22{
23public:
24        const int kilometers;
25
26        Car( int year, string manufacturer, int kilometers )
27                : Transport( year, manufacturer ), kilometers( kilometers )
28        {
29        }
30
31        void ToString()
32        {
33                cout << "a car made in " << year << " by " << manufacturer << ", having " << kilometers << " kilometers on the clock." << endl;
34        }
35};
36
37class Bike : public Transport
38{
39public:
40        const bool electricLights;
41
42        Bike( int age, string manufacturer, bool electricLights )
43                : Transport( age, manufacturer ), electricLights( electricLights )
44        {
45        }
46
47        void ToString()
48        {
49                cout << "a bike made in " << year << " by " << manufacturer;
50                if( electricLights ) cout << " with electric lights included";                                         
51                cout << endl;           
52        }
53};
54
55//////////////////////////////////////////////////////////////////////////////////////////////
56////////////////////////////////////////// AND RELATED USER INFOS ////////////////////////////
57//////////////////////////////////////////////////////////////////////////////////////////////
58
59
60class CarUI: public CompoundUserInfo<Car>
61{
62private:
63        BindedElement<int> year; 
64        BindedElement<int> kilometers; 
65        BindedElement<string> manufacturer; 
66public:
67        CarUI()
68                :CompoundUserInfo<Car>("car"),
69                year( this, "year", 0 ),
70                kilometers( this, "kilometers", 0 ),
71                manufacturer( this, "manufacturer", "unknown")
72        {
73        }
74private:
75
76        Car* AssemblyInstance()
77        {
78                // assembly new instance
79                return new Car( year.value, manufacturer.value, kilometers.value );
80        }
81
82        bool DisassemblyInstance(Car& instance)
83        {
84                year.value = instance.year;
85                manufacturer.value = instance.manufacturer;
86                kilometers.value = instance.kilometers;
87                return true;
88        }
89};
90
91template<> const TypedUserInfo<Car>& TypedUserInfo<Car>::instance = CarUI( );
92
93
94class BikeUI: public CompoundUserInfo<Bike>
95{
96private:
97        BindedElement<int> year; 
98        BindedElement<bool> lights; 
99        BindedElement<string> manufacturer; 
100public:
101        BikeUI()
102                :CompoundUserInfo<Bike>("bike"),
103                year( this, "year", 0 ),
104                lights( this, "electric lights", false ), 
105                manufacturer( this, "manufacturer", "unknown")
106        {
107        }
108private:
109
110        Bike* AssemblyInstance()
111        {
112                // assembly new instance
113                return new Bike( year.value, manufacturer.value, lights.value );
114        }
115
116        bool DisassemblyInstance(Bike& instance)
117        {
118                year.value = instance.year;
119                manufacturer.value = instance.manufacturer;
120                lights.value = instance.electricLights;
121                return true;
122        }
123};
124
125template<> const TypedUserInfo<Bike>& TypedUserInfo<Bike>::instance = BikeUI( );
126
127
128int main()
129{
130        Car audi( 1998, "audi", 25000);
131        Car liaz( 1992, "liaz", 1555000);
132        Bike author( 1996, "author", true );
133
134        /////////////////////////////////// SAVING ///////////////////////////
135
136        RootElement root("transport.xml");
137
138        if( !UserInfo::Disassembly( audi, root, "pepikovo") 
139                || !UserInfo::Disassembly( liaz, root, "jardovo") 
140                || !UserInfo::Disassembly( author, root, "ondrejovo")  )
141        {
142                cout << "there was some error!" << endl;
143                getchar();
144                return 0;                       
145        }
146
147        root.Save();
148        cout << "all the transport means were saved correctly" << endl;                         
149        getchar();
150
151        //////////////////////////////////// LOADING ////////////////////////////////
152
153        string whichone = "pepikovo";
154        whichone = "jardovo";
155//      whichone = "ondrejovo";
156
157        root.Load();
158        Transport *loaded = UserInfo::Assembly<Transport>( root,whichone);
159       
160        if( loaded )
161                loaded->ToString();
162        else
163                cout << "there was some error during loading!" << endl;
164               
165        getchar(); 
166        return 0;
167}
Note: See TracBrowser for help on using the browser.