-
Notifications
You must be signed in to change notification settings - Fork 0
/
timestep.cpp
110 lines (87 loc) · 2.48 KB
/
timestep.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
#include "consts.h"
#include "timestep.h"
const int TimeStep::INDEX_BEG;
const int TimeStep::INDEX_END;
const int TimeStep::SECONDS_END;
TimeStep::TimeStep(QObject *parent) :
QObject(parent),
steps({SECONDS_JOB, SECONDS_MIN_BREAK,
SECONDS_JOB, SECONDS_MIN_BREAK,
SECONDS_JOB, SECONDS_MIN_BREAK,
SECONDS_JOB, SECONDS_MAX_BREAK}),
index(TimeStep::INDEX_BEG)
{
}
void TimeStep::setSteps(int jobSteps, int jobSeconds, int minBreakSeconds, int maxBreakSeconds)
{
if(jobSteps == 0)
throw new std::underflow_error("Argument \"jobSteps\" allowed value is 1 or more");
if(jobSeconds == 0)
throw new std::underflow_error("Argument \"jobSeconds\" allowed value is 1 or more");
if(minBreakSeconds == 0)
throw new std::underflow_error("Argument \"minBreakSeconds\" allowed value is 1 or more");
if(maxBreakSeconds == 0)
throw new std::underflow_error("Argument \"maxBreakSeconds\" allowed value is 1 or more");
steps.clear();
steps.reserve(jobSteps*2);
for(int i=0; i<jobSteps; ++i)
{
steps.push_back(jobSeconds);
steps.push_back(minBreakSeconds);
}
steps.back() = maxBreakSeconds;
reset();
}
TTimeSteps::size_type TimeStep::getSize() const
{
return steps.size();
}
int TimeStep::getIndex() const
{
return index;
}
int TimeStep::getSeconds() const
{
if(index == TimeStep::INDEX_BEG)
throw new std::underflow_error("Member \"index\" is at begin position");
if(index == TimeStep::INDEX_END)
throw new std::underflow_error("Member \"index\" is at end position");
return steps[index];
}
int TimeStep::next()
{
int size = steps.size();
if(index < size)
index += 1;
else
index = TimeStep::INDEX_END;
return steps[index];
}
bool TimeStep::hasNext()
{
return static_cast<std::vector<int>::size_type>(index+1) != steps.size();
}
bool TimeStep::isLast()
{
return static_cast<std::vector<int>::size_type>(index+1) == steps.size();
}
bool TimeStep::isFirst()
{
return index == 0;
}
void TimeStep::reset()
{
index = TimeStep::INDEX_BEG;
}
bool TimeStep::isJob() const
{
return index % 2 == 0 && static_cast<std::vector<int>::size_type>(index+1) != steps.size();
}
bool TimeStep::isBreakMin() const
{
return index % 2 != 0 && static_cast<std::vector<int>::size_type>(index+1) != steps.size();
}
bool TimeStep::isBreakMax() const
{
return static_cast<std::vector<int>::size_type>(index+1) == steps.size();
}