]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/train.cpp
Add basic support for signals
[r2c2.git] / source / libr2c2 / train.cpp
index 782da37cc305935ff5b5186f0151f7dd432a5cef..ab52ce6ad081bd31eb2b6cd919cc7b9c4e744994 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>
@@ -63,7 +64,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p):
        overshoot_dist(false)
 {
        if(!loco_type.is_locomotive())
-               throw InvalidParameterValue("Initial vehicle must be a locomotive");
+               throw invalid_argument("Train::Train");
 
        unsigned speed_steps = layout.get_driver().get_protocol_speed_steps(protocol);
        if(speed_steps)
@@ -125,9 +126,9 @@ void Train::add_vehicle(const VehicleType &vt)
 void Train::remove_vehicle(unsigned i)
 {
        if(i>=vehicles.size())
-               throw InvalidParameterValue("Vehicle index out of range");
+               throw out_of_range("Train::remove_vehicle");
        if(i==0)
-               throw InvalidParameterValue("Can't remove the locomotive");
+               throw logic_error("can't remove locomotive");
        delete vehicles[i];
        vehicles.erase(vehicles.begin()+i);
        if(i<vehicles.size())
@@ -142,14 +143,14 @@ unsigned Train::get_n_vehicles() const
 Vehicle &Train::get_vehicle(unsigned i)
 {
        if(i>=vehicles.size())
-               throw InvalidParameterValue("Vehicle index out of range");
+               throw out_of_range("Train::get_vehicle");
        return *vehicles[i];
 }
 
 const Vehicle &Train::get_vehicle(unsigned i) const
 {
        if(i>=vehicles.size())
-               throw InvalidParameterValue("Vehicle index out of range");
+               throw out_of_range("Train::get_vehicle");
        return *vehicles[i];
 }
 
@@ -163,7 +164,7 @@ void Train::set_active(bool a)
        if(a==active)
                return;
        if(!a && controller->get_speed())
-               throw InvalidState("Can't deactivate while moving");
+               throw logic_error("moving");
 
        active = a;
        if(active)
@@ -178,7 +179,7 @@ void Train::set_active(bool a)
 void Train::set_function(unsigned func, bool state)
 {
        if(!loco_type.get_functions().count(func))
-               throw InvalidParameterValue("Invalid function");
+               throw invalid_argument("Train::set_function");
        layout.get_driver().set_loco_function(address, func, state);
 }
 
@@ -320,7 +321,7 @@ bool Train::go_to(const Zone &to)
 bool Train::divert(Track &from)
 {
        if(!from.get_turnout_id())
-               throw InvalidParameterValue("Can't divert from a non-turnout");
+               throw invalid_argument("Train::divert");
        if(routes.empty())
                return false;
 
@@ -394,7 +395,7 @@ bool Train::divert(Track &from)
                if(end!=routes.end())
                        break;
                else if(!diversion->has_track(*track))
-                       throw LogicError("Pathfinder returned a bad route");
+                       throw logic_error("bad diversion");
 
                track = track.next(diversion->get_path(*track));
        }
@@ -422,7 +423,7 @@ const Route *Train::get_route() const
 void Train::place(Block &block, unsigned entry)
 {
        if(controller->get_speed())
-               throw InvalidState("Must be stopped before placing");
+               throw logic_error("moving");
 
        release_blocks();
 
@@ -448,7 +449,7 @@ void Train::place(Block &block, unsigned entry)
 void Train::unplace()
 {
        if(controller->get_speed())
-               throw InvalidState("Must be stopped before unplacing");
+               throw logic_error("moving");
 
        release_blocks();
 
@@ -541,7 +542,7 @@ void Train::free_noncritical_blocks()
        }
 }
 
-int Train::get_entry_to_block(Block &block) const
+int Train::get_entry_to_block(const Block &block) const
 {
        for(BlockList::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
                if(i->block()==&block)
@@ -976,7 +977,7 @@ void Train::reserve_more()
                        Train *other_train = block->get_train();
                        int other_entry = other_train->get_entry_to_block(*block);
                        if(other_entry<0)
-                               throw LogicError("Block reservation inconsistency");
+                               throw logic_error("block reservation inconsistency");
 
                        unsigned exit = block.reverse().entry();
                        unsigned other_exit = BlockIter(block.block(), other_entry).reverse().entry();
@@ -1288,7 +1289,7 @@ Train::RouteRef::RouteRef(const Route *r, unsigned d):
 
 
 Train::Loader::Loader(Train &t):
-       DataFile::BasicLoader<Train>(t),
+       DataFile::ObjectLoader<Train>(t),
        prev_block(0),
        blocks_valid(true)
 {
@@ -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;
        }