-
Notifications
You must be signed in to change notification settings - Fork 0
/
timetable.cpp
389 lines (345 loc) · 10.8 KB
/
timetable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include "timetable.h"
// STL dependencies
#include <stdexcept>
//#############################################################################
// Timetable::Plan
//#############################################################################
// Constructors
//**********************************************************************************
/** \brief standard constructor
*
*
*/
Timetable::Plan::Plan()
:_startTime(0), _endTime(0), _interval(0), _days(F_DAY_NONE)
{ }
//**********************************************************************************
/** \brief initialize constructor
*
* \param[in] startTime The time of the day the Plan starts
* \param[in] endTime The time of the day the Plan ends
* \param[in] interval the time interval
* \param[in] daymask The days at which the plan is active (flags)
*
*/
Timetable::Plan::Plan(const DayTimeType& startTime, const DayTimeType& endTime, const TimeInterval& interval, DayFlags daymask)
: _startTime(startTime), _endTime(endTime), _interval(interval), _days(daymask)
{
_startTime.makeDaytime();
_endTime.makeDaytime();
}
//**********************************************************************************
/** \brief deserialize constructor
*
* \param[in] pt PropertyTree from which to construct
*
*/
Timetable::Plan::Plan(const Serializable::PropertyTree& pt)
{
deserialize(pt);
}
// public members
//**********************************************************************************
/** \brief sets the start time of the plan
*
* \param[in] startTime Time of the day at which the Plan starts
*
*/
void Timetable::Plan::setStartTime(const Time& startTime)
{
_startTime = startTime;
_startTime.makeDaytime();
}
//**********************************************************************************
/** \brief returns the start time of the plan
*
* \return Time of the day at which the Plan starts
*
*/
Time& Timetable::Plan::getStartTime()
{
return _startTime;
}
//**********************************************************************************
/** \brief returns the start time of the plan (const version)
*
* \return Time of the day at which the Plan starts
*
*/
const Time& Timetable::Plan::getStartTime() const
{
return _startTime;
}
/** \brief sets the end time of the plan
*
* \param[in] endTime Time of the day at which the Plan ends
*
*/
void Timetable::Plan::setEndTime(const Time& endTime)
{
_endTime = endTime;
_endTime.makeDaytime();
}
//**********************************************************************************
/** \brief returns the end time of the plan
*
* \return Time of the day at which the Plan ends
*
*/
Time& Timetable::Plan::getEndTime()
{
return _endTime;
}
//**********************************************************************************
/** \brief returns the end time of the plan (const version)
*
* \return Time of the day at which the Plan ends
*
*/
const Time& Timetable::Plan::getEndTime() const
{
return _endTime;
}
//**********************************************************************************
/** \brief sets the interval of the plan
*
* \param[in] new interval
*
*/
void Timetable::Plan::setInterval(const Time& interval)
{
_interval = interval;
}
//**********************************************************************************
/** \brief returns the interval of the plan
*
* \return interval
*
*/
Time& Timetable::Plan::getInterval()
{
return _interval;
}
//**********************************************************************************
/** \brief returns the interval of the plan (const version)
*
* \return interval
*
*/
const Time& Timetable::Plan::getInterval() const
{
return _interval;
}
//**********************************************************************************
/** \brief returns true if the Plan is active at the given day
*
* \param[in] day name of the day
* \return true if active at given day. False if not
*
*/
const bool Timetable::Plan::activeAtDay(const DayName& day)const
{
assert(day < DAY_NUM);
return _days & (1 << day);
}
//**********************************************************************************
/** \brief returns true if the Plan is active at the given days
*
* \param[in] days flags
* \return true if active at all given days. False if not
*
*/
const bool Timetable::Plan::activeAtDays(const DayFlags& days)const
{
return (_days | days) == _days;
}
//**********************************************************************************
/** \brief activate days
*
* \param[in] days flags
* \detail Plans dayflags indicated by days will be set after calling this method
*
*/
void Timetable::Plan::activateDays(const DayFlags& days)
{
_days = _days | days;
}
//**********************************************************************************
/** \brief activates day
*
* \param[in] day The day to activate
*
*/
void Timetable::Plan::activateDay(const DayName& day)
{
activateDays((1 << day));
}
//**********************************************************************************
/** \brief deactivate days
*
* \param[in] days mask
* \detail deactivate the days indicated by mask
*
*/
void Timetable::Plan::deactivateDays(const DayFlags& days)
{
_days = _days & ~days;
}
//**********************************************************************************
/** \brief deactivates day
*
* \param[in] day The day to deactivate
*
*/
void Timetable::Plan::deactivateDay(const DayName& day)
{
deactivateDays((1 << day));
}
//**********************************************************************************
/** \brief toggles the days indicated by mask
*
* \param[in] days mask
*
*/
void Timetable::Plan::toggleDays(const DayFlags& days)
{
_days = _days ^ days;
}
//**********************************************************************************
/** \brief toggles the given day
*
* \param[in] day day to toggle
*
*/
void Timetable::Plan::toggleDay(const DayName& day)
{
toggleDays((1 << day));
}
//**********************************************************************************
/** \brief serializes the Plan
*
*
*/
Serializable::PropertyTree Timetable::Plan::serialize() const
{
PropertyTree pt;
pt.put("start-time", _startTime.time);
pt.put("end-time", _endTime.time);
pt.put("interval", _interval.time);
pt.put("days", _days);
return pt;
}
//**********************************************************************************
/** \brief deserializes the Plan
*
*
*/
void Timetable::Plan::deserialize(const Serializable::PropertyTree& pt)
{
_startTime = Time(pt.get<Time::ValueType>("start-time", 0));
_endTime = Time(pt.get<Time::ValueType>("end-time", 0));
_interval = Time(pt.get<Time::ValueType>("interval", 0));
_days = pt.get<DayFlags>("days", F_DAY_NONE);
}
//#############################################################################
// Timetable
//#############################################################################
// constructors
//**********************************************************************************
/** \brief standard constructor
*
* \detail sets up a standard CiM2 timetable
*/
Timetable::Timetable()
{
_plans[PLAN_WEEKDAY] = Plan(Time(6, 0), Time(22, 0), Time(2, 0), F_DAY_MON_TO_THU | F_DAY_FRIDAY);
_plans[PLAN_MORNING_RUSH] = Plan(Time(7, 0), Time(7, 30), Time(2, 0), F_DAY_MON_TO_THU | F_DAY_FRIDAY);
_plans[PLAN_EVENING_RUSH] = Plan(Time(17, 0), Time(17, 30), Time(2, 0), F_DAY_MON_TO_THU | F_DAY_FRIDAY);
_plans[PLAN_WEEKEND] = Plan(Time(6, 0), Time(21, 0), Time(3, 0), F_DAY_SATURDAY | F_DAY_SUNDAY);
_plans[PLAN_NIGHT] = Plan(Time(0, 0), Time(3, 0), Time(3, 0), F_DAY_EVERY_DAY);
_plans[PLAN_CUSTOM] = Plan(Time(0, 0), Time(0, 0), Time(0, 0), F_DAY_NONE);
}
//**********************************************************************************
/** \brief deserialize constructor
*
*/
Timetable::Timetable(const PropertyTree& pt)
{
deserialize(pt);
}
// public members
//**********************************************************************************
/** \brief returns a plan
*
* \param[in] plan plan name
* \return reference to plan
* \throw std::invalid_argument if plan-name is invalid
* \detail should only be used with values of E_PlanLabel
*/
Timetable::Plan& Timetable::getPlan(const PlanName& plan)
{
if(plan >= PLAN_NUM) throw std::invalid_argument("invalid plan");
return _plans[plan];
}
//**********************************************************************************
/** \brief returns a plan (const version)
*
* \param[in] plan plan name
* \return const reference to plan
* \throw std::invalid_argument if plan-name is invalid
* \detail should only be used with values of E_PlanLabel
*/
const Timetable::Plan& Timetable::getPlan(const PlanName& plan) const
{
if(plan >= PLAN_NUM) throw std::invalid_argument("invalid plan");
return _plans[plan];
}
//**********************************************************************************
/** \brief sets a plan
*
* \param[in] planName plan name
* \param[in] plan plan
* \throw std::invalid_argument if plan-name is invalid
* \detail should only be used with values of E_PlanLabel
*/
void Timetable::setPlan(const PlanName& planName, const Timetable::Plan& plan)
{
if(planName >= PLAN_NUM) throw std::invalid_argument("invalid plan name");
_plans[planName] = plan;
}
//**********************************************************************************
/** \brief returns the last time slice of the week + 1
*
*/
inline Time Timetable::getEndOfWeek() const
{
return Time(TIME_SLICES_PER_DAY * DAY_NUM);
}
//**********************************************************************************
/** \brief serializes the timetable
*
*/
Serializable::PropertyTree Timetable::serialize() const
{
PropertyTree pt;
pt.put_child("plans.weekday", _plans[PLAN_WEEKDAY].serialize());
pt.put_child("plans.morning-rush", _plans[PLAN_MORNING_RUSH].serialize());
pt.put_child("plans.evening-rush", _plans[PLAN_EVENING_RUSH].serialize());
pt.put_child("plans.weekend", _plans[PLAN_WEEKEND].serialize());
pt.put_child("plans.night", _plans[PLAN_NIGHT].serialize());
pt.put_child("plans.custom", _plans[PLAN_CUSTOM].serialize());
return pt;
}
//**********************************************************************************
/** \brief deserializes the timetable
*
*/
void Timetable::deserialize(const Serializable::PropertyTree& pt)
{
_plans[PLAN_WEEKDAY] = Plan(pt.get_child("plans.weekday"));
_plans[PLAN_MORNING_RUSH] = Plan(pt.get_child("plans.morning-rush"));
_plans[PLAN_EVENING_RUSH] = Plan(pt.get_child("plans.evening-rush"));
_plans[PLAN_WEEKEND] = Plan(pt.get_child("plans.weekend"));
_plans[PLAN_NIGHT] = Plan(pt.get_child("plans.night"));
_plans[PLAN_CUSTOM] = Plan(pt.get_child("plans.custom"));
}