From: Mikko Rasa Date: Sun, 7 Jul 2013 21:20:00 +0000 (+0300) Subject: Add a virtual get_block function to Sensor X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f730acd5fa2b4f0d0185770ff7950ab814aec47c;p=r2c2.git Add a virtual get_block function to Sensor --- diff --git a/source/libr2c2/blockallocator.cpp b/source/libr2c2/blockallocator.cpp index 022a8df..fc2705c 100644 --- a/source/libr2c2/blockallocator.cpp +++ b/source/libr2c2/blockallocator.cpp @@ -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(&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) diff --git a/source/libr2c2/layout.cpp b/source/libr2c2/layout.cpp index 4086323..6cb0eb3 100644 --- a/source/libr2c2/layout.cpp +++ b/source/libr2c2/layout.cpp @@ -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(&sensor)) - block = &tc->get_block(); - + Block *block = sensor.get_block(); if(block && !block->get_train()) emergency(format("Unreserved sensor %d triggered", sensor.get_address())); } diff --git a/source/libr2c2/sensor.h b/source/libr2c2/sensor.h index 849f899..d3f472d 100644 --- a/source/libr2c2/sensor.h +++ b/source/libr2c2/sensor.h @@ -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 &); diff --git a/source/libr2c2/trackcircuit.h b/source/libr2c2/trackcircuit.h index 64e7462..19c11c7 100644 --- a/source/libr2c2/trackcircuit.h +++ b/source/libr2c2/trackcircuit.h @@ -15,7 +15,7 @@ private: public: TrackCircuit(Layout &, Block &); - Block &get_block() const { return block; } + virtual Block *get_block() const { return █ } }; } // namespace R2C2 diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index 25dcae9..b2a49c7 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -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(&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) {