]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/catalogue.cpp
Add accessors adding things to a Catalogue from the outside
[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 "locotype.h"
12 #include "tracktype.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, LocoType *>::iterator i=locos.begin(); i!=locos.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 void Catalogue::add_locomotive(LocoType &loco)
43 {
44         if(locos.count(loco.get_article_number()))
45                 throw Exception("Duplicate track type");
46
47         locos[loco.get_article_number()] = &loco;
48         signal_loco_added.emit(loco);
49 }
50
51 const TrackType &Catalogue::get_track(unsigned art_nr) const
52 {
53         map<unsigned, TrackType *>::const_iterator i=tracks.find(art_nr);
54         if(i==tracks.end())
55                 throw KeyError("Unknown track type");
56
57         return *i->second;
58 }
59
60 const LocoType &Catalogue::get_locomotive(unsigned art_nr) const
61 {
62         map<unsigned, LocoType *>::const_iterator i=locos.find(art_nr);
63         if(i==locos.end())
64                 throw KeyError("Unknown locomotive 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("locomotive", &Loader::locomotive);
77         add("rail_profile", &Loader::rail_profile);
78         add("scale", &Loader::scale);
79         add("track", &Loader::track);
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 }
91
92 void Catalogue::Loader::layout()
93 {
94         load_sub(obj.layout);
95 }
96
97 void Catalogue::Loader::locomotive(unsigned art_nr)
98 {
99         RefPtr<LocoType> loco = new LocoType(art_nr);
100         load_sub(*loco);
101         obj.add_locomotive(*loco);
102         loco.release();
103 }
104
105 void Catalogue::Loader::rail_profile()
106 {
107         load_sub(obj.rail_profile);
108 }
109
110 void Catalogue::Loader::scale(float n, float d)
111 {
112         obj.scale = n/d;
113 }
114
115 void Catalogue::Loader::track(unsigned art_nr)
116 {
117         RefPtr<TrackType> trk = new TrackType(art_nr);
118         load_sub(*trk);
119         obj.add_track(*trk);
120         trk.release();
121 }
122
123 } // namespace Marklin