]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/catalogue.cpp
Support trains with multiple vehicles
[r2c2.git] / source / libmarklin / catalogue.cpp
index bdd5369f9130d504d2a22182a6c1b920f111c892..286407cc09326818973d5a7c18593ac81108efb9 100644 (file)
@@ -26,7 +26,7 @@ Catalogue::~Catalogue()
 {
        for(map<unsigned, TrackType *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
                delete i->second;
-       for(map<unsigned, LocoType *>::iterator i=locos.begin(); i!=locos.end(); ++i)
+       for(map<unsigned, VehicleType *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
                delete i->second;
 }
 
@@ -39,15 +39,6 @@ void Catalogue::add_track(TrackType &track)
        signal_track_added.emit(track);
 }
 
-void Catalogue::add_locomotive(LocoType &loco)
-{
-       if(locos.count(loco.get_article_number()))
-               throw Exception("Duplicate track type");
-
-       locos[loco.get_article_number()] = &loco;
-       signal_loco_added.emit(loco);
-}
-
 const TrackType &Catalogue::get_track(unsigned art_nr) const
 {
        map<unsigned, TrackType *>::const_iterator i=tracks.find(art_nr);
@@ -57,15 +48,33 @@ const TrackType &Catalogue::get_track(unsigned art_nr) const
        return *i->second;
 }
 
-const LocoType &Catalogue::get_locomotive(unsigned art_nr) const
+void Catalogue::add_vehicle(VehicleType &veh)
 {
-       map<unsigned, LocoType *>::const_iterator i=locos.find(art_nr);
-       if(i==locos.end())
-               throw KeyError("Unknown locomotive type");
+       if(vehicles.count(veh.get_article_number()))
+               throw Exception("Duplicate vehicle type");
+
+       vehicles[veh.get_article_number()] = &veh;
+       signal_vehicle_added.emit(veh);
+}
+
+const VehicleType &Catalogue::get_vehicle(unsigned art_nr) const
+{
+       map<unsigned, VehicleType *>::const_iterator i = vehicles.find(art_nr);
+       if(i==vehicles.end())
+               throw KeyError("Unknown vehicle type");
 
        return *i->second;
 }
 
+const LocoType &Catalogue::get_locomotive(unsigned art_nr) const
+{
+       const VehicleType &veh = get_vehicle(art_nr);
+       if(const LocoType *loco = dynamic_cast<const LocoType *>(&veh))
+               return *loco;
+       
+       throw Exception("Vehicle is not a locomotive");
+}
+
 
 Catalogue::Loader::Loader(Catalogue &c):
        DataFile::BasicLoader<Catalogue>(c)
@@ -77,6 +86,7 @@ Catalogue::Loader::Loader(Catalogue &c):
        add("rail_profile", &Loader::rail_profile);
        add("scale", &Loader::scale);
        add("track", &Loader::track);
+       add("vehicle", &Loader::vehicle);
 }
 
 void Catalogue::Loader::ballast_profile()
@@ -101,7 +111,7 @@ void Catalogue::Loader::locomotive(unsigned art_nr)
 {
        RefPtr<LocoType> loco = new LocoType(art_nr);
        load_sub(*loco);
-       obj.add_locomotive(*loco);
+       obj.add_vehicle(*loco);
        loco.release();
 }
 
@@ -123,4 +133,12 @@ void Catalogue::Loader::track(unsigned art_nr)
        trk.release();
 }
 
+void Catalogue::Loader::vehicle(unsigned art_nr)
+{
+       RefPtr<VehicleType> veh = new VehicleType(art_nr);
+       load_sub(*veh);
+       obj.add_vehicle(*veh);
+       veh.release();
+}
+
 } // namespace Marklin