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