]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/catalogue.cpp
d86fac071383852f5f241784b9240226e9b5af51
[r2c2.git] / source / libr2c2 / catalogue.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/core/refptr.h>
3 #include <msp/datafile/parser.h>
4 #include "catalogue.h"
5 #include "tracktype.h"
6 #include "vehicletype.h"
7
8 using namespace std;
9 using namespace Msp;
10
11 namespace R2C2 {
12
13 Catalogue::Catalogue():
14         scale(1),
15         gauge(1.524),
16         layout(*this)
17 { }
18
19 Catalogue::~Catalogue()
20 {
21         for(TrackMap::iterator i=tracks.begin(); i!=tracks.end(); ++i)
22                 delete i->second;
23         for(VehicleMap::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
24                 delete i->second;
25 }
26
27 float Catalogue::get_rail_elevation() const
28 {
29         return ballast_profile.get_height()+rail_profile.get_height();
30 }
31
32 void Catalogue::add_track(TrackType &track)
33 {
34         insert_unique(tracks, track.get_article_number(), &track);
35         signal_track_added.emit(track);
36 }
37
38 const TrackType &Catalogue::get_track(const ArticleNumber &art_nr) const
39 {
40         return *get_item(tracks, art_nr);
41 }
42
43 void Catalogue::add_vehicle(VehicleType &veh)
44 {
45         insert_unique(vehicles, veh.get_article_number(), &veh);
46         signal_vehicle_added.emit(veh);
47 }
48
49 const VehicleType &Catalogue::get_vehicle(const ArticleNumber &art_nr) const
50 {
51         return *get_item(vehicles, art_nr);
52 }
53
54
55 Catalogue::Loader::Loader(Catalogue &c):
56         DataFile::ObjectLoader<Catalogue>(c)
57 {
58         add("ballast_profile", &Loader::ballast_profile);
59         add("gauge", &Loader::gauge);
60         add("layout", &Loader::layout);
61         add("rail_profile", &Loader::rail_profile);
62         add("scale", &Loader::scale);
63         add("track", static_cast<void (Loader::*)(unsigned)>(&Loader::track));
64         add("track", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::track));
65         add("track_technique", &Catalogue::track_technique);
66         add("vehicle", static_cast<void (Loader::*)(unsigned)>(&Loader::vehicle));
67         add("vehicle", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::vehicle));
68 }
69
70 void Catalogue::Loader::ballast_profile()
71 {
72         load_sub(obj.ballast_profile);
73 }
74
75 void Catalogue::Loader::gauge(float g)
76 {
77         obj.gauge = g/1000;
78         obj.path_profile = Profile();
79         obj.path_profile.append_vertex(Vector(0.1*obj.gauge, 0), false);
80         obj.path_profile.append_vertex(Vector(-0.1*obj.gauge, 0), false);
81 }
82
83 void Catalogue::Loader::layout()
84 {
85         load_sub(obj.layout);
86 }
87
88 void Catalogue::Loader::rail_profile()
89 {
90         load_sub(obj.rail_profile);
91 }
92
93 void Catalogue::Loader::scale(float n, float d)
94 {
95         obj.scale = n/d;
96 }
97
98 void Catalogue::Loader::track(unsigned art_nr)
99 {
100         track(ArticleNumber(art_nr));
101 }
102
103 void Catalogue::Loader::track(ArticleNumber art_nr)
104 {
105         if(obj.tracks.count(art_nr))
106                 throw key_error(art_nr);
107
108         RefPtr<TrackType> trk = new TrackType(art_nr);
109         load_sub(*trk);
110         obj.add_track(*trk.release());
111 }
112
113 void Catalogue::Loader::vehicle(unsigned art_nr)
114 {
115         vehicle(ArticleNumber(art_nr));
116 }
117
118 void Catalogue::Loader::vehicle(ArticleNumber art_nr)
119 {
120         if(obj.vehicles.count(art_nr))
121                 throw key_error(art_nr);
122
123         RefPtr<VehicleType> veh = new VehicleType(art_nr);
124         load_sub(*veh);
125         obj.add_vehicle(*veh.release());
126 }
127
128 } // namespace R2C2