X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Fblockallocator.cpp;h=b38b62d6d1f1cd37763fcb2c3979dff8d4e97439;hb=0c2a3e6c435fd4cf05cc5275f750d341842aa543;hp=fc414cc99825d25b94140438923b9ed9f46bfff7;hpb=5c1ddd2f213af3fea15237e02f7da112c0abba36;p=r2c2.git diff --git a/source/libr2c2/blockallocator.cpp b/source/libr2c2/blockallocator.cpp index fc414cc..b38b62d 100644 --- a/source/libr2c2/blockallocator.cpp +++ b/source/libr2c2/blockallocator.cpp @@ -1,9 +1,11 @@ #include #include #include "blockallocator.h" +#include "block.h" #include "catalogue.h" #include "driver.h" #include "layout.h" +#include "trackcircuit.h" #include "trackiter.h" #include "train.h" #include "vehicle.h" @@ -13,6 +15,16 @@ using namespace Msp; namespace R2C2 { +struct BlockAllocator::BlockMatch +{ + const Block █ + + BlockMatch(const Block &b): block(b) { } + + bool operator()(const BlockIter &bi) const { return &*bi==█ } +}; + + BlockAllocator::BlockAllocator(Train &t): train(t), cur_blocks_end(blocks.end()), @@ -22,12 +34,27 @@ BlockAllocator::BlockAllocator(Train &t): { Layout &layout = train.get_layout(); layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved)); - layout.signal_block_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::block_state_changed)); + layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::sensor_state_changed)); - const set &tracks = layout.get_tracks(); + const set &tracks = layout.get_all(); for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) if((*i)->get_turnout_id()) + { + (*i)->signal_path_changing.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changing), sigc::ref(**i)))); (*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i)))); + } +} + +void BlockAllocator::set_active(bool a) +{ + active = a; + if(active) + reserve_more(); + else + { + release_blocks_end(cur_blocks_end); + pending_block = 0; + } } void BlockAllocator::start_from(const BlockIter &block) @@ -39,8 +66,22 @@ void BlockAllocator::start_from(const BlockIter &block) reserve_block(block); } +void BlockAllocator::rewind_to(const Block &block) +{ + if(!active) + return; + + BlockList::iterator i = find_if(cur_blocks_end, blocks.end(), BlockMatch(block)); + if(i!=blocks.end()) + { + release_blocks_end(i); + reserve_more(); + } +} + void BlockAllocator::clear() { + active = false; release_blocks_begin(blocks.end()); pending_block = 0; stop_at_block = 0; @@ -49,6 +90,8 @@ void BlockAllocator::clear() void BlockAllocator::stop_at(const Block *block) { stop_at_block = block; + if(active && !block) + reserve_more(); } const BlockIter &BlockAllocator::first() const @@ -80,7 +123,7 @@ const BlockIter &BlockAllocator::last_current() const const BlockIter &BlockAllocator::iter_for(const Block &block) const { - BlockList::const_iterator i = find_block(blocks.begin(), blocks.end(), block); + BlockList::const_iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(block)); if(i==blocks.end()) throw key_error(&block); return *i; @@ -88,19 +131,13 @@ const BlockIter &BlockAllocator::iter_for(const Block &block) const bool BlockAllocator::has_block(const Block &block) const { - return find_block(blocks.begin(), blocks.end(), block)!=blocks.end(); + return find_if(blocks.begin(), blocks.end(), BlockMatch(block))!=blocks.end(); } bool BlockAllocator::is_block_current(const Block &block) const { - return find_block(blocks.begin(), cur_blocks_end, block)!=cur_blocks_end; -} - -BlockAllocator::BlockList::const_iterator BlockAllocator::find_block(const BlockList::const_iterator &begin, const BlockList::const_iterator &end, const Block &block) const -{ - BlockList::const_iterator i; - for(i=begin; (i!=end && &**i!=&block); ++i) ; - return i; + BlockList::const_iterator end = cur_blocks_end; + return find_if(blocks.begin(), end, BlockMatch(block))!=cur_blocks_end; } void BlockAllocator::reserve_more() @@ -227,29 +264,6 @@ bool BlockAllocator::reserve_block(const BlockIter &block) } } -bool BlockAllocator::release_from(const Block &block) -{ - bool have_sensor = false; - for(BlockList::iterator i=cur_blocks_end; i!=blocks.end(); ++i) - { - if(i->block()==&block) - { - if(have_sensor) - release_blocks_end(i); - return have_sensor; - } - else if((*i)->get_sensor_id()) - have_sensor = true; - } - - return false; -} - -void BlockAllocator::release_noncurrent() -{ - release_blocks_end(cur_blocks_end); -} - void BlockAllocator::release_blocks_begin(const BlockList::iterator &end) { for(BlockList::iterator i=blocks.begin(); i!=end; ) @@ -262,10 +276,13 @@ void BlockAllocator::release_blocks_end(const BlockList::iterator &begin) if(begin==blocks.begin()) return release_blocks_begin(blocks.end()); + if(begin==blocks.end()) + return; + /* Release the blocks in reverse order so that a consistent state is presented in block_reserved signal. */ bool done = false; - for(BlockList::iterator i=blocks.end(); !done; ) + for(BlockList::iterator i=--blocks.end(); !done; ) { done = (i==begin); release_block(i--); @@ -286,10 +303,24 @@ void BlockAllocator::release_block(const BlockList::iterator &i) void BlockAllocator::reverse() { - release_noncurrent(); + release_blocks_end(cur_blocks_end); blocks.reverse(); for(BlockList::iterator i=blocks.begin(); i!=blocks.end(); ++i) *i = i->reverse(); + + if(active) + reserve_more(); +} + +void BlockAllocator::turnout_path_changing(Track &track) +{ + BlockList::iterator i = find_if(blocks.begin(), blocks.end(), BlockMatch(track.get_block())); + if(i!=blocks.end()) + { + ++i; + release_blocks_end(i); + pending_block = &track.get_block(); + } } void BlockAllocator::turnout_path_changed(Track &track) @@ -304,12 +335,18 @@ void BlockAllocator::block_reserved(Block &block, const Train *tr) reserve_more(); } -void BlockAllocator::block_state_changed(Block &block, Block::State state) +void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state) { - if(block.get_train()!=&train) + Block *block = 0; + if(TrackCircuit *tc = dynamic_cast(&sensor)) + block = &tc->get_block(); + else return; - if(state==Block::MAYBE_ACTIVE) + if(block->get_train()!=&train) + return; + + if(state==Sensor::MAYBE_ACTIVE) { // Find the first sensor block from our reserved blocks that isn't this sensor BlockList::iterator end; @@ -317,7 +354,7 @@ void BlockAllocator::block_state_changed(Block &block, Block::State state) for(end=cur_blocks_end; end!=blocks.end(); ++end) if((*end)->get_sensor_id()) { - if(&**end!=&block) + if(&**end!=block) { if(result==0) result = 2; @@ -336,11 +373,14 @@ void BlockAllocator::block_state_changed(Block &block, Block::State state) for(BlockList::iterator j=cur_blocks_end; j!=end; ++j) train.signal_advanced.emit(**j); cur_blocks_end = end; + + if(active) + reserve_more(); } else if(result==3) train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order"); } - else if(state==Block::INACTIVE) + else if(state==Sensor::INACTIVE) { const Vehicle &veh = train.get_vehicle(train.get_controller().get_reverse() ? 0 : train.get_n_vehicles()-1); const Block &veh_block = veh.get_track()->get_block();