19#include <q26numeric.h>
21using namespace std::chrono_literals;
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
123
124
126QTimer::QTimer(QObject *parent)
127 : QObject(*
new QTimerPrivate(
this), parent)
129 Q_ASSERT(d_func()->isQTimer);
134
135
139 if (d_func()->isActive())
145
146
147
148
149
150
153
154
155
156
157
158
161
162
163
164
165bool QTimer::isActive()
const
167 return d_func()->isActiveData.value();
170QBindable<
bool> QTimer::bindableActive()
172 return QBindable<
bool>(&d_func()->isActiveData);
176
177
178
179
180
181int QTimer::timerId()
const
183 auto v = qToUnderlying(id());
184 return v == 0 ? -1 : v;
188
189
190
191
192
193
194Qt::TimerId QTimer::id()
const
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
223 Qt::TimerId newId{ QObject::startTimer(d->inter * 1ms, d->type) };
224 if (newId > Qt::TimerId::Invalid) {
226 d->isActiveData.notify();
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249void QTimer::start(
int msec)
257 if (interval < 0ms) {
258 qWarning(
"%s: negative intervals aren't allowed; the interval will be set to 1ms.", caller);
262 const auto msec = interval.count();
263 int ret =
q26::saturate_cast<
int>(msec);
265 qWarning(
"%s: interval exceeds maximum allowed interval, it will be clamped to "
266 "INT_MAX ms (about 24 days).", caller);
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292void QTimer::start(std::chrono::milliseconds interval)
296 const int msec = checkInterval(
"QTimer::start", interval);
297 const bool intervalChanged = msec != d->inter;
298 d->inter.setValue(msec);
307
308
309
310
316 QObject::killTimer(d->id);
317 d->id = Qt::TimerId::Invalid;
318 d->isActiveData.notify();
324
325
326void QTimer::timerEvent(QTimerEvent *e)
329 if (e->id() == d->id) {
332 emit timeout(QPrivateSignal());
336QAbstractEventDispatcher::Duration
337QTimer::from_msecs(std::chrono::milliseconds ms)
339 using Duration = QAbstractEventDispatcher::Duration;
341 using namespace std::chrono;
342 using ratio = std::ratio_divide<std::milli, Duration::period>;
343 static_assert(ratio::den == 1);
346 if (qMulOverflow<ratio::num>(ms.count(), &r)) {
347 qWarning(
"QTimer::singleShot(std::chrono::milliseconds, ...): "
348 "interval argument overflowed when converted to nanoseconds.");
349 return Duration::max();
355
356
357
358
359
360
361
362
363
364
365void QTimer::singleShotImpl(std::chrono::nanoseconds ns, Qt::TimerType timerType,
366 const QObject *receiver,
367 QtPrivate::QSlotObjectBase *slotObj)
370 bool deleteReceiver =
false;
378 if (!receiver && QThread::currentThread() == QCoreApplicationPrivate::mainThread()) {
380 receiver = QThread::currentThread();
381 }
else if (!receiver) {
384 receiver =
new QObject;
385 deleteReceiver =
true;
388 auto h = QtPrivate::invokeMethodHelper({});
389 QMetaObject::invokeMethodImpl(
const_cast<QObject *>(receiver), slotObj,
390 Qt::QueuedConnection, h.parameterCount(), h.parameters.data(), h.typeNames.data(),
394 const_cast<QObject *>(receiver)->deleteLater();
398 (
void)
new QSingleShotTimer(ns, timerType, receiver, slotObj);
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
445void QTimer::singleShot(std::chrono::nanoseconds ns, Qt::TimerType timerType,
446 const QObject *receiver,
const char *member)
449 qWarning(
"QTimer::singleShot: negative intervals aren't allowed; the "
450 "interval will be set to 1ms.");
453 if (receiver && member) {
456 const char* bracketPosition = strchr(member,
'(');
457 if (!bracketPosition || !(member[0] >=
'0' && member[0] <=
'2')) {
458 qWarning(
"QTimer::singleShot: Invalid slot specification");
461 const auto methodName = QByteArrayView(member + 1,
462 bracketPosition - 1 - member).trimmed();
463 QMetaObject::invokeMethod(
const_cast<QObject *>(receiver), methodName.toByteArray().constData(),
464 Qt::QueuedConnection);
467 (
void)
new QSingleShotTimer(ns, timerType, receiver, member);
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
573
574
575
576
577
578
579
580
581
582
583
584
585
586
589
590
591
592
593
594
595
598
599
600
601
602
603
604
605
606
607
608
611
612
613
614
615
616
617
618
619
620
621void QTimer::setSingleShot(
bool singleShot)
623 d_func()->single = singleShot;
626bool QTimer::isSingleShot()
const
628 return d_func()->single;
631QBindable<
bool> QTimer::bindableSingleShot()
633 return QBindable<
bool>(&d_func()->single);
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654void QTimer::setInterval(
int msec)
656 setInterval(std::chrono::milliseconds{msec});
659void QTimer::setInterval(std::chrono::milliseconds interval)
663 const int msec = checkInterval(
"QTimer::setInterval", interval);
664 d->inter.removeBindingUnlessInWrapper();
665 const bool intervalChanged = msec != d->inter.valueBypassingBindings();
666 d->inter.setValueBypassingBindings(msec);
668 QObject::killTimer(d->id);
669 Qt::TimerId newId{ QObject::startTimer(msec * 1ms, d->type) };
670 if (newId > Qt::TimerId::Invalid) {
676 d->id = Qt::TimerId::Invalid;
677 d->isActiveData.notify();
684int QTimer::interval()
const
686 return d_func()->inter;
689QBindable<
int> QTimer::bindableInterval()
691 return QBindable<
int>(&d_func()->inter);
695
696
697
698
699
700
701
702
703
704
705int QTimer::remainingTime()
const
709 using namespace std::chrono;
710 auto remaining = QAbstractEventDispatcher::instance()->remainingTime(d->id);
711 const auto msec = ceil<milliseconds>(remaining).count();
712 const int ret = q26::saturate_cast<
int>(msec);
713 Q_ASSERT(ret == msec);
721
722
723
724
725
726
727
728void QTimer::setTimerType(Qt::TimerType atype)
730 d_func()->type = atype;
733Qt::TimerType QTimer::timerType()
const
735 return d_func()->type;
738QBindable<Qt::TimerType> QTimer::bindableTimerType()
740 return QBindable<Qt::TimerType>(&d_func()->type);
745#include "moc_qtimer.cpp"
~QTimerPrivate() override
static int checkInterval(const char *caller, std::chrono::milliseconds interval)