Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15 andreas 1
/*
2
 * Copyright (C) 2021 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) 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 General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#ifndef __TTIMER_H__
20
#define __TTIMER_H__
21
 
22
#include <thread>
23
#include <chrono>
24
#include <functional>
25
 
21 andreas 26
#ifdef __ANDROID__
27
typedef unsigned long int ulong;
28
#endif
29
 
30
extern bool prg_stopped;
31
 
15 andreas 32
class TTimer
33
{
34
    public:
35
        TTimer();
36
        ~TTimer();
37
 
38
        void run();
39
        void run_once();
40
        void run_once(std::chrono::milliseconds ms);
41
        void stop() { mStopped = true; }
42
        void setInterval(std::chrono::milliseconds ms) { mMsec = ms; }
43
        void registerCallback(std::function<void(ulong)> callback) { _callback = callback; }
44
        bool isRunning() { return mRunning; }
45
 
46
    protected:
47
        void _run();
48
 
49
    private:
50
        std::function<void(ulong)> _callback{nullptr};
51
        std::chrono::milliseconds mMsec{0};
52
        bool mOnce{false};
53
        std::thread mThread;
54
        ulong mCounter{0};
55
        bool mStopped{false};
56
        bool mRunning{false};
57
};
58
 
59
#endif