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