]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/catalogue.cpp
Make LCD output selectable at runtime through an extra I/O pin
[r2c2.git] / source / libmarklin / catalogue.cpp
index 3ab96a99ebbadb325f821c1c52e74ba083ee1ac0..82dde18fdf9ccf76512576dc5157517f480ffd79 100644 (file)
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
 #include <msp/core/refptr.h>
 #include <msp/datafile/parser.h>
 #include "catalogue.h"
-#include "locotype.h"
 #include "tracktype.h"
+#include "vehicletype.h"
 
 using namespace std;
 using namespace Msp;
 
 namespace Marklin {
 
+Catalogue::Catalogue():
+       scale(1),
+       gauge(1.524),
+       layout(*this)
+{ }
+
 Catalogue::~Catalogue()
 {
-       for(map<unsigned, TrackType *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+       for(TrackMap::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               delete i->second;
+       for(VehicleMap::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
                delete i->second;
 }
 
-TrackType &Catalogue::get_track(unsigned art_nr) const
+float Catalogue::get_rail_elevation() const
 {
-       map<unsigned, TrackType *>::const_iterator i=tracks.find(art_nr);
+       return ballast_profile.get_height()+rail_profile.get_height();
+}
+
+void Catalogue::add_track(TrackType &track)
+{
+       if(tracks.count(track.get_article_number()))
+               throw Exception("Duplicate track type");
+
+       tracks[track.get_article_number()] = &track;
+       signal_track_added.emit(track);
+}
+
+const TrackType &Catalogue::get_track(const ArticleNumber &art_nr) const
+{
+       TrackMap::const_iterator i=tracks.find(art_nr);
        if(i==tracks.end())
                throw KeyError("Unknown track type");
 
        return *i->second;
 }
 
-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");
 
-       return *i->second;
+       vehicles[veh.get_article_number()] = &veh;
+       signal_vehicle_added.emit(veh);
 }
 
-void Catalogue::load(const string &fn)
+const VehicleType &Catalogue::get_vehicle(const ArticleNumber &art_nr) const
 {
-       IO::File in(fn.c_str());
+       VehicleMap::const_iterator i = vehicles.find(art_nr);
+       if(i==vehicles.end())
+               throw KeyError("Unknown vehicle type");
 
-       DataFile::Parser parser(in, fn);
-       Loader loader(*this);
-       loader.load(parser);
+       return *i->second;
 }
 
 
 Catalogue::Loader::Loader(Catalogue &c):
-       cat(c)
+       DataFile::BasicLoader<Catalogue>(c)
+{
+       add("ballast_profile", &Loader::ballast_profile);
+       add("gauge", &Loader::gauge);
+       add("layout", &Loader::layout);
+       add("rail_profile", &Loader::rail_profile);
+       add("scale", &Loader::scale);
+       add("track", static_cast<void (Loader::*)(unsigned)>(&Loader::track));
+       add("track", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::track));
+       add("vehicle", static_cast<void (Loader::*)(unsigned)>(&Loader::vehicle));
+       add("vehicle", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::vehicle));
+}
+
+void Catalogue::Loader::ballast_profile()
+{
+       load_sub(obj.ballast_profile);
+}
+
+void Catalogue::Loader::gauge(float g)
 {
-       add("locomotive", &Loader::locomotive);
-       add("track", &Loader::track);
+       obj.gauge = g/1000;
+       obj.path_profile = Profile();
+       obj.path_profile.append_point(Point(0.1*obj.gauge, 0));
+       obj.path_profile.append_point(Point(-0.1*obj.gauge, 0));
 }
 
-void Catalogue::Loader::locomotive(unsigned art_no)
+void Catalogue::Loader::layout()
 {
-       map<unsigned, LocoType *>::iterator i=cat.locos.find(art_no);
-       if(i!=cat.locos.end())
-               throw Exception("Duplicate locomotive number");
+       load_sub(obj.layout);
+}
 
-       RefPtr<LocoType> loco=new LocoType(art_no);
-       load_sub(*loco);
-       unsigned art_nr=loco->get_article_number();
-       cat.locos[art_nr]=loco.release();
+void Catalogue::Loader::rail_profile()
+{
+       load_sub(obj.rail_profile);
 }
 
-void Catalogue::Loader::track(unsigned art_no)
+void Catalogue::Loader::scale(float n, float d)
 {
-       map<unsigned, TrackType *>::iterator i=cat.tracks.find(art_no);
-       if(i!=cat.tracks.end())
-               throw Exception("Duplicate track number");
+       obj.scale = n/d;
+}
 
-       RefPtr<TrackType> trk=new TrackType(art_no);
+void Catalogue::Loader::track(unsigned art_nr)
+{
+       track(ArticleNumber(art_nr));
+}
+
+void Catalogue::Loader::track(ArticleNumber art_nr)
+{
+       if(obj.tracks.count(art_nr))
+               throw KeyError("Duplicate track type", art_nr.str());
+
+       RefPtr<TrackType> trk = new TrackType(art_nr);
        load_sub(*trk);
-       unsigned art_nr=trk->get_article_number();
-       cat.tracks[art_nr]=trk.release();
+       obj.add_track(*trk.release());
+}
+
+void Catalogue::Loader::vehicle(unsigned art_nr)
+{
+       vehicle(ArticleNumber(art_nr));
+}
+
+void Catalogue::Loader::vehicle(ArticleNumber art_nr)
+{
+       if(obj.vehicles.count(art_nr))
+               throw KeyError("Duplicate vehicle type", art_nr.str());
+
+       RefPtr<VehicleType> veh = new VehicleType(art_nr);
+       load_sub(*veh);
+       obj.add_vehicle(*veh.release());
 }
 
 } // namespace Marklin