]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/catalogue.cpp
221a6f0663667cca1665e4388d867e422c16fb5e
[r2c2.git] / source / libr2c2 / catalogue.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/core/refptr.h>
3 #include <msp/datafile/parser.h>
4 #include "catalogue.h"
5 #include "signaltype.h"
6 #include "tracktype.h"
7 #include "vehicletype.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 namespace R2C2 {
13
14 Catalogue::Catalogue():
15         scale(1),
16         gauge(1.524),
17         layout(*this)
18 { }
19
20 Catalogue::~Catalogue()
21 {
22         for(ObjectMap::iterator i=objects.begin(); i!=objects.end(); ++i)
23                 delete i->second;
24 }
25
26 float Catalogue::get_rail_elevation() const
27 {
28         return ballast_profile.get_height()+rail_profile.get_height();
29 }
30
31 void Catalogue::add(ObjectType &object)
32 {
33         insert_unique(objects, object.get_article_number(), &object);
34         signal_object_added.emit(object);
35 }
36
37 const ObjectType &Catalogue::get(const ArticleNumber &art_nr) const
38 {
39         return *get_item(objects, art_nr);
40 }
41
42
43 Catalogue::Loader::Loader(Catalogue &c):
44         DataFile::ObjectLoader<Catalogue>(c)
45 {
46         add("ballast_profile", &Loader::ballast_profile);
47         add("gauge", &Loader::gauge);
48         add("layout", &Loader::layout);
49         add("rail_profile", &Loader::rail_profile);
50         add("scale", &Loader::scale);
51         add("signal", &Loader::signal);
52         add("track", &Loader::track);
53         add("track_technique", &Catalogue::track_technique);
54         add("vehicle", &Loader::vehicle);
55 }
56
57 void Catalogue::Loader::ballast_profile()
58 {
59         load_sub(obj.ballast_profile);
60 }
61
62 void Catalogue::Loader::gauge(float g)
63 {
64         obj.gauge = g/1000;
65 }
66
67 void Catalogue::Loader::layout()
68 {
69         load_sub(obj.layout);
70 }
71
72 void Catalogue::Loader::rail_profile()
73 {
74         load_sub(obj.rail_profile);
75 }
76
77 void Catalogue::Loader::scale(float n, float d)
78 {
79         obj.scale = n/d;
80 }
81
82 void Catalogue::Loader::signal(ArticleNumber art_nr)
83 {
84         if(obj.objects.count(art_nr))
85                 throw key_error(art_nr);
86
87         RefPtr<SignalType> sig = new SignalType(art_nr);
88         load_sub(*sig);
89         obj.add(*sig.release());
90 }
91
92 void Catalogue::Loader::track(ArticleNumber art_nr)
93 {
94         if(obj.objects.count(art_nr))
95                 throw key_error(art_nr);
96
97         RefPtr<TrackType> trk = new TrackType(art_nr);
98         load_sub(*trk);
99         obj.add(*trk.release());
100 }
101
102 void Catalogue::Loader::vehicle(ArticleNumber art_nr)
103 {
104         if(obj.objects.count(art_nr))
105                 throw key_error(art_nr);
106
107         RefPtr<VehicleType> veh = new VehicleType(art_nr);
108         load_sub(*veh);
109         obj.add(*veh.release());
110 }
111
112 } // namespace R2C2