1 | |
---|
2 | #include <string> |
---|
3 | |
---|
4 | #include <user_info.h> |
---|
5 | |
---|
6 | using std::string; |
---|
7 | using namespace std; |
---|
8 | using namespace bdm; |
---|
9 | |
---|
10 | class Passenger : public bdmroot |
---|
11 | { |
---|
12 | public: |
---|
13 | |
---|
14 | Passenger() |
---|
15 | { |
---|
16 | } |
---|
17 | }; |
---|
18 | |
---|
19 | class Human : public Passenger |
---|
20 | { |
---|
21 | public: |
---|
22 | string name; |
---|
23 | |
---|
24 | Human() |
---|
25 | { |
---|
26 | name = "none"; |
---|
27 | } |
---|
28 | |
---|
29 | virtual void from_setting( const Setting &root ) |
---|
30 | { |
---|
31 | root.lookupValue( "name", name ); |
---|
32 | } |
---|
33 | |
---|
34 | virtual void to_setting( Setting &root ) const |
---|
35 | { |
---|
36 | Setting &name_setting = root.add("name", Setting::TypeString ); |
---|
37 | name_setting = name; |
---|
38 | } |
---|
39 | |
---|
40 | string ToString() |
---|
41 | { |
---|
42 | return name; |
---|
43 | } |
---|
44 | }; |
---|
45 | |
---|
46 | UIREGISTER(Human); |
---|
47 | |
---|
48 | class Robot : public Passenger |
---|
49 | { |
---|
50 | public: |
---|
51 | int number; |
---|
52 | Array<string> software; |
---|
53 | |
---|
54 | Robot() : software() |
---|
55 | { |
---|
56 | number = -1; |
---|
57 | } |
---|
58 | |
---|
59 | virtual void from_setting( const Setting &root ) |
---|
60 | { |
---|
61 | root.lookupValue( "number", number ); |
---|
62 | |
---|
63 | UI::get( software, root, "software" ); |
---|
64 | } |
---|
65 | |
---|
66 | virtual void to_setting( Setting &root ) const |
---|
67 | { |
---|
68 | Setting &number_setting = root.add("number", Setting::TypeInt ); |
---|
69 | number_setting = number; |
---|
70 | |
---|
71 | UI::save( software, root, "software" ); |
---|
72 | } |
---|
73 | |
---|
74 | string ToString() |
---|
75 | { |
---|
76 | stringstream stream; |
---|
77 | stream << number; |
---|
78 | for( int i = 0; i<software.length(); i++) |
---|
79 | stream << "_" + software(i); |
---|
80 | return stream.str(); |
---|
81 | } |
---|
82 | }; |
---|
83 | |
---|
84 | UIREGISTER(Robot); |
---|
85 | |
---|
86 | class Transport : public bdmroot |
---|
87 | { |
---|
88 | public: |
---|
89 | int year; |
---|
90 | string manufacturer; |
---|
91 | |
---|
92 | Transport() |
---|
93 | { |
---|
94 | year = 1900; |
---|
95 | manufacturer = "unknown"; |
---|
96 | } |
---|
97 | |
---|
98 | Transport( int year, string manufacturer ) |
---|
99 | : year( year ), manufacturer( manufacturer ) |
---|
100 | { |
---|
101 | } |
---|
102 | |
---|
103 | virtual void from_setting( const Setting &root ) |
---|
104 | { |
---|
105 | root.lookupValue( "year", year ); |
---|
106 | root.lookupValue( "manufacturer", manufacturer ); |
---|
107 | } |
---|
108 | |
---|
109 | virtual void to_setting( Setting &root ) const |
---|
110 | { |
---|
111 | Setting &year_setting = root.add("year", Setting::TypeInt ); |
---|
112 | year_setting = year; |
---|
113 | |
---|
114 | Setting &manufacturer_setting = root.add("manufacturer", Setting::TypeString ); |
---|
115 | manufacturer_setting = manufacturer; |
---|
116 | } |
---|
117 | }; |
---|
118 | |
---|
119 | class Car : public Transport |
---|
120 | { |
---|
121 | public: |
---|
122 | int kilometers; |
---|
123 | Array<Passenger*> passengers; |
---|
124 | |
---|
125 | Car() : Transport() |
---|
126 | { |
---|
127 | kilometers = 0; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | Car( int year, string manufacturer, int kilometers ) |
---|
132 | : Transport( year, manufacturer ), kilometers( kilometers ) |
---|
133 | { |
---|
134 | } |
---|
135 | |
---|
136 | virtual void from_setting( const Setting &root ) |
---|
137 | { |
---|
138 | Transport::from_setting( root ); |
---|
139 | |
---|
140 | root.lookupValue( "kilometers", kilometers ); |
---|
141 | |
---|
142 | UI::get( passengers, root, "passengers" ); |
---|
143 | } |
---|
144 | |
---|
145 | virtual void to_setting( Setting &root ) const |
---|
146 | { |
---|
147 | Transport::to_setting( root ); |
---|
148 | |
---|
149 | Setting &kilometers_setting = root.add("kilometers", Setting::TypeInt ); |
---|
150 | kilometers_setting = kilometers; |
---|
151 | |
---|
152 | UI::save( passengers, root, "passengers" ); |
---|
153 | } |
---|
154 | |
---|
155 | string ToString() |
---|
156 | { |
---|
157 | stringstream stream; |
---|
158 | stream << "A car made in " << year << " by " << manufacturer << ", having " << kilometers << " kilometers on the clock."; |
---|
159 | if( passengers.length() ) |
---|
160 | { |
---|
161 | stream << "The names of passengers are as follows: "; |
---|
162 | for( int i=0; i<passengers.length(); i++) |
---|
163 | stream << passengers(i)->ToString() << " "; |
---|
164 | } |
---|
165 | return stream.str(); |
---|
166 | } |
---|
167 | }; |
---|
168 | |
---|
169 | UIREGISTER(Car); |
---|
170 | |
---|
171 | class Bike : public Transport |
---|
172 | { |
---|
173 | public: |
---|
174 | bool electricLights; |
---|
175 | mat matr; |
---|
176 | |
---|
177 | Bike() : Transport(), matr("2,2;3,4") |
---|
178 | { |
---|
179 | electricLights = false; |
---|
180 | } |
---|
181 | |
---|
182 | Bike( int year, string manufacturer, bool electricLights ) |
---|
183 | : Transport( year, manufacturer ), electricLights( electricLights ) |
---|
184 | { |
---|
185 | } |
---|
186 | |
---|
187 | ~Bike() |
---|
188 | { |
---|
189 | } |
---|
190 | |
---|
191 | void from_setting( const Setting &root ) |
---|
192 | { |
---|
193 | Transport::from_setting( root ); |
---|
194 | |
---|
195 | root.lookupValue( "electricLights", electricLights ); |
---|
196 | |
---|
197 | UI::get( matr, root, "matr" ); |
---|
198 | } |
---|
199 | |
---|
200 | void to_setting( Setting &root ) const |
---|
201 | { |
---|
202 | Transport::to_setting( root ); |
---|
203 | |
---|
204 | Setting &electricLights_setting = root.add("electricLights", Setting::TypeBoolean ); |
---|
205 | electricLights_setting = electricLights; |
---|
206 | |
---|
207 | UI::save( matr, root, "matr" ); |
---|
208 | } |
---|
209 | |
---|
210 | string ToString() |
---|
211 | { |
---|
212 | stringstream stream; |
---|
213 | stream << "a bike made in " << year << " by " << manufacturer; |
---|
214 | if( electricLights ) stream << " with electric lights included"; |
---|
215 | return stream.str(); |
---|
216 | } |
---|
217 | }; |
---|
218 | |
---|
219 | UIREGISTER(Bike); |
---|
220 | |
---|
221 | int main() |
---|
222 | { |
---|
223 | //////////////////////////////////// LOADING //////////////////////////////// |
---|
224 | UI_File in("testUI_in.cfg"); |
---|
225 | in.load(); |
---|
226 | Transport *pepikovo = UI::build<Transport>( in, "pepikovo"); |
---|
227 | cout << "pepikovo: " << pepikovo->ToString() << endl; |
---|
228 | Transport *jardovo = UI::build<Transport>( in, "jardovo"); |
---|
229 | cout << "jardovo: " << jardovo->ToString() << endl; |
---|
230 | Transport *ondrejovo = UI::build<Transport>( in, "ondrejovo"); |
---|
231 | cout << "ondrejovo: " << ondrejovo->ToString() << endl; |
---|
232 | Transport *elisky = UI::build<Transport>( in, "elisky"); |
---|
233 | cout << "elisky: " << elisky->ToString() << endl; |
---|
234 | Transport *kati = UI::build<Transport>( in, "kati"); |
---|
235 | cout << "kati: " << kati->ToString() << endl; |
---|
236 | getchar(); |
---|
237 | |
---|
238 | /////////////////////////////////// SAVING ////////////////////////// |
---|
239 | |
---|
240 | Car audi( 1968, "zyl", 200); |
---|
241 | Car liaz( 1989, "skoda", 1000); |
---|
242 | Bike author( 2001, "noname", false ); |
---|
243 | UI_File out("testUI_out.cfg"); |
---|
244 | |
---|
245 | UI::save( &audi, out, "marty"); |
---|
246 | UI::save( &liaz, out, "bohousovo"); |
---|
247 | UI::save( &author, out, "karlovo"); |
---|
248 | UI::save( pepikovo, out, "pepikovo"); |
---|
249 | out.save(); |
---|
250 | |
---|
251 | cout << "all the transport means were saved correctly" << endl; |
---|
252 | getchar(); |
---|
253 | return 0; |
---|
254 | } |
---|