]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/blockallocator.cpp
Add signals for adding and removing vehicles
[r2c2.git] / source / libr2c2 / blockallocator.cpp
index f73697a5b45381f8173b045ce70de1ac0abfd590..ff5776d3281f1b166af785b708b2e4e0a267424d 100644 (file)
@@ -15,12 +15,23 @@ 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()),
        pending_block(0),
        stop_at_block(0),
-       reserving(false)
+       reserving(false),
+       advancing(false)
 {
        Layout &layout = train.get_layout();
        layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved));
@@ -29,7 +40,22 @@ BlockAllocator::BlockAllocator(Train &t):
        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_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)
@@ -41,9 +67,23 @@ 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()
 {
        release_blocks_begin(blocks.end());
+       active = false;
        pending_block = 0;
        stop_at_block = 0;
 }
@@ -51,6 +91,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
@@ -82,7 +124,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;
@@ -90,19 +132,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()
@@ -157,7 +193,10 @@ void BlockAllocator::reserve_more()
                }
 
                if(!reserve_block(block))
+               {
+                       pending_block = &*block;
                        break;
+               }
 
                if(cur_blocks_end==blocks.end())
                        --cur_blocks_end;
@@ -229,29 +268,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; )
@@ -264,10 +280,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--);
@@ -276,6 +295,8 @@ void BlockAllocator::release_blocks_end(const BlockList::iterator &begin)
 
 void BlockAllocator::release_block(const BlockList::iterator &i)
 {
+       if(advancing)
+               throw logic_error("cannot release while advancing");
        if(i==cur_blocks_end)
                ++cur_blocks_end;
        if(&**i==pending_block)
@@ -288,10 +309,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)
@@ -340,10 +375,17 @@ void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
 
                if(result==1)
                {
-                       // Move blocks up to the next sensor to our current blocks
-                       for(BlockList::iterator j=cur_blocks_end; j!=end; ++j)
-                               train.signal_advanced.emit(**j);
+                       /* Advance the train to the new blocks.  Update cur_blocks_end first
+                       to keep things in sync. */
+                       SetFlag setf(advancing);
+                       BlockList::iterator i = cur_blocks_end;
                        cur_blocks_end = end;
+                       for(; i!=end; ++i)
+                               train.signal_advanced.emit(**i);
+                       advancing = false;
+
+                       if(active)
+                               reserve_more();
                }
                else if(result==3)
                        train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");