From ce2ff9a6e154f5b245cfb35114566489ab3e597c Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 27 Oct 2013 17:28:44 +0200 Subject: [PATCH] Remove things from the driver when no longer used --- source/libr2c2/centralstation.cpp | 28 ++++++++++++++++++++++++++++ source/libr2c2/centralstation.h | 5 +++++ source/libr2c2/driver.h | 4 ++++ source/libr2c2/dummy.h | 4 ++++ source/libr2c2/intellibox.cpp | 21 +++++++++++++++++++++ source/libr2c2/intellibox.h | 4 ++++ source/libr2c2/sensor.cpp | 9 +++++++-- source/libr2c2/signal.cpp | 11 ++++++++--- source/libr2c2/track.cpp | 10 ++++++++-- 9 files changed, 89 insertions(+), 7 deletions(-) diff --git a/source/libr2c2/centralstation.cpp b/source/libr2c2/centralstation.cpp index 82a0dfd..2138e10 100644 --- a/source/libr2c2/centralstation.cpp +++ b/source/libr2c2/centralstation.cpp @@ -111,6 +111,13 @@ void CentralStation::add_loco(unsigned addr, const string &proto_name, const Veh command(format("request(%d, view, control, force)", id)); } +void CentralStation::remove_loco(unsigned addr) +{ + unsigned id = map_address(locos, loco_addr, addr); + if(id) + command(format("release(%d, view, control)", id)); +} + void CentralStation::set_loco_speed(unsigned addr, unsigned speed) { if(speed && halted) @@ -175,6 +182,11 @@ void CentralStation::add_turnout(unsigned addr, const TrackType &type) turnout.bits = type.get_state_bits(); } +void CentralStation::remove_turnout(unsigned addr) +{ + remove_accessory(addr); +} + void CentralStation::set_turnout(unsigned addr, unsigned state) { set_accessory_state(addr, MagnetAccessory::TURNOUT, state); @@ -190,6 +202,11 @@ void CentralStation::add_signal(unsigned addr, const SignalType &) add_accessory(addr, MagnetAccessory::SIGNAL, MagnetAccessory::SEMAPHORE_HOME); } +void CentralStation::remove_signal(unsigned addr) +{ + remove_accessory(addr); +} + void CentralStation::set_signal(unsigned addr, unsigned state) { set_accessory_state(addr, MagnetAccessory::SIGNAL, state); @@ -230,6 +247,13 @@ CentralStation::MagnetAccessory &CentralStation::add_accessory(unsigned addr, Ma } } +void CentralStation::remove_accessory(unsigned addr) +{ + unsigned id = map_address(accessories, accessory_addr, addr); + if(id) + command(format("release(%d, view, control)", id)); +} + void CentralStation::set_accessory_state(unsigned addr, MagnetAccessory::Type type, unsigned state) { unsigned id = map_address(accessories, accessory_addr, addr); @@ -285,6 +309,10 @@ void CentralStation::add_sensor(unsigned addr) } } +void CentralStation::remove_sensor(unsigned) +{ +} + bool CentralStation::get_sensor(unsigned addr) const { SensorMap::const_iterator i = sensors.find(addr); diff --git a/source/libr2c2/centralstation.h b/source/libr2c2/centralstation.h index 6fe26f6..a34a3e3 100644 --- a/source/libr2c2/centralstation.h +++ b/source/libr2c2/centralstation.h @@ -138,26 +138,31 @@ public: virtual unsigned get_protocol_speed_steps(const std::string &) const; virtual void add_loco(unsigned, const std::string &, const VehicleType &); + virtual void remove_loco(unsigned); virtual void set_loco_speed(unsigned, unsigned); virtual void set_loco_reverse(unsigned, bool); virtual void set_loco_function(unsigned, unsigned, bool); virtual void add_turnout(unsigned, const TrackType &); + virtual void remove_turnout(unsigned); virtual void set_turnout(unsigned, unsigned); virtual unsigned get_turnout(unsigned) const; virtual void add_signal(unsigned, const SignalType &); + virtual void remove_signal(unsigned); virtual void set_signal(unsigned, unsigned); virtual unsigned get_signal(unsigned) const; private: MagnetAccessory &add_accessory(unsigned, MagnetAccessory::Type, MagnetAccessory::Symbol); + void remove_accessory(unsigned); void set_accessory_state(unsigned, MagnetAccessory::Type, unsigned); unsigned get_accessory_state(unsigned, MagnetAccessory::Type) const; void accessory_state_changed(const MagnetAccessory &) const; public: virtual void add_sensor(unsigned); + virtual void remove_sensor(unsigned); virtual void set_sensor(unsigned, bool) { } virtual bool get_sensor(unsigned) const; diff --git a/source/libr2c2/driver.h b/source/libr2c2/driver.h index dc143b0..9648c92 100644 --- a/source/libr2c2/driver.h +++ b/source/libr2c2/driver.h @@ -34,19 +34,23 @@ public: virtual const char *enumerate_protocols(unsigned) const = 0; virtual unsigned get_protocol_speed_steps(const std::string &) const = 0; virtual void add_loco(unsigned, const std::string &, const VehicleType &) = 0; + virtual void remove_loco(unsigned) = 0; virtual void set_loco_speed(unsigned, unsigned) = 0; virtual void set_loco_reverse(unsigned, bool) = 0; virtual void set_loco_function(unsigned, unsigned, bool) = 0; virtual void add_turnout(unsigned, const TrackType &) = 0; + virtual void remove_turnout(unsigned) = 0; virtual void set_turnout(unsigned, unsigned) = 0; virtual unsigned get_turnout(unsigned) const = 0; virtual void add_signal(unsigned, const SignalType &) = 0; + virtual void remove_signal(unsigned) = 0; virtual void set_signal(unsigned, unsigned) = 0; virtual unsigned get_signal(unsigned) const = 0; virtual void add_sensor(unsigned) = 0; + virtual void remove_sensor(unsigned) = 0; virtual void set_sensor(unsigned, bool) = 0; virtual bool get_sensor(unsigned) const = 0; diff --git a/source/libr2c2/dummy.h b/source/libr2c2/dummy.h index 2c6f992..933fce6 100644 --- a/source/libr2c2/dummy.h +++ b/source/libr2c2/dummy.h @@ -41,19 +41,23 @@ public: virtual const char *enumerate_protocols(unsigned) const; virtual unsigned get_protocol_speed_steps(const std::string &) const; virtual void add_loco(unsigned, const std::string &, const VehicleType &) { } + virtual void remove_loco(unsigned) { } virtual void set_loco_speed(unsigned, unsigned); virtual void set_loco_reverse(unsigned, bool); virtual void set_loco_function(unsigned, unsigned, bool); virtual void add_turnout(unsigned, const TrackType &); + virtual void remove_turnout(unsigned) { } virtual void set_turnout(unsigned, unsigned); virtual unsigned get_turnout(unsigned) const; virtual void add_signal(unsigned, const SignalType &) { } + virtual void remove_signal(unsigned) { } virtual void set_signal(unsigned, unsigned) { } virtual unsigned get_signal(unsigned) const { return 0; } virtual void add_sensor(unsigned) { } + virtual void remove_sensor(unsigned) { } virtual void set_sensor(unsigned, bool); virtual bool get_sensor(unsigned) const; diff --git a/source/libr2c2/intellibox.cpp b/source/libr2c2/intellibox.cpp index d4129ec..338aed2 100644 --- a/source/libr2c2/intellibox.cpp +++ b/source/libr2c2/intellibox.cpp @@ -114,6 +114,11 @@ void Intellibox::add_loco(unsigned addr, const string &proto_name, const Vehicle } } +void Intellibox::remove_loco(unsigned addr) +{ + locos.erase(addr); +} + void Intellibox::set_loco_speed(unsigned addr, unsigned speed) { Locomotive &loco = locos[addr]; @@ -197,6 +202,11 @@ void Intellibox::add_turnout(unsigned addr, const TrackType &type) add_turnout(addr, type.get_state_bits(), false); } +void Intellibox::remove_turnout(unsigned addr) +{ + turnouts.erase(addr); +} + void Intellibox::add_turnout(unsigned addr, unsigned bits, bool signal) { if(!turnouts.count(addr)) @@ -264,6 +274,11 @@ void Intellibox::add_signal(unsigned addr, const SignalType &) add_turnout(addr, 1, true); } +void Intellibox::remove_signal(unsigned addr) +{ + remove_turnout(addr); +} + void Intellibox::set_signal(unsigned addr, unsigned state) { set_turnout(addr, state); @@ -283,6 +298,12 @@ void Intellibox::add_sensor(unsigned addr) } } +void Intellibox::remove_sensor(unsigned addr) +{ + sensors.erase(addr); + update_sensors = true; +} + bool Intellibox::get_sensor(unsigned addr) const { map::const_iterator i = sensors.find(addr); diff --git a/source/libr2c2/intellibox.h b/source/libr2c2/intellibox.h index 4740a1e..46c769b 100644 --- a/source/libr2c2/intellibox.h +++ b/source/libr2c2/intellibox.h @@ -137,11 +137,13 @@ public: virtual const char *enumerate_protocols(unsigned) const; virtual unsigned get_protocol_speed_steps(const std::string &) const; virtual void add_loco(unsigned, const std::string &, const VehicleType &); + virtual void remove_loco(unsigned); virtual void set_loco_speed(unsigned, unsigned); virtual void set_loco_reverse(unsigned, bool); virtual void set_loco_function(unsigned, unsigned, bool); virtual void add_turnout(unsigned, const TrackType &); + virtual void remove_turnout(unsigned); private: void add_turnout(unsigned, unsigned, bool); void turnout_state_changed(unsigned, const Turnout &) const; @@ -150,11 +152,13 @@ public: virtual unsigned get_turnout(unsigned) const; virtual void add_signal(unsigned, const SignalType &); + virtual void remove_signal(unsigned); virtual void set_signal(unsigned, unsigned); virtual unsigned get_signal(unsigned) const; public: virtual void add_sensor(unsigned); + virtual void remove_sensor(unsigned); virtual void set_sensor(unsigned, bool) { } virtual bool get_sensor(unsigned) const; diff --git a/source/libr2c2/sensor.cpp b/source/libr2c2/sensor.cpp index c31c795..618a06b 100644 --- a/source/libr2c2/sensor.cpp +++ b/source/libr2c2/sensor.cpp @@ -20,14 +20,19 @@ Sensor::Sensor(Layout &l): Sensor::~Sensor() { + if(layout.has_driver() && address) + layout.get_driver().remove_sensor(address); layout.remove(*this); } void Sensor::set_address(unsigned a) { + Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0); + if(driver && address) + driver->remove_sensor(address); address = a; - if(layout.has_driver()) - layout.get_driver().add_sensor(address); + if(driver && address) + driver->add_sensor(address); } void Sensor::tick(const Time::TimeDelta &dt) diff --git a/source/libr2c2/signal.cpp b/source/libr2c2/signal.cpp index 96c44d8..73563b6 100644 --- a/source/libr2c2/signal.cpp +++ b/source/libr2c2/signal.cpp @@ -28,6 +28,8 @@ Signal::Signal(Layout &l, const SignalType &t): Signal::~Signal() { + if(layout.has_driver() && address) + layout.get_driver().remove_sensor(address); layout.remove(*this); } @@ -41,10 +43,13 @@ Signal *Signal::clone(Layout *to_layout) const void Signal::set_address(unsigned a) { + Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0); + + if(driver && address) + driver->remove_signal(address); address = a; - - if(layout.has_driver() && address) - layout.get_driver().add_signal(address, type); + if(driver && address) + driver->add_signal(address, type); } void Signal::set_position(const Vector &p) diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index 7a17467..5da34f6 100644 --- a/source/libr2c2/track.cpp +++ b/source/libr2c2/track.cpp @@ -53,6 +53,8 @@ Track::Track(Layout &l, const TrackType &t): Track::~Track() { break_links(); + if(layout.has_driver() && turnout_id) + layout.get_driver().remove_turnout(turnout_id); layout.remove(*this); } @@ -155,11 +157,15 @@ void Track::set_turnout_id(unsigned i) if(!i) throw invalid_argument("Track::set_turnout_id"); + Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0); + + if(driver && turnout_id) + driver->remove_turnout(turnout_id); turnout_id = i; layout.create_blocks(*this); layout.update_routes(); - if(layout.has_driver() && turnout_id) - layout.get_driver().add_turnout(turnout_id, type); + if(driver && turnout_id) + driver->add_turnout(turnout_id, type); } void Track::set_sensor_id(unsigned i) -- 2.45.2