3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #include <msp/core/refptr.h>
9 #include <msp/datafile/parser.h>
10 #include "catalogue.h"
12 #include "tracktype.h"
19 Catalogue::Catalogue():
25 Catalogue::~Catalogue()
27 for(map<unsigned, TrackType *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
29 for(map<unsigned, VehicleType *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
33 void Catalogue::add_track(TrackType &track)
35 if(tracks.count(track.get_article_number()))
36 throw Exception("Duplicate track type");
38 tracks[track.get_article_number()] = &track;
39 signal_track_added.emit(track);
42 const TrackType &Catalogue::get_track(unsigned art_nr) const
44 map<unsigned, TrackType *>::const_iterator i=tracks.find(art_nr);
46 throw KeyError("Unknown track type");
51 void Catalogue::add_vehicle(VehicleType &veh)
53 if(vehicles.count(veh.get_article_number()))
54 throw Exception("Duplicate vehicle type");
56 vehicles[veh.get_article_number()] = &veh;
57 signal_vehicle_added.emit(veh);
60 const VehicleType &Catalogue::get_vehicle(unsigned art_nr) const
62 map<unsigned, VehicleType *>::const_iterator i = vehicles.find(art_nr);
64 throw KeyError("Unknown vehicle type");
69 const LocoType &Catalogue::get_locomotive(unsigned art_nr) const
71 const VehicleType &veh = get_vehicle(art_nr);
72 if(const LocoType *loco = dynamic_cast<const LocoType *>(&veh))
75 throw Exception("Vehicle is not a locomotive");
79 Catalogue::Loader::Loader(Catalogue &c):
80 DataFile::BasicLoader<Catalogue>(c)
82 add("ballast_profile", &Loader::ballast_profile);
83 add("gauge", &Loader::gauge);
84 add("layout", &Loader::layout);
85 add("locomotive", &Loader::locomotive);
86 add("rail_profile", &Loader::rail_profile);
87 add("scale", &Loader::scale);
88 add("track", &Loader::track);
89 add("vehicle", &Loader::vehicle);
92 void Catalogue::Loader::ballast_profile()
94 load_sub(obj.ballast_profile);
97 void Catalogue::Loader::gauge(float g)
100 obj.path_profile = Profile();
101 obj.path_profile.append_point(Point(0.1*obj.gauge, 0));
102 obj.path_profile.append_point(Point(-0.1*obj.gauge, 0));
105 void Catalogue::Loader::layout()
107 load_sub(obj.layout);
110 void Catalogue::Loader::locomotive(unsigned art_nr)
112 RefPtr<LocoType> loco = new LocoType(art_nr);
114 obj.add_vehicle(*loco);
118 void Catalogue::Loader::rail_profile()
120 load_sub(obj.rail_profile);
123 void Catalogue::Loader::scale(float n, float d)
128 void Catalogue::Loader::track(unsigned art_nr)
130 RefPtr<TrackType> trk = new TrackType(art_nr);
136 void Catalogue::Loader::vehicle(unsigned art_nr)
138 RefPtr<VehicleType> veh = new VehicleType(art_nr);
140 obj.add_vehicle(*veh);
144 } // namespace Marklin