]> git.tdb.fi Git - r2c2.git/commitdiff
Remove things from the driver when no longer used
authorMikko Rasa <tdb@tdb.fi>
Sun, 27 Oct 2013 15:28:44 +0000 (17:28 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 27 Oct 2013 17:10:27 +0000 (19:10 +0200)
source/libr2c2/centralstation.cpp
source/libr2c2/centralstation.h
source/libr2c2/driver.h
source/libr2c2/dummy.h
source/libr2c2/intellibox.cpp
source/libr2c2/intellibox.h
source/libr2c2/sensor.cpp
source/libr2c2/signal.cpp
source/libr2c2/track.cpp

index 82a0dfdb16878ae308f0b18d5ea6fd52a67a78e4..2138e10702ee27497e70a641edc8fbd12d9d20ab 100644 (file)
@@ -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);
index 6fe26f61e6b94802c5f74eb2501430d02552c805..a34a3e361aeaffe8af82e36cf7e123346c5edad4 100644 (file)
@@ -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;
 
index dc143b03132486d82702ccc088394859b6336eca..9648c921354c4d8fbd4c85c494eb3e3bd0b9f474 100644 (file)
@@ -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;
 
index 2c6f992c64d05ee5ca7cba8456093abdebe6d0db..933fce61705a6355fc6a2fb2e1f7f25bf482886d 100644 (file)
@@ -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;
 
index d4129ec7535162d44deb3225de1608423d76c759..338aed2544127844dff4de200cf7bbffc90f2a2a 100644 (file)
@@ -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<unsigned, Sensor>::const_iterator i = sensors.find(addr);
index 4740a1ec6d6f2ccb611a5b5d2b4913eaaca5c37e..46c769b925053e60553596841663816c371d7144 100644 (file)
@@ -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;
 
index c31c795f12986e32fbd4f23e95c885e03d1aff2f..618a06b7b609c6ac8a930bc62c197bb4ac2db768 100644 (file)
@@ -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)
index 96c44d831d225cec9c06613c2500e779a8495558..73563b6e815d1d6b7d3c8fd6ca957a4120ca8cca 100644 (file)
@@ -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)
index 7a1746716574abdbedb40c0356042ae7932dba66..5da34f692b370c9f5711536e2d0838da78e9f8cb 100644 (file)
@@ -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)