]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/blockallocator.h
7ff5d77f2cf538b4effc9fa4c40826b9a8cb956d
[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 "block.h"
7 #include "blockiter.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
52         int get_entry_to_block(const Block &) const;
53
54         void reserve_more();
55         void release_until(const Block &);
56         bool release_from(const Block &);
57         void release_noncurrent();
58 private:
59         void release_blocks(const BlockList::iterator &, const BlockList::iterator &);
60 public:
61         void reverse();
62
63 private:
64         void turnout_path_changed(Track &);
65         void block_reserved(Block &, const Train *);
66         void block_state_changed(Block &, Block::State);
67
68 public:
69         void save(std::list<Msp::DataFile::Statement> &) const;
70 };
71
72 } // namepsace R2C2
73
74 #endif