/
loop.cpp
158 lines (128 loc) · 3.31 KB
/
loop.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
/** @file loop.cpp
*
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/Loop"
#include "de/App"
#include "de/Time"
#include "de/Log"
#include "de/math.h"
#include "callbacktimer.h"
#include <QCoreApplication>
#include <QTimer>
namespace de {
static Loop *loopSingleton = 0;
DENG2_PIMPL(Loop)
{
TimeDelta interval;
bool running;
QTimer *timer;
Instance(Public *i) : Base(i), interval(0), running(false)
{
DENG2_ASSERT(!loopSingleton);
loopSingleton = i;
timer = new QTimer(thisPublic);
QObject::connect(timer, SIGNAL(timeout()), thisPublic, SLOT(nextLoopIteration()));
}
~Instance()
{
loopSingleton = 0;
}
DENG2_PIMPL_AUDIENCE(Iteration)
};
DENG2_AUDIENCE_METHOD(Loop, Iteration)
Loop::Loop() : d(new Instance(this))
{}
void Loop::setRate(int freqHz)
{
d->interval = 1.0 / freqHz;
d->timer->setInterval(de::max(1, int(d->interval.asMilliSeconds())));
}
void Loop::start()
{
d->running = true;
d->timer->start();
}
void Loop::stop()
{
d->running = false;
d->timer->stop();
}
void Loop::pause()
{
d->timer->stop();
}
void Loop::resume()
{
d->timer->start();
}
void Loop::timer(TimeDelta const &delay, void (*func)(void))
{
// The timer will delete itself after it's triggered.
internal::CallbackTimer *timer = new internal::CallbackTimer(func, qApp);
timer->start(delay.asMilliSeconds());
}
Loop &Loop::get()
{
DENG2_ASSERT(loopSingleton != 0);
return *loopSingleton;
}
void Loop::nextLoopIteration()
{
try
{
if(d->running)
{
DENG2_FOR_AUDIENCE2(Iteration, i) i->loopIteration();
}
}
catch(Error const &er)
{
LOG_AS("Loop");
// This is called from Qt's event loop, we mustn't let exceptions
// out of here uncaught.
App::app().handleUncaughtException("Uncaught exception during loop iteration:\n" + er.asText());
}
}
LoopCallback::LoopCallback()
{}
LoopCallback::~LoopCallback()
{
Loop::get().audienceForIteration() -= this;
}
bool LoopCallback::isEmpty() const
{
return _funcs.isEmpty();
}
void LoopCallback::enqueue(Callback func)
{
DENG2_GUARD(this);
_funcs << func;
Loop::get().audienceForIteration() += this;
}
void LoopCallback::loopIteration()
{
DENG2_GUARD(this);
Loop::get().audienceForIteration() -= this;
// Make a copy of the list if new callbacks get enqueued in the callback.
QList<Callback> const funcs = _funcs;
_funcs.clear();
for(Callback const &cb : funcs)
{
cb();
}
}
} // namespace de