1 | #include "userinfo.h" |
---|
2 | |
---|
3 | |
---|
4 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
5 | ////////////////////////////////////////// CLASSES DEFINITION //////////////////////// |
---|
6 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
7 | class Transport |
---|
8 | { |
---|
9 | public: |
---|
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 | |
---|
21 | class Car : public Transport |
---|
22 | { |
---|
23 | public: |
---|
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 | |
---|
37 | class Bike : public Transport |
---|
38 | { |
---|
39 | public: |
---|
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 | |
---|
60 | class CarUI: public CompoundUserInfo<Car> |
---|
61 | { |
---|
62 | private: |
---|
63 | BindedElement<int> year; |
---|
64 | BindedElement<int> kilometers; |
---|
65 | BindedElement<string> manufacturer; |
---|
66 | public: |
---|
67 | CarUI() |
---|
68 | :CompoundUserInfo<Car>("car"), |
---|
69 | #pragma warning(push) |
---|
70 | #pragma warning(disable: 4355) |
---|
71 | year( this, "year", 0 ), |
---|
72 | kilometers( this, "kilometers", 0 ), |
---|
73 | manufacturer( this, "manufacturer", "unknown") |
---|
74 | # pragma warning(pop) |
---|
75 | { |
---|
76 | } |
---|
77 | private: |
---|
78 | |
---|
79 | Car* AssemblyInstance() |
---|
80 | { |
---|
81 | // assembly new instance |
---|
82 | return new Car( year.value, manufacturer.value, kilometers.value ); |
---|
83 | } |
---|
84 | |
---|
85 | bool DisassemblyInstance(Car& instance) |
---|
86 | { |
---|
87 | year.value = instance.year; |
---|
88 | manufacturer.value = instance.manufacturer; |
---|
89 | kilometers.value = instance.kilometers; |
---|
90 | return true; |
---|
91 | } |
---|
92 | }; |
---|
93 | |
---|
94 | template<> const TypedUserInfo<Car>& TypedUserInfo<Car>::instance = CarUI( ); |
---|
95 | |
---|
96 | |
---|
97 | class BikeUI: public CompoundUserInfo<Bike> |
---|
98 | { |
---|
99 | private: |
---|
100 | BindedElement<int> year; |
---|
101 | BindedElement<bool> lights; |
---|
102 | BindedElement<string> manufacturer; |
---|
103 | public: |
---|
104 | BikeUI() |
---|
105 | :CompoundUserInfo<Bike>("bike"), |
---|
106 | #pragma warning(push) |
---|
107 | #pragma warning(disable: 4355) |
---|
108 | year( this, "year", 0 ), |
---|
109 | lights( this, "electric lights", false ), |
---|
110 | manufacturer( this, "manufacturer", "unknown") |
---|
111 | #pragma warning(pop) |
---|
112 | { |
---|
113 | } |
---|
114 | private: |
---|
115 | |
---|
116 | Bike* AssemblyInstance() |
---|
117 | { |
---|
118 | // assembly new instance |
---|
119 | return new Bike( year.value, manufacturer.value, lights.value ); |
---|
120 | } |
---|
121 | |
---|
122 | bool DisassemblyInstance(Bike& instance) |
---|
123 | { |
---|
124 | year.value = instance.year; |
---|
125 | manufacturer.value = instance.manufacturer; |
---|
126 | lights.value = instance.electricLights; |
---|
127 | return true; |
---|
128 | } |
---|
129 | }; |
---|
130 | |
---|
131 | template<> const TypedUserInfo<Bike>& TypedUserInfo<Bike>::instance = BikeUI( ); |
---|
132 | |
---|
133 | |
---|
134 | int main() |
---|
135 | { |
---|
136 | Car audi( 1998, "audi", 25000); |
---|
137 | Car liaz( 1992, "liaz", 1555000); |
---|
138 | Bike author( 1996, "author", true ); |
---|
139 | |
---|
140 | /////////////////////////////////// SAVING /////////////////////////// |
---|
141 | |
---|
142 | RootElement root("transport.xml"); |
---|
143 | |
---|
144 | if( !UserInfo::Disassembly( audi, root, "pepikovo") |
---|
145 | || !UserInfo::Disassembly( liaz, root, "jardovo") |
---|
146 | || !UserInfo::Disassembly( author, root, "ondrejovo") ) |
---|
147 | { |
---|
148 | cout << "there was some error!" << endl; |
---|
149 | getchar(); |
---|
150 | return 0; |
---|
151 | } |
---|
152 | |
---|
153 | root.Save(); |
---|
154 | cout << "all the transport means were saved correctly" << endl; |
---|
155 | //getchar(); |
---|
156 | |
---|
157 | //////////////////////////////////// LOADING //////////////////////////////// |
---|
158 | |
---|
159 | string whichone = "pepikovo"; |
---|
160 | whichone = "jardovo"; |
---|
161 | // whichone = "ondrejovo"; |
---|
162 | |
---|
163 | root.Load(); |
---|
164 | Transport *loaded = UserInfo::Assembly<Transport>( root,whichone); |
---|
165 | |
---|
166 | if( loaded ) |
---|
167 | loaded->ToString(); |
---|
168 | else |
---|
169 | cout << "there was some error during loading!" << endl; |
---|
170 | |
---|
171 | getchar(); |
---|
172 | return 0; |
---|
173 | } |
---|