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