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>
93 andreas 25
#include <atomic>
15 andreas 26
 
235 andreas 27
#if defined(__ANDROID__) || defined(__MACH__)
21 andreas 28
typedef unsigned long int ulong;
29
#endif
30
 
31
extern bool prg_stopped;
32
 
15 andreas 33
class TTimer
34
{
35
    public:
36
        TTimer();
37
        ~TTimer();
38
 
39
        void run();
40
        void run_once();
41
        void run_once(std::chrono::milliseconds ms);
42
        void stop() { mStopped = true; }
43
        void setInterval(std::chrono::milliseconds ms) { mMsec = ms; }
44
        void registerCallback(std::function<void(ulong)> callback) { _callback = callback; }
45
        bool isRunning() { return mRunning; }
46
 
47
    protected:
48
        void _run();
49
 
50
    private:
51
        std::function<void(ulong)> _callback{nullptr};
52
        std::chrono::milliseconds mMsec{0};
53
        bool mOnce{false};
54
        std::thread mThread;
55
        ulong mCounter{0};
93 andreas 56
        std::atomic<bool> mStopped{false};
57
        std::atomic<bool> mRunning{false};
15 andreas 58
};
59
 
60
#endif