Stride Reference Manual  1.0
SimplePlanner.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <list>
4 #include <vector>
5 #include <memory>
6 #include <iterator>
7 
8 namespace stride {
9 namespace util {
10 
11 using namespace std;
12 
22 template<typename T>
24 public:
25  using Block = vector<unique_ptr<T>>;
26  using Agenda = list<unique_ptr<Block>>;
27 
28  Block* getModifiableDay(unsigned int days) {
29  Block* block;
30  if (days >= m_agenda.size()) {
31  // make sure there are enough blocks in place
32  uint new_blocks = (days + 1) - m_agenda.size();
33  for (unsigned int i = 0; i < new_blocks; i++) {
34  block = new Block();
35  m_agenda.emplace_back(block);
36  }
37  } else {
38  block = next(m_agenda.begin(), days)->get();
39  }
40  return block;
41  }
42 
43  const Block* getDay(unsigned int days) const {
44  if (days >= m_agenda.size()) {
45  // nothing planned, return an empty one
46  return &g_empty_day;
47  } else {
48  return next(m_agenda.cbegin(), days)->get();
49  }
50  }
51 
52  const Block* today() const {
53  return getDay(0);
54  }
55 
56  void add(unsigned int days, T thing) {
57  Block* block = getModifiableDay(days);
58  T* new_thing = new T(thing);
59  block->emplace_back(new_thing);
60  }
61 
62  void nextDay() {
63  if (not m_agenda.empty()) m_agenda.pop_front();
64  }
65 
66  unsigned int days() const {
67  return m_agenda.size();
68  }
69 
71  return m_agenda;
72  }
73 
74  const Agenda& getAgenda() const {
75  return m_agenda;
76  }
77 
78  unsigned int size() const {
79  unsigned int size = 0;
80 
81  for (auto& block: m_agenda) {
82  size += block->size();
83  }
84 
85  return size;
86  }
87 
88 private:
91 };
92 
93 // look at how elegant C++ templates are
94 template<typename T>
96 
97 }
98 }
You can see this class as a kind of planner that stores events in the near future.
Definition: SimplePlanner.h:23
Time Dependent Person DataType.
Definition: NoBehaviour.h:17
unsigned int days() const
Definition: SimplePlanner.h:66
const Block * getDay(unsigned int days) const
Definition: SimplePlanner.h:43
void add(unsigned int days, T thing)
Definition: SimplePlanner.h:56
vector< unique_ptr< stride::Traveller< stride::Simulator::stride::Person > >> Block
Definition: SimplePlanner.h:25
const Agenda & getAgenda() const
Definition: SimplePlanner.h:74
unsigned int size() const
Definition: SimplePlanner.h:78
STL namespace.
Block * getModifiableDay(unsigned int days)
Definition: SimplePlanner.h:28
const Block * today() const
Definition: SimplePlanner.h:52