-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokencontainer.cpp
47 lines (41 loc) · 1.06 KB
/
tokencontainer.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
#include "tokencontainer.h"
TokenContainer::TokenContainer(QObject *parent,QTimer &timer, int maxTokens, int initial):QObject(parent), maxTokens(maxTokens), tokens(initial)
{
if(timer.parent() == nullptr){
timer.setParent(this);
}
setTokenAvailable(tokens>0);
connect(&timer,&QTimer::timeout,this,&TokenContainer::timePassed);
}
bool TokenContainer::tokenAvailable() const
{
return m_tokenAvailable;
}
///
/// \brief TokenContainer::getToken
/// \return
///
/// If there are any available tokens returns true and decrease the number of tokens.
bool TokenContainer::getToken()
{
if(tokens>0){
tokens -= 1;
setTokenAvailable(tokens>0);
return true;
}
return false;
}
void TokenContainer::timePassed()
{
if(tokens < maxTokens){
tokens = tokens+1;
setTokenAvailable(tokens>0);
}
}
void TokenContainer::setTokenAvailable(bool tokenAvailable)
{
if (m_tokenAvailable == tokenAvailable)
return;
m_tokenAvailable = tokenAvailable;
emit notifyTokenAvailableChanged(m_tokenAvailable);
}