#include <msp/core/maputils.h>
#include <msp/core/raii.h>
#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"
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()),
{
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<Track *> &tracks = layout.get_tracks();
+ const set<Track *> &tracks = layout.get_all<Track>();
for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
if((*i)->get_turnout_id())
(*i)->signal_path_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &BlockAllocator::turnout_path_changed), sigc::ref(**i))));
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(), IterBlockMatch(block));
if(i==blocks.end())
throw key_error(&block);
return *i;
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(), IterBlockMatch(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;
+ return find_if(blocks.begin(), cur_blocks_end, IterBlockMatch(block))!=cur_blocks_end;
}
void BlockAllocator::reserve_more()
}
}
-void BlockAllocator::release_until(const Block &block)
-{
- for(BlockList::iterator i=blocks.begin(); i!=cur_blocks_end; ++i)
- if(i->block()==&block)
- {
- if(++i!=cur_blocks_end)
- release_blocks_begin(i);
- return;
- }
-}
-
bool BlockAllocator::release_from(const Block &block)
{
bool have_sensor = false;
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<TrackCircuit *>(&sensor))
+ block = &tc->get_block();
+ else
+ return;
+
+ if(block->get_train()!=&train)
return;
- if(state==Block::MAYBE_ACTIVE)
+ if(state==Sensor::MAYBE_ACTIVE)
{
// Find the first sensor block from our reserved blocks that isn't this sensor
BlockList::iterator end;
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;
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();