Subversion Repositories tpanel

Rev

Rev 446 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
446 andreas 1
/*
486 andreas 2
 * Copyright (C) 2021 to 2025 by Andreas Theofilu <andreas@theosys.at>
446 andreas 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
#include <atomic>
26
 
27
typedef unsigned long int ulong;
28
 
29
extern bool prg_stopped;
30
 
31
class TTimer
32
{
33
    public:
34
        TTimer();
35
        ~TTimer();
36
 
37
        void run();
38
        void run_once();
39
        void run_once(std::chrono::milliseconds ms);
40
        void stop() { mStopped = true; }
41
        void setInterval(std::chrono::milliseconds ms) { mMsec = ms; }
42
        void registerCallback(std::function<void(ulong)> callback) { _callback = callback; }
43
        bool isRunning() { return mRunning; }
44
 
45
    protected:
46
        void _run();
47
 
48
    private:
49
        std::function<void(ulong)> _callback{nullptr};
50
        std::chrono::milliseconds mMsec{0};
51
        bool mOnce{false};
52
        std::thread mThread;
53
        ulong mCounter{0};
54
        std::atomic<bool> mStopped{false};
55
        std::atomic<bool> mRunning{false};
56
};
57
 
58
#endif