]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/catalogue.cpp
55a5a440a3903efbff54e86b61dc8fa7382e654d
[r2c2.git] / source / libmarklin / catalogue.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009  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 {
21         for(map<unsigned, TrackType *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
22                 delete i->second;
23         for(map<unsigned, LocoType *>::iterator i=locos.begin(); i!=locos.end(); ++i)
24                 delete i->second;
25 }
26
27 TrackType &Catalogue::get_track(unsigned art_nr) const
28 {
29         map<unsigned, TrackType *>::const_iterator i=tracks.find(art_nr);
30         if(i==tracks.end())
31                 throw KeyError("Unknown track type");
32
33         return *i->second;
34 }
35
36 LocoType &Catalogue::get_locomotive(unsigned art_nr) const
37 {
38         map<unsigned, LocoType *>::const_iterator i=locos.find(art_nr);
39         if(i==locos.end())
40                 throw KeyError("Unknown locomotive type");
41
42         return *i->second;
43 }
44
45
46 Catalogue::Loader::Loader(Catalogue &c):
47         DataFile::BasicLoader<Catalogue>(c)
48 {
49         add("locomotive", &Loader::locomotive);
50         add("track", &Loader::track);
51 }
52
53 void Catalogue::Loader::locomotive(unsigned art_nr)
54 {
55         if(obj.locos.count(art_nr))
56                 throw Exception("Duplicate locomotive number");
57
58         RefPtr<LocoType> loco = new LocoType(art_nr);
59         load_sub(*loco);
60         obj.locos[art_nr] = loco.release();
61 }
62
63 void Catalogue::Loader::track(unsigned art_nr)
64 {
65         if(obj.tracks.count(art_nr))
66                 throw Exception("Duplicate track number");
67
68         RefPtr<TrackType> trk = new TrackType(art_nr);
69         load_sub(*trk);
70         obj.tracks[art_nr] = trk.release();
71 }
72
73 } // namespace Marklin