]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/catalogue.cpp
Full vehicle unification
[r2c2.git] / source / libmarklin / catalogue.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
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 Marklin {
18
19 Catalogue::Catalogue():
20         scale(1),
21         gauge(1.524),
22         layout(*this)
23 { }
24
25 Catalogue::~Catalogue()
26 {
27         for(map<unsigned, TrackType *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
28                 delete i->second;
29         for(map<unsigned, VehicleType *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
30                 delete i->second;
31 }
32
33 void Catalogue::add_track(TrackType &track)
34 {
35         if(tracks.count(track.get_article_number()))
36                 throw Exception("Duplicate track type");
37
38         tracks[track.get_article_number()] = &track;
39         signal_track_added.emit(track);
40 }
41
42 const TrackType &Catalogue::get_track(unsigned art_nr) const
43 {
44         map<unsigned, TrackType *>::const_iterator i=tracks.find(art_nr);
45         if(i==tracks.end())
46                 throw KeyError("Unknown track type");
47
48         return *i->second;
49 }
50
51 void Catalogue::add_vehicle(VehicleType &veh)
52 {
53         if(vehicles.count(veh.get_article_number()))
54                 throw Exception("Duplicate vehicle type");
55
56         vehicles[veh.get_article_number()] = &veh;
57         signal_vehicle_added.emit(veh);
58 }
59
60 const VehicleType &Catalogue::get_vehicle(unsigned art_nr) const
61 {
62         map<unsigned, VehicleType *>::const_iterator i = vehicles.find(art_nr);
63         if(i==vehicles.end())
64                 throw KeyError("Unknown vehicle type");
65
66         return *i->second;
67 }
68
69
70 Catalogue::Loader::Loader(Catalogue &c):
71         DataFile::BasicLoader<Catalogue>(c)
72 {
73         add("ballast_profile", &Loader::ballast_profile);
74         add("gauge", &Loader::gauge);
75         add("layout", &Loader::layout);
76         add("rail_profile", &Loader::rail_profile);
77         add("scale", &Loader::scale);
78         add("track", &Loader::track);
79         add("vehicle", &Loader::vehicle);
80 }
81
82 void Catalogue::Loader::ballast_profile()
83 {
84         load_sub(obj.ballast_profile);
85 }
86
87 void Catalogue::Loader::gauge(float g)
88 {
89         obj.gauge = g/1000;
90         obj.path_profile = Profile();
91         obj.path_profile.append_point(Point(0.1*obj.gauge, 0));
92         obj.path_profile.append_point(Point(-0.1*obj.gauge, 0));
93 }
94
95 void Catalogue::Loader::layout()
96 {
97         load_sub(obj.layout);
98 }
99
100 void Catalogue::Loader::rail_profile()
101 {
102         load_sub(obj.rail_profile);
103 }
104
105 void Catalogue::Loader::scale(float n, float d)
106 {
107         obj.scale = n/d;
108 }
109
110 void Catalogue::Loader::track(unsigned art_nr)
111 {
112         RefPtr<TrackType> trk = new TrackType(art_nr);
113         load_sub(*trk);
114         obj.add_track(*trk);
115         trk.release();
116 }
117
118 void Catalogue::Loader::vehicle(unsigned art_nr)
119 {
120         RefPtr<VehicleType> veh = new VehicleType(art_nr);
121         load_sub(*veh);
122         obj.add_vehicle(*veh);
123         veh.release();
124 }
125
126 } // namespace Marklin