DtkCore
DTK Core module
dutil.h
1/*
2 * Copyright (C) 2016 ~ 2017 Deepin Technology Co., Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <QTimer>
21#include <QThread>
22#include <QMetaObject>
23#include <QCoreApplication>
24
25namespace DUtil
26{
27
28template <typename Func1>
29inline void TimerSingleShot(int msec, Func1 slot)
30{
31#if QT_VERSION >= 0x050500
32 QTimer::singleShot(msec, slot);
33#else
34 QTimer *timer = new QTimer;
35 timer->setSingleShot(true);
36 timer->setInterval(msec);
37 timer->moveToThread(qApp->thread());
38 QObject::connect(timer, &QTimer::timeout, slot);
39 QObject::connect(timer, &QTimer::timeout, timer, &QTimer::deleteLater);
40 if (QThread::currentThread() == qApp->thread()) { timer->start(); }
41 else { QMetaObject::invokeMethod(timer, "start", Qt::QueuedConnection); }
42#endif
43}
44
45template <class T>
46void SecureErase(T *p, size_t size)
47{
48 memset(p, 0, size);
49}
50
51template <class T>
52void SecureErase(T &obj)
53{
54 for (typename T::iterator i = obj.begin(); i != obj.end(); ++i) {
55 *i = 0;
56 }
57 obj.clear();
58}
59
60}