root/tests/testUI.cpp @ 218

Revision 218, 4.0 kB (checked in by smidl, 16 years ago)

navrat simulatoru + zmeny v pmsm

  • Property svn:eol-style set to native
RevLine 
[82]1#include "userinfo.h"
2
[151]3
4//////////////////////////////////////////////////////////////////////////////////////////////
[156]5////////////////////////////////////////// CLASSES DEFINITION ////////////////////////
[151]6//////////////////////////////////////////////////////////////////////////////////////////////
7class Transport
8{
9public:
10        const int year;
11        const string manufacturer;
12
13        Transport( int year, string manufacturer )
14                : year( year ), manufacturer( manufacturer )
[122]15        {
16        }
[146]17
[154]18        virtual void ToString() = 0;
[151]19};
20
21class Car : public Transport
22{
23public:
24        const int kilometers;
25
26        Car( int year, string manufacturer, int kilometers )
27                : Transport( year, manufacturer ), kilometers( kilometers )
[146]28        {
29        }
[151]30
31        void ToString()
32        {
33                cout << "a car made in " << year << " by " << manufacturer << ", having " << kilometers << " kilometers on the clock." << endl;
34        }
[122]35};
36
[151]37class Bike : public Transport
[146]38{
39public:
[151]40        const bool electricLights;
[142]41
[151]42        Bike( int age, string manufacturer, bool electricLights )
43                : Transport( age, manufacturer ), electricLights( electricLights )
[146]44        {
45        }
[151]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        }
[146]53};
54
[151]55//////////////////////////////////////////////////////////////////////////////////////////////
56////////////////////////////////////////// AND RELATED USER INFOS ////////////////////////////
57//////////////////////////////////////////////////////////////////////////////////////////////
58
59
[146]60class CarUI: public CompoundUserInfo<Car>
61{
[151]62private:
63        BindedElement<int> year; 
64        BindedElement<int> kilometers; 
65        BindedElement<string> manufacturer; 
[146]66public:
67        CarUI()
68                :CompoundUserInfo<Car>("car"),
[159]69#pragma warning(push)
70#pragma warning(disable: 4355)
[151]71                year( this, "year", 0 ),
72                kilometers( this, "kilometers", 0 ),
73                manufacturer( this, "manufacturer", "unknown")
[159]74# pragma warning(pop)
[146]75        {
76        }
[151]77private:
[146]78
[151]79        Car* AssemblyInstance()
[146]80        {
[151]81                // assembly new instance
82                return new Car( year.value, manufacturer.value, kilometers.value );
[146]83        }
84
[151]85        bool DisassemblyInstance(Car& instance)
[146]86        {
[151]87                year.value = instance.year;
88                manufacturer.value = instance.manufacturer;
89                kilometers.value = instance.kilometers;
90                return true;
[146]91        }
92};
93
[153]94template<> const TypedUserInfo<Car>& TypedUserInfo<Car>::instance = CarUI( );
[146]95
96
[151]97class BikeUI: public CompoundUserInfo<Bike>
98{
99private:
100        BindedElement<int> year; 
101        BindedElement<bool> lights; 
102        BindedElement<string> manufacturer; 
103public:
104        BikeUI()
105                :CompoundUserInfo<Bike>("bike"),
[159]106#pragma warning(push)
107#pragma warning(disable: 4355)
[151]108                year( this, "year", 0 ),
[156]109                lights( this, "electric lights", false ), 
[151]110                manufacturer( this, "manufacturer", "unknown")
[159]111#pragma warning(pop)
[151]112        {
113        }
114private:
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
[153]131template<> const TypedUserInfo<Bike>& TypedUserInfo<Bike>::instance = BikeUI( );
[151]132
133
[82]134int main()
135{
[151]136        Car audi( 1998, "audi", 25000);
137        Car liaz( 1992, "liaz", 1555000);
138        Bike author( 1996, "author", true );
139
[146]140        /////////////////////////////////// SAVING ///////////////////////////
[122]141
[151]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")  )
[146]147        {
148                cout << "there was some error!" << endl;
149                getchar();
150                return 0;                       
151        }
152
153        root.Save();
[151]154        cout << "all the transport means were saved correctly" << endl;                         
[218]155        //getchar();
[146]156
157        //////////////////////////////////// LOADING ////////////////////////////////
158
[151]159        string whichone = "pepikovo";
[154]160        whichone = "jardovo";
161//      whichone = "ondrejovo";
[146]162
[142]163        root.Load();
[151]164        Transport *loaded = UserInfo::Assembly<Transport>( root,whichone);
[142]165       
[151]166        if( loaded )
167                loaded->ToString();
[146]168        else
[151]169                cout << "there was some error during loading!" << endl;
[146]170               
[142]171        getchar(); 
[82]172        return 0;
[122]173}
Note: See TracBrowser for help on using the browser.