From 4236c30c0a7a48854e9634e9ad7d8d6e0fd243a3 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 17 Jan 2013 12:45:45 +0200 Subject: [PATCH] Replace various map lookups with functions from maputils.h --- source/3d/catalogue.cpp | 12 ++---------- source/3d/layout.cpp | 22 ++++------------------ source/designer/manipulator.cpp | 6 +----- source/libr2c2/catalogue.cpp | 27 +++++++-------------------- source/libr2c2/layout.cpp | 17 ++++++----------- source/libr2c2/route.cpp | 9 ++++----- source/libr2c2/simplecontroller.cpp | 3 ++- source/libr2c2/train.cpp | 5 +++-- source/libr2c2/vehicletype.cpp | 11 +++-------- source/network/client.cpp | 6 ++---- 10 files changed, 34 insertions(+), 84 deletions(-) diff --git a/source/3d/catalogue.cpp b/source/3d/catalogue.cpp index f4e4fd0..8d31219 100644 --- a/source/3d/catalogue.cpp +++ b/source/3d/catalogue.cpp @@ -42,20 +42,12 @@ Catalogue3D::~Catalogue3D() const TrackType3D &Catalogue3D::get_track(const TrackType &tt) const { - map::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_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) diff --git a/source/3d/layout.cpp b/source/3d/layout.cpp index 266c921..7fd0d4d 100644 --- a/source/3d/layout.cpp +++ b/source/3d/layout.cpp @@ -51,10 +51,7 @@ void Layout3D::get_bounds(Vector &minp, Vector &maxp) const 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) @@ -64,19 +61,12 @@ 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) @@ -86,11 +76,7 @@ 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) diff --git a/source/designer/manipulator.cpp b/source/designer/manipulator.cpp index 5f03540..c103314 100644 --- a/source/designer/manipulator.cpp +++ b/source/designer/manipulator.cpp @@ -646,11 +646,7 @@ vector Manipulator::create_straight(const R2C2::Vector &start, float di float s = sin(dir); for(vector::iterator i=lengths.begin(); i!=lengths.end(); ++i) { - map::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); diff --git a/source/libr2c2/catalogue.cpp b/source/libr2c2/catalogue.cpp index df59a0e..8470456 100644 --- a/source/libr2c2/catalogue.cpp +++ b/source/libr2c2/catalogue.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "catalogue.h" @@ -30,38 +31,24 @@ float Catalogue::get_rail_elevation() const 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); } @@ -116,7 +103,7 @@ void Catalogue::Loader::track(unsigned 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 trk = new TrackType(art_nr); load_sub(*trk); @@ -131,7 +118,7 @@ void Catalogue::Loader::vehicle(unsigned art_nr) 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 veh = new VehicleType(art_nr); load_sub(*veh); diff --git a/source/libr2c2/layout.cpp b/source/libr2c2/layout.cpp index df8ce74..821482a 100644 --- a/source/libr2c2/layout.cpp +++ b/source/libr2c2/layout.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -119,7 +120,7 @@ Block &Layout::get_block(unsigned id) const if((*i)->get_id()==id) return **i; - throw KeyError("Unknown block", lexical_cast(id)); + throw key_error(id); } void Layout::create_blocks() @@ -179,7 +180,7 @@ Route &Layout::get_route(const string &name) const for(set::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() @@ -218,7 +219,7 @@ Zone &Layout::get_zone(const string &group, unsigned num) const 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) @@ -229,19 +230,13 @@ 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::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) diff --git a/source/libr2c2/route.cpp b/source/libr2c2/route.cpp index 58e50dd..78f96a6 100644 --- a/source/libr2c2/route.cpp +++ b/source/libr2c2/route.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "layout.h" #include "route.h" @@ -154,12 +155,10 @@ void Route::set_turnout(unsigned addr, unsigned path) { if(!addr) throw InvalidParameterValue("Invalid turnout address"); - map::iterator i = turnouts.find(addr); - if(i==turnouts.end()) - throw KeyError("Turnout is not in this route"); - if(i->second>=0 && path!=static_cast(i->second)) + int &state = get_item(turnouts, addr); + if(state>=0 && path!=static_cast(state)) throw InvalidState("Setting conflicts with route"); - i->second = path; + state = path; } void Route::update_turnouts() diff --git a/source/libr2c2/simplecontroller.cpp b/source/libr2c2/simplecontroller.cpp index b53b291..d88ade0 100644 --- a/source/libr2c2/simplecontroller.cpp +++ b/source/libr2c2/simplecontroller.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "simplecontroller.h" @@ -49,7 +50,7 @@ const Controller::Control &SimpleController::get_control(const string &name) con else if(name==reverse.name) return reverse; else - throw KeyError("Unknown control", name); + throw key_error(name); } float SimpleController::get_braking_distance() const diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index 782da37..5f1b46b 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -1322,7 +1323,7 @@ void Train::Loader::block(unsigned id) { blk = &obj.layout.get_block(id); } - catch(const KeyError &) + catch(const key_error &) { blocks_valid = false; return; @@ -1349,7 +1350,7 @@ void Train::Loader::block_hint(unsigned id) { prev_block = &obj.layout.get_block(id); } - catch(const KeyError &) + catch(const key_error &) { blocks_valid = false; } diff --git a/source/libr2c2/vehicletype.cpp b/source/libr2c2/vehicletype.cpp index c6c3992..9f88146 100644 --- a/source/libr2c2/vehicletype.cpp +++ b/source/libr2c2/vehicletype.cpp @@ -1,3 +1,4 @@ +#include #include "vehicletype.h" using namespace std; @@ -223,10 +224,7 @@ VehicleType::Rod::Loader::Loader(Rod &r, const map &t): void VehicleType::Rod::Loader::connect(const string &t, float px, float pz, float ox, float oz) { - map::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); } @@ -251,11 +249,8 @@ void VehicleType::Rod::Loader::pivot_bogie_axle(unsigned i, unsigned j) void VehicleType::Rod::Loader::pivot_rod(const string &t) { - map::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) diff --git a/source/network/client.cpp b/source/network/client.cpp index 7c58325..41cd28e 100644 --- a/source/network/client.cpp +++ b/source/network/client.cpp @@ -1,3 +1,4 @@ +#include #include "client.h" using namespace std; @@ -56,10 +57,7 @@ void Client::set_halt(bool h) NetTrain &Client::get_train(unsigned addr) const { - map::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) -- 2.45.2