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