]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/catalogue.cpp
Foundations of using physics simulation for trains
[r2c2.git] / source / libmarklin / catalogue.cpp
index 3c8e279bcf354a957f05a418c2592538b3f97f67..bdd5369f9130d504d2a22182a6c1b920f111c892 100644 (file)
-#include <fstream>
-#include <msp/parser/parser.h>
+/* $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 "track.h"
+#include "locotype.h"
+#include "tracktype.h"
 
 using namespace std;
 using namespace Msp;
 
 namespace Marklin {
 
-Track *Catalogue::get_track(unsigned art_nr)
+Catalogue::Catalogue():
+       scale(1),
+       gauge(1.524),
+       layout(*this)
+{ }
+
+Catalogue::~Catalogue()
 {
-       TrackMap::const_iterator i=tracks.find(art_nr);
+       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)
+               delete i->second;
+}
 
-       if(i!=tracks.end())
-               return i->second;
+void Catalogue::add_track(TrackType &track)
+{
+       if(tracks.count(track.get_article_number()))
+               throw Exception("Duplicate track type");
 
-       return 0;
+       tracks[track.get_article_number()] = &track;
+       signal_track_added.emit(track);
 }
 
-void Catalogue::load(const string &fn)
+void Catalogue::add_locomotive(LocoType &loco)
 {
-       ifstream in(fn.c_str());
-       if(!in)
-               throw Exception("File not found");
+       if(locos.count(loco.get_article_number()))
+               throw Exception("Duplicate track type");
 
-       Parser::Parser parser(in, fn);
-       Loader loader(*this);
-       loader.load(parser);
+       locos[loco.get_article_number()] = &loco;
+       signal_loco_added.emit(loco);
 }
 
-Catalogue::~Catalogue()
+const TrackType &Catalogue::get_track(unsigned art_nr) const
 {
-       for(TrackMap::iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               delete i->second;
+       map<unsigned, TrackType *>::const_iterator i=tracks.find(art_nr);
+       if(i==tracks.end())
+               throw KeyError("Unknown track type");
+
+       return *i->second;
+}
+
+const LocoType &Catalogue::get_locomotive(unsigned art_nr) const
+{
+       map<unsigned, LocoType *>::const_iterator i=locos.find(art_nr);
+       if(i==locos.end())
+               throw KeyError("Unknown locomotive type");
+
+       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("locomotive", &Loader::locomotive);
+       add("rail_profile", &Loader::rail_profile);
+       add("scale", &Loader::scale);
        add("track", &Loader::track);
 }
 
-void Catalogue::Loader::track(unsigned art_no)
+void Catalogue::Loader::ballast_profile()
 {
-       TrackMap::iterator i=cat.tracks.find(art_no);
-       if(i!=cat.tracks.end())
-               throw Exception("Duplicate track number");
+       load_sub(obj.ballast_profile);
+}
 
-       Track *trk=new Track(art_no);
-       try
-       {
-               load_sub(*trk);
-       }
-       catch(const Msp::Exception &)
-       {
-               delete trk;
-               throw;
-       }
-       cat.tracks.insert(TrackMap::value_type(trk->get_article_number(), trk));
+void Catalogue::Loader::gauge(float g)
+{
+       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::layout()
+{
+       load_sub(obj.layout);
+}
+
+void Catalogue::Loader::locomotive(unsigned art_nr)
+{
+       RefPtr<LocoType> loco = new LocoType(art_nr);
+       load_sub(*loco);
+       obj.add_locomotive(*loco);
+       loco.release();
+}
+
+void Catalogue::Loader::rail_profile()
+{
+       load_sub(obj.rail_profile);
+}
+
+void Catalogue::Loader::scale(float n, float d)
+{
+       obj.scale = n/d;
+}
+
+void Catalogue::Loader::track(unsigned art_nr)
+{
+       RefPtr<TrackType> trk = new TrackType(art_nr);
+       load_sub(*trk);
+       obj.add_track(*trk);
+       trk.release();
 }
 
 } // namespace Marklin