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