]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/blockallocator.h
Avoid nested block reservations completely
[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         bool reserve_pending;
48
49 public:
50         BlockAllocator(Train &);
51
52         void set_active(bool);
53         bool is_active() const { return active; }
54
55         bool start_from(const BlockIter &);
56         void rewind_to(const Block &);
57         void clear();
58         bool empty() const { return blocks.empty(); }
59         void stop_at(const Block *);
60
61         const BlockIter &first() const;
62         const BlockIter &last() const;
63         const BlockIter &last_current() const;
64         const BlockIter &iter_for(const Block &) const;
65
66         bool has_block(const Block &) const;
67         bool is_block_current(const Block &) const;
68
69         void tick();
70
71 private:
72         void reserve_more();
73         bool reserve_block(const BlockIter &);
74         void advance_front(const Block *, bool);
75         void advance_front(const Sensor *);
76         void advance_back();
77         void release_blocks_begin(const BlockList::iterator &);
78         void release_blocks_end(const BlockList::iterator &);
79         void release_block(const BlockList::iterator &);
80 public:
81         void reverse();
82
83 private:
84         void turnout_path_changing(Track &);
85         void turnout_path_changed(Track &);
86         void block_reserved(Block &, const Train *);
87         void sensor_state_changed(Sensor &, Sensor::State);
88         void update_next_sensor(Sensor *);
89         void halt_event(bool);
90
91 public:
92         void save(std::list<Msp::DataFile::Statement> &) const;
93 };
94
95 } // namepsace R2C2
96
97 #endif