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