]> git.tdb.fi Git - r2c2.git/commitdiff
Replace various map lookups with functions from maputils.h
authorMikko Rasa <tdb@tdb.fi>
Thu, 17 Jan 2013 10:45:45 +0000 (12:45 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 17 Jan 2013 10:45:45 +0000 (12:45 +0200)
source/3d/catalogue.cpp
source/3d/layout.cpp
source/designer/manipulator.cpp
source/libr2c2/catalogue.cpp
source/libr2c2/layout.cpp
source/libr2c2/route.cpp
source/libr2c2/simplecontroller.cpp
source/libr2c2/train.cpp
source/libr2c2/vehicletype.cpp
source/network/client.cpp

index f4e4fd04a4a4eb0b4736cb6ff3d355a7bb7a6dec..8d31219eb24dfabc1c718457be7db9fda7c5ac76 100644 (file)
@@ -42,20 +42,12 @@ Catalogue3D::~Catalogue3D()
 
 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)
index 266c921cfdf55f9e09414061c96322fa7b724189..7fd0d4ddac4b112073374fc25f6d4f96284f29c1 100644 (file)
@@ -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)
index 5f0354088f4641eed72f8b0edc751c60ffadce53..c103314564a69daead55d65c255382681819b3e1 100644 (file)
@@ -646,11 +646,7 @@ vector<Track *> Manipulator::create_straight(const R2C2::Vector &start, float di
                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);
 
index df59a0ee20ef7e73a5235762ced88a59e23dfd7a..847045634a1927ea142020a1a54c9a2d43360d72 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/maputils.h>
 #include <msp/core/refptr.h>
 #include <msp/datafile/parser.h>
 #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<TrackType> 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<VehicleType> veh = new VehicleType(art_nr);
        load_sub(*veh);
index df8ce74e62b01ba1d52a178469903992afb78a83..821482ae1da813c76cfeb32e64b2747dcac548fa 100644 (file)
@@ -1,4 +1,5 @@
 #include <algorithm>
+#include <msp/core/maputils.h>
 #include <msp/core/refptr.h>
 #include <msp/datafile/parser.h>
 #include <msp/datafile/writer.h>
@@ -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<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()
@@ -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<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)
index 58e50ddc7fb9fb3b39109f3a68dceb02fd2e7dd7..78f96a638f7bf28463127ef9f69ac81cecccdf38 100644 (file)
@@ -1,4 +1,5 @@
 #include <queue>
+#include <msp/core/maputils.h>
 #include <msp/strings/format.h>
 #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<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()
index b53b29163622c107be2cf0606c92ab790aa6ffdb..d88ade005929309e38d15f04714e7fa08278cad0 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/core/except.h>
+#include <msp/core/maputils.h>
 #include <msp/time/units.h>
 #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
index 782da37cc305935ff5b5186f0151f7dd432a5cef..5f1b46bd950f625fb04581565d7bc849c4a224ad 100644 (file)
@@ -1,5 +1,6 @@
 #include <algorithm>
 #include <cmath>
+#include <msp/core/maputils.h>
 #include <msp/strings/format.h>
 #include <msp/time/units.h>
 #include <msp/time/utils.h>
@@ -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;
        }
index c6c399243d9c40854192b0540f4784a4a7f802f6..9f8814652770448aa1ae1dc84f5b4d542f4e167a 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/maputils.h>
 #include "vehicletype.h"
 
 using namespace std;
@@ -223,10 +224,7 @@ VehicleType::Rod::Loader::Loader(Rod &r, const map<string, unsigned> &t):
 
 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);
 }
@@ -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<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)
index 7c583255b2b7b29f04b714c0590257f959c78afb..41cd28e900a712a5f5cc5693777bec615381e4f7 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/maputils.h>
 #include "client.h"
 
 using namespace std;
@@ -56,10 +57,7 @@ void Client::set_halt(bool h)
 
 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)