]> git.tdb.fi Git - r2c2.git/commitdiff
Add a virtual get_block function to Sensor
authorMikko Rasa <tdb@tdb.fi>
Sun, 7 Jul 2013 21:20:00 +0000 (00:20 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 7 Jul 2013 21:20:00 +0000 (00:20 +0300)
source/libr2c2/blockallocator.cpp
source/libr2c2/layout.cpp
source/libr2c2/sensor.h
source/libr2c2/trackcircuit.h
source/libr2c2/train.cpp

index 022a8df230ee72e8e2185e36624ac13f5928b64b..fc2705cd58d81bc5e5ec8060b4f03653d753f6b0 100644 (file)
@@ -343,13 +343,8 @@ void BlockAllocator::block_reserved(Block &block, const Train *tr)
 
 void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
 {
-       Block *block = 0;
-       if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
-               block = &tc->get_block();
-       else
-               return;
-
-       if(block->get_train()!=&train)
+       Block *block = sensor.get_block();
+       if(!block || block->get_train()!=&train)
                return;
 
        if(state==Sensor::MAYBE_ACTIVE)
index 40863237081c9677a6451eadf29de9584f51bf80..6cb0eb3634ae334cca8714b87f5d6fb7ae5251bd 100644 (file)
@@ -435,10 +435,7 @@ void Layout::sensor_state_changed(Sensor &sensor, Sensor::State state)
 {
        if(state==Sensor::ACTIVE)
        {
-               Block *block = 0;
-               if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
-                       block = &tc->get_block();
-
+               Block *block = sensor.get_block();
                if(block && !block->get_train())
                        emergency(format("Unreserved sensor %d triggered", sensor.get_address()));
        }
index 849f899a9687d1be8d3e6a3f12992753917e8c08..d3f472dbeef11370690360a20dd4d24be56d8a7b 100644 (file)
@@ -6,6 +6,7 @@
 
 namespace R2C2 {
 
+class Block;
 class Layout;
 
 class Sensor: public sigc::trackable
@@ -33,6 +34,7 @@ public:
 
        unsigned get_address() const { return address; }
        State get_state() const { return state; }
+       virtual Block *get_block() const { return 0; }
 
        void tick(const Msp::Time::TimeDelta &);
 
index 64e7462788f0da2ab43adfa1d8fb86098524ceda..19c11c7e40991f293dc145d40c8a1a5494f3d77c 100644 (file)
@@ -15,7 +15,7 @@ private:
 public:
        TrackCircuit(Layout &, Block &);
 
-       Block &get_block() const { return block; }
+       virtual Block *get_block() const { return &block; }
 };
 
 } // namespace R2C2
index 25dcae97da9580cea1946315674fb9bd36c5c3ed..b2a49c722e7732938cfce5ed3cdefa95b5838d9b 100644 (file)
@@ -422,13 +422,9 @@ void Train::loco_func_event(unsigned addr, unsigned func, bool state)
 
 void Train::sensor_state_changed(Sensor &sensor, Sensor::State state)
 {
-       Block *block = 0;
-       if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
-               block = &tc->get_block();
-       else
-               return;
+       Block *block = sensor.get_block();
 
-       if(block->get_train()==this && state==Sensor::MAYBE_ACTIVE)
+       if(block && block->get_train()==this && state==Sensor::MAYBE_ACTIVE)
        {
                if(last_entry_block)
                {