const TrackType3D &Catalogue3D::get_track(const TrackType &tt) const
{
- map<const TrackType *, TrackType3D *>::const_iterator i = tracks.find(&tt);
- if(i==tracks.end())
- throw KeyError("Unknown track type");
-
- return *i->second;
+ return *get_item(tracks, &tt);
}
const VehicleType3D &Catalogue3D::get_vehicle(const VehicleType &vt) const
{
- map<const VehicleType *, VehicleType3D *>::const_iterator i = vehicles.find(&vt);
- if(i==vehicles.end())
- throw KeyError("Unknown vehicle type");
-
- return *i->second;
+ return *get_item(vehicles, &vt);
}
void Catalogue3D::track_added(const TrackType &track)
void Layout3D::add_track(Track3D &t)
{
- if(tracks.count(&t.get_track()))
- throw KeyError("Duplicate track");
-
- tracks[&t.get_track()] = &t;
+ insert_unique(tracks, &t.get_track(), &t);
}
void Layout3D::remove_track(Track3D &t)
Track3D &Layout3D::get_track(Track &t) const
{
- TrackMap::const_iterator i = tracks.find(&t);
- if(i==tracks.end())
- throw KeyError("Unknown track");
-
- return *i->second;
+ return *get_item(tracks, &t);
}
void Layout3D::add_vehicle(Vehicle3D &v)
{
- if(vehicles.count(&v.get_vehicle()))
- throw KeyError("Duplicate vehicle");
-
- vehicles[&v.get_vehicle()] = &v;
+ insert_unique(vehicles, &v.get_vehicle(), &v);
}
void Layout3D::remove_vehicle(Vehicle3D &v)
Vehicle3D &Layout3D::get_vehicle(Vehicle &v) const
{
- VehicleMap::const_iterator i = vehicles.find(&v);
- if(i==vehicles.end())
- throw KeyError("Unknown vehicle");
-
- return *i->second;
+ return *get_item(vehicles, &v);
}
void Layout3D::track_added(Track &t)
float s = sin(dir);
for(vector<float>::iterator i=lengths.begin(); i!=lengths.end(); ++i)
{
- map<float, const TrackType *>::iterator j = types_by_length.find(*i);
- if(j==types_by_length.end())
- throw LogicError("Internal error");
-
- Track *track = new Track(designer.get_layout(), *j->second);
+ Track *track = new Track(designer.get_layout(), *get_item(types_by_length, *i));
track->set_position(pos);
track->set_rotation(dir);
+#include <msp/core/maputils.h>
#include <msp/core/refptr.h>
#include <msp/datafile/parser.h>
#include "catalogue.h"
void Catalogue::add_track(TrackType &track)
{
- if(tracks.count(track.get_article_number()))
- throw Exception("Duplicate track type");
-
- tracks[track.get_article_number()] = &track;
+ insert_unique(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;
+ return *get_item(tracks, art_nr);
}
void Catalogue::add_vehicle(VehicleType &veh)
{
- if(vehicles.count(veh.get_article_number()))
- throw Exception("Duplicate vehicle type");
-
- vehicles[veh.get_article_number()] = &veh;
+ insert_unique(vehicles, veh.get_article_number(), &veh);
signal_vehicle_added.emit(veh);
}
const VehicleType &Catalogue::get_vehicle(const ArticleNumber &art_nr) const
{
- VehicleMap::const_iterator i = vehicles.find(art_nr);
- if(i==vehicles.end())
- throw KeyError("Unknown vehicle type");
-
- return *i->second;
+ return *get_item(vehicles, art_nr);
}
void Catalogue::Loader::track(ArticleNumber art_nr)
{
if(obj.tracks.count(art_nr))
- throw KeyError("Duplicate track type", art_nr.str());
+ throw key_error(art_nr);
RefPtr<TrackType> trk = new TrackType(art_nr);
load_sub(*trk);
void Catalogue::Loader::vehicle(ArticleNumber art_nr)
{
if(obj.vehicles.count(art_nr))
- throw KeyError("Duplicate vehicle type", art_nr.str());
+ throw key_error(art_nr);
RefPtr<VehicleType> veh = new VehicleType(art_nr);
load_sub(*veh);
#include <algorithm>
+#include <msp/core/maputils.h>
#include <msp/core/refptr.h>
#include <msp/datafile/parser.h>
#include <msp/datafile/writer.h>
if((*i)->get_id()==id)
return **i;
- throw KeyError("Unknown block", lexical_cast(id));
+ throw key_error(id);
}
void Layout::create_blocks()
for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
if((*i)->get_name()==name)
return **i;
- throw KeyError("Unknown route", name);
+ throw key_error(name);
}
void Layout::update_routes()
if((*i)->get_group()==group && (*i)->get_number()==num)
return **i;
- throw KeyError("Unknown zone", format("%s %d", group, num));
+ throw key_error(format("%s %d", group, num));
}
void Layout::remove_zone(Zone &z)
void Layout::add_train(Train &t)
{
- if(trains.count(t.get_address()))
- throw KeyError("Duplicate train address", lexical_cast(t.get_address()));
-
- trains[t.get_address()] = &t;
+ insert_unique(trains, t.get_address(), &t);
signal_train_added.emit(t);
}
Train &Layout::get_train(unsigned addr) const
{
- map<unsigned, Train *>::const_iterator i = trains.find(addr);
- if(i==trains.end())
- throw KeyError("Unknown train", lexical_cast(addr));
- return *i->second;
+ return *get_item(trains, addr);
}
void Layout::remove_train(Train &t)
#include <queue>
+#include <msp/core/maputils.h>
#include <msp/strings/format.h>
#include "layout.h"
#include "route.h"
{
if(!addr)
throw InvalidParameterValue("Invalid turnout address");
- map<unsigned, int>::iterator i = turnouts.find(addr);
- if(i==turnouts.end())
- throw KeyError("Turnout is not in this route");
- if(i->second>=0 && path!=static_cast<unsigned>(i->second))
+ int &state = get_item(turnouts, addr);
+ if(state>=0 && path!=static_cast<unsigned>(state))
throw InvalidState("Setting conflicts with route");
- i->second = path;
+ state = path;
}
void Route::update_turnouts()
#include <msp/core/except.h>
+#include <msp/core/maputils.h>
#include <msp/time/units.h>
#include "simplecontroller.h"
else if(name==reverse.name)
return reverse;
else
- throw KeyError("Unknown control", name);
+ throw key_error(name);
}
float SimpleController::get_braking_distance() const
#include <algorithm>
#include <cmath>
+#include <msp/core/maputils.h>
#include <msp/strings/format.h>
#include <msp/time/units.h>
#include <msp/time/utils.h>
{
blk = &obj.layout.get_block(id);
}
- catch(const KeyError &)
+ catch(const key_error &)
{
blocks_valid = false;
return;
{
prev_block = &obj.layout.get_block(id);
}
- catch(const KeyError &)
+ catch(const key_error &)
{
blocks_valid = false;
}
+#include <msp/core/maputils.h>
#include "vehicletype.h"
using namespace std;
void VehicleType::Rod::Loader::connect(const string &t, float px, float pz, float ox, float oz)
{
- map<string, unsigned>::const_iterator i = tags.find(t);
- if(i==tags.end())
- throw KeyError("Unknown rod tag", t);
- obj.connect_index = i->second;
+ obj.connect_index = get_item(tags, t);
obj.connect_point = Vector(px/1000, 0, pz/1000);
obj.connect_offset = Vector(ox/1000, 0, oz/1000);
}
void VehicleType::Rod::Loader::pivot_rod(const string &t)
{
- map<string, unsigned>::const_iterator i = tags.find(t);
- if(i==tags.end())
- throw KeyError("Unknown rod tag", t);
+ obj.pivot_index = get_item(tags, t);
obj.pivot = ROD;
- obj.pivot_index = i->second;
}
void VehicleType::Rod::Loader::position(float x, float y, float z)
+#include <msp/core/maputils.h>
#include "client.h"
using namespace std;
NetTrain &Client::get_train(unsigned addr) const
{
- map<unsigned, NetTrain *>::const_iterator i = trains.find(addr);
- if(i==trains.end())
- throw KeyError("Unknown train");
- return *i->second;
+ return *get_item(trains, addr);
}
void Client::receive(const DriverStatePacket &pkt)