[82] | 1 | #include "userinfo.h" |
---|
| 2 | |
---|
[151] | 3 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 4 | ////////////////////////////////////////// BASIC VALUED USER INFOS /////////////////////////// |
---|
| 5 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 6 | |
---|
| 7 | class BoolUI: public ValuedUserInfo<bool> |
---|
| 8 | { |
---|
| 9 | private: |
---|
| 10 | |
---|
| 11 | bool* AssemblyInstance() |
---|
| 12 | { |
---|
| 13 | if( value == "true" ) |
---|
| 14 | return new bool( true ); |
---|
| 15 | else |
---|
| 16 | return new bool( false ); |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | bool DisassemblyInstance(bool &instance) |
---|
| 20 | { |
---|
| 21 | if( instance ) |
---|
| 22 | value = "true"; |
---|
| 23 | else |
---|
| 24 | value = "false"; |
---|
| 25 | return true; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | public: |
---|
| 29 | |
---|
| 30 | BoolUI() |
---|
| 31 | : ValuedUserInfo<bool>("bool") |
---|
| 32 | { |
---|
| 33 | } |
---|
| 34 | }; |
---|
| 35 | |
---|
| 36 | const TypedUserInfo<bool>& TypedUserInfo<bool>::instance = BoolUI(); |
---|
| 37 | |
---|
| 38 | |
---|
[142] | 39 | class IntUI: public ValuedUserInfo<int> |
---|
[122] | 40 | { |
---|
[151] | 41 | private: |
---|
| 42 | |
---|
| 43 | int* AssemblyInstance() |
---|
| 44 | { |
---|
| 45 | return new int( atoi( value.c_str()) ); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | bool DisassemblyInstance(int &instance) |
---|
| 49 | { |
---|
| 50 | char buff[30]; |
---|
| 51 | sprintf(buff, "%d", instance ); |
---|
| 52 | value = buff; |
---|
| 53 | return true; |
---|
| 54 | } |
---|
| 55 | |
---|
[122] | 56 | public: |
---|
[146] | 57 | IntUI():ValuedUserInfo<int>("int") |
---|
| 58 | { |
---|
| 59 | } |
---|
[151] | 60 | }; |
---|
[82] | 61 | |
---|
[151] | 62 | const TypedUserInfo<int>& TypedUserInfo<int>::instance = IntUI(); |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | class DoubleUI: public ValuedUserInfo<double> |
---|
| 66 | { |
---|
| 67 | private: |
---|
| 68 | |
---|
| 69 | double* AssemblyInstance() |
---|
[122] | 70 | { |
---|
[151] | 71 | return new double( atof( value.c_str()) ); |
---|
[122] | 72 | } |
---|
| 73 | |
---|
[151] | 74 | bool DisassemblyInstance(double &instance) |
---|
[146] | 75 | { |
---|
| 76 | char buff[30]; |
---|
[151] | 77 | sprintf(buff, "%f", instance ); |
---|
[146] | 78 | value = buff; |
---|
[151] | 79 | return true; |
---|
[146] | 80 | } |
---|
[151] | 81 | |
---|
| 82 | public: |
---|
| 83 | DoubleUI():ValuedUserInfo<double>("double") |
---|
| 84 | { |
---|
| 85 | } |
---|
[122] | 86 | }; |
---|
| 87 | |
---|
[151] | 88 | const TypedUserInfo<double>& TypedUserInfo<double>::instance = DoubleUI(); |
---|
[122] | 89 | |
---|
| 90 | |
---|
[142] | 91 | class StringUI: public ValuedUserInfo<string> |
---|
[122] | 92 | { |
---|
[151] | 93 | private: |
---|
| 94 | string* AssemblyInstance() |
---|
| 95 | { |
---|
| 96 | return new string( value ); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | bool DisassemblyInstance(string &instance) |
---|
| 100 | { |
---|
| 101 | value = instance; |
---|
| 102 | return true; |
---|
| 103 | } |
---|
| 104 | |
---|
[122] | 105 | public: |
---|
[146] | 106 | StringUI():ValuedUserInfo<string>("string") |
---|
| 107 | { |
---|
| 108 | } |
---|
[151] | 109 | }; |
---|
[146] | 110 | |
---|
[151] | 111 | const TypedUserInfo<string>& TypedUserInfo<string>::instance = StringUI(); |
---|
| 112 | |
---|
| 113 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 114 | ////////////////////////////////////////// EXAMPLE CLASSES DEFINITION //////////////////////// |
---|
| 115 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 116 | class Transport |
---|
| 117 | { |
---|
| 118 | public: |
---|
| 119 | const int year; |
---|
| 120 | const string manufacturer; |
---|
| 121 | |
---|
| 122 | Transport( int year, string manufacturer ) |
---|
| 123 | : year( year ), manufacturer( manufacturer ) |
---|
[122] | 124 | { |
---|
| 125 | } |
---|
[146] | 126 | |
---|
[151] | 127 | virtual public void ToString() = 0; |
---|
| 128 | }; |
---|
| 129 | |
---|
| 130 | class Car : public Transport |
---|
| 131 | { |
---|
| 132 | public: |
---|
| 133 | const int kilometers; |
---|
| 134 | |
---|
| 135 | Car( int year, string manufacturer, int kilometers ) |
---|
| 136 | : Transport( year, manufacturer ), kilometers( kilometers ) |
---|
[146] | 137 | { |
---|
| 138 | } |
---|
[151] | 139 | |
---|
| 140 | void ToString() |
---|
| 141 | { |
---|
| 142 | cout << "a car made in " << year << " by " << manufacturer << ", having " << kilometers << " kilometers on the clock." << endl; |
---|
| 143 | } |
---|
[122] | 144 | }; |
---|
| 145 | |
---|
[151] | 146 | class Bike : public Transport |
---|
[146] | 147 | { |
---|
| 148 | public: |
---|
[151] | 149 | const bool electricLights; |
---|
[142] | 150 | |
---|
[151] | 151 | Bike( int age, string manufacturer, bool electricLights ) |
---|
| 152 | : Transport( age, manufacturer ), electricLights( electricLights ) |
---|
[146] | 153 | { |
---|
| 154 | } |
---|
[151] | 155 | |
---|
| 156 | void ToString() |
---|
| 157 | { |
---|
| 158 | cout << "a bike made in " << year << " by " << manufacturer; |
---|
| 159 | if( electricLights ) cout << " with electric lights included"; |
---|
| 160 | cout << endl; |
---|
| 161 | } |
---|
[146] | 162 | }; |
---|
| 163 | |
---|
[151] | 164 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 165 | ////////////////////////////////////////// AND RELATED USER INFOS //////////////////////////// |
---|
| 166 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 167 | |
---|
| 168 | |
---|
[146] | 169 | class CarUI: public CompoundUserInfo<Car> |
---|
| 170 | { |
---|
[151] | 171 | private: |
---|
| 172 | BindedElement<int> year; |
---|
| 173 | BindedElement<int> kilometers; |
---|
| 174 | BindedElement<string> manufacturer; |
---|
[146] | 175 | public: |
---|
| 176 | CarUI() |
---|
| 177 | :CompoundUserInfo<Car>("car"), |
---|
[151] | 178 | year( this, "year", 0 ), |
---|
| 179 | kilometers( this, "kilometers", 0 ), |
---|
| 180 | manufacturer( this, "manufacturer", "unknown") |
---|
[146] | 181 | { |
---|
| 182 | } |
---|
[151] | 183 | private: |
---|
[146] | 184 | |
---|
[151] | 185 | Car* AssemblyInstance() |
---|
[146] | 186 | { |
---|
[151] | 187 | // assembly new instance |
---|
| 188 | return new Car( year.value, manufacturer.value, kilometers.value ); |
---|
[146] | 189 | } |
---|
| 190 | |
---|
[151] | 191 | bool DisassemblyInstance(Car& instance) |
---|
[146] | 192 | { |
---|
[151] | 193 | year.value = instance.year; |
---|
| 194 | manufacturer.value = instance.manufacturer; |
---|
| 195 | kilometers.value = instance.kilometers; |
---|
| 196 | return true; |
---|
[146] | 197 | } |
---|
| 198 | }; |
---|
| 199 | |
---|
[151] | 200 | const TypedUserInfo<Car>& TypedUserInfo<Car>::instance = CarUI( ); |
---|
[146] | 201 | |
---|
| 202 | |
---|
[151] | 203 | class BikeUI: public CompoundUserInfo<Bike> |
---|
| 204 | { |
---|
| 205 | private: |
---|
| 206 | BindedElement<int> year; |
---|
| 207 | BindedElement<bool> lights; |
---|
| 208 | BindedElement<string> manufacturer; |
---|
| 209 | public: |
---|
| 210 | BikeUI() |
---|
| 211 | :CompoundUserInfo<Bike>("bike"), |
---|
| 212 | year( this, "year", 0 ), |
---|
| 213 | lights( this, "electric_lights", false ), // jen jedno slovo! |
---|
| 214 | manufacturer( this, "manufacturer", "unknown") |
---|
| 215 | { |
---|
| 216 | } |
---|
| 217 | private: |
---|
| 218 | |
---|
| 219 | Bike* AssemblyInstance() |
---|
| 220 | { |
---|
| 221 | // assembly new instance |
---|
| 222 | return new Bike( year.value, manufacturer.value, lights.value ); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | bool DisassemblyInstance(Bike& instance) |
---|
| 226 | { |
---|
| 227 | year.value = instance.year; |
---|
| 228 | manufacturer.value = instance.manufacturer; |
---|
| 229 | lights.value = instance.electricLights; |
---|
| 230 | return true; |
---|
| 231 | } |
---|
| 232 | }; |
---|
| 233 | |
---|
| 234 | const TypedUserInfo<Bike>& TypedUserInfo<Bike>::instance = BikeUI( ); |
---|
| 235 | |
---|
| 236 | |
---|
[82] | 237 | int main() |
---|
| 238 | { |
---|
[151] | 239 | Car audi( 1998, "audi", 25000); |
---|
| 240 | Car liaz( 1992, "liaz", 1555000); |
---|
| 241 | Bike author( 1996, "author", true ); |
---|
| 242 | |
---|
[146] | 243 | /////////////////////////////////// SAVING /////////////////////////// |
---|
[122] | 244 | |
---|
[151] | 245 | RootElement root("transport.xml"); |
---|
| 246 | |
---|
| 247 | if( !UserInfo::Disassembly( audi, root, "pepikovo") |
---|
| 248 | || !UserInfo::Disassembly( liaz, root, "jardovo") |
---|
| 249 | || !UserInfo::Disassembly( author, root, "ondrejovo") ) |
---|
[146] | 250 | { |
---|
| 251 | cout << "there was some error!" << endl; |
---|
| 252 | getchar(); |
---|
| 253 | return 0; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | root.Save(); |
---|
[151] | 257 | cout << "all the transport means were saved correctly" << endl; |
---|
[146] | 258 | getchar(); |
---|
| 259 | |
---|
| 260 | //////////////////////////////////// LOADING //////////////////////////////// |
---|
| 261 | |
---|
[151] | 262 | string whichone = "pepikovo"; |
---|
| 263 | // whichone = "jardovo"; |
---|
| 264 | whichone = "ondrejovo"; |
---|
[146] | 265 | |
---|
[142] | 266 | root.Load(); |
---|
[151] | 267 | Transport *loaded = UserInfo::Assembly<Transport>( root,whichone); |
---|
[142] | 268 | |
---|
[151] | 269 | if( loaded ) |
---|
| 270 | loaded->ToString(); |
---|
[146] | 271 | else |
---|
[151] | 272 | cout << "there was some error during loading!" << endl; |
---|
[146] | 273 | |
---|
[142] | 274 | getchar(); |
---|
[82] | 275 | return 0; |
---|
[122] | 276 | } |
---|