]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/blockallocator.h
Don't diverge from critical blocks
[r2c2.git] / source / libr2c2 / blockallocator.h
1 #ifndef LIBR2C2_BLOCKALLOCATOR_H_
2 #define LIBR2C2_BLOCKALLOCATOR_H_
3
4 #include <list>
5 #include <msp/datafile/objectloader.h>
6 #include "blockiter.h"
7 #include "sensor.h"
8
9 namespace R2C2 {
10
11 class Train;
12
13 class BlockAllocator
14 {
15 public:
16         class Loader: public Msp::DataFile::ObjectLoader<BlockAllocator>
17         {
18         private:
19                 Block *prev_block;
20                 bool valid;
21
22         public:
23                 Loader(BlockAllocator &);
24
25         private:
26                 void block(unsigned);
27                 void hint(unsigned);
28         };
29
30         sigc::signal<void, Block &, Sensor *> signal_advanced;
31         sigc::signal<void, Block &> signal_rear_advanced;
32
33 private:
34         struct BlockMatch;
35
36         typedef std::list<BlockIter> BlockList;
37
38         Train &train;
39         bool active;
40         BlockList blocks;
41         BlockList::iterator cur_blocks_end;
42         Sensor *next_sensor;
43         Block *pending_block;
44         const Block *stop_at_block;
45         bool reserving;
46         bool advancing;
47
48 public:
49         BlockAllocator(Train &);
50
51         void set_active(bool);
52         bool is_active() const { return active; }
53
54         bool start_from(const BlockIter &);
55         void rewind_to(const Block &);
56         void clear();
57         bool empty() const { return blocks.empty(); }
58         void stop_at(const Block *);
59
60         const BlockIter &first() const;
61         const BlockIter &last() const;
62         const BlockIter &last_current() const;
63         const BlockIter &iter_for(const Block &) const;
64
65         bool has_block(const Block &) const;
66         bool is_block_current(const Block &) const;
67
68 private:
69         void reserve_more();
70         bool reserve_block(const BlockIter &);
71         void advance_front(const Block *, bool);
72         void advance_front(const Sensor *);
73         void advance_back();
74         void release_blocks_begin(const BlockList::iterator &);
75         void release_blocks_end(const BlockList::iterator &);
76         void release_block(const BlockList::iterator &);
77 public:
78         void reverse();
79
80 private:
81         void turnout_path_changing(Track &);
82         void turnout_path_changed(Track &);
83         void block_reserved(Block &, const Train *);
84         void sensor_state_changed(Sensor &, Sensor::State);
85         void update_next_sensor(Sensor *);
86
87 public:
88         void save(std::list<Msp::DataFile::Statement> &) const;
89 };
90
91 } // namepsace R2C2
92
93 #endif