]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/blockallocator.h
Make train advancement logic more robust
[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 private:
31         struct BlockMatch;
32
33         typedef std::list<BlockIter> BlockList;
34
35         Train &train;
36         bool active;
37         BlockList blocks;
38         BlockList::iterator cur_blocks_end;
39         Block *pending_block;
40         const Block *stop_at_block;
41         bool reserving;
42         bool advancing;
43
44 public:
45         BlockAllocator(Train &);
46
47         void set_active(bool);
48         bool is_active() const { return active; }
49
50         void start_from(const BlockIter &);
51         void rewind_to(const Block &);
52         void clear();
53         bool empty() const { return blocks.empty(); }
54         void stop_at(const Block *);
55
56         const BlockIter &first() const;
57         const BlockIter &last() const;
58         const BlockIter &last_current() const;
59         const BlockIter &iter_for(const Block &) const;
60
61         bool has_block(const Block &) const;
62         bool is_block_current(const Block &) const;
63
64 private:
65         void reserve_more();
66         bool reserve_block(const BlockIter &);
67         void release_blocks_begin(const BlockList::iterator &);
68         void release_blocks_end(const BlockList::iterator &);
69         void release_block(const BlockList::iterator &);
70 public:
71         void reverse();
72
73 private:
74         void turnout_path_changing(Track &);
75         void turnout_path_changed(Track &);
76         void block_reserved(Block &, const Train *);
77         void sensor_state_changed(Sensor &, Sensor::State);
78
79 public:
80         void save(std::list<Msp::DataFile::Statement> &) const;
81 };
82
83 } // namepsace R2C2
84
85 #endif