]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/train.cpp
Full vehicle unification
[r2c2.git] / source / libmarklin / train.cpp
index a6fd46d1e40519381c9723058b7252736a5f3b98..eadc37de3d460a136ed8e94f0ebd108e9d258a53 100644 (file)
@@ -13,20 +13,20 @@ Distributed under the GPL
 #include "catalogue.h"
 #include "driver.h"
 #include "layout.h"
-#include "locotype.h"
 #include "route.h"
 #include "simplephysics.h"
 #include "timetable.h"
 #include "tracktype.h"
 #include "train.h"
 #include "vehicle.h"
+#include "vehicletype.h"
 
 using namespace std;
 using namespace Msp;
 
 namespace Marklin {
 
-Train::Train(Layout &l, const LocoType &t, unsigned a):
+Train::Train(Layout &l, const VehicleType &t, unsigned a):
        layout(l),
        loco_type(t),
        address(a),
@@ -49,6 +49,9 @@ Train::Train(Layout &l, const LocoType &t, unsigned a):
        accurate_position(false),
        overshoot_dist(false)
 {
+       if(!loco_type.is_locomotive())
+               throw InvalidParameterValue("Initial vehicle must be a locomotive");
+
        vehicles.push_back(new Vehicle(layout, loco_type));
 
        layout.add_train(*this);
@@ -87,6 +90,30 @@ void Train::set_priority(int p)
        priority = p;
 }
 
+void Train::add_vehicle(const VehicleType &vt)
+{
+       Vehicle *veh = new Vehicle(layout, vt);
+       vehicles.back()->attach_back(*veh);
+       vehicles.push_back(veh);
+}
+
+void Train::remove_vehicle(unsigned i)
+{
+       if(i>=vehicles.size())
+               throw InvalidParameterValue("Vehicle index out of range");
+       if(i==0)
+               throw InvalidParameterValue("Can't remove the locomotive");
+       delete vehicles[i];
+       vehicles.erase(vehicles.begin()+i);
+       if(i<vehicles.size())
+               vehicles[i-1]->attach_back(*vehicles[i]);
+}
+
+unsigned Train::get_n_vehicles() const
+{
+       return vehicles.size();
+}
+
 Vehicle &Train::get_vehicle(unsigned i)
 {
        if(i>=vehicles.size())