amend e09c620; now with semicolon :)
[ardour.git] / libs / pbd / pbd / timer.h
1 /*
2     Copyright (C) 2014 Tim Mayberry
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 2 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
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __libpbd_timer_h__
21 #define __libpbd_timer_h__
22
23 #include <sigc++/signal.h>
24
25 #include <glibmm/main.h>
26
27 #include "pbd/libpbd_visibility.h"
28
29 namespace PBD {
30
31 /**
32  * The Timer class is a wrapper around Glib TimeoutSources
33  * The Timer will start automatically when the first connection
34  * is made and stop when the last callback is disconnected.
35  */
36 class LIBPBD_API Timer
37 {
38 public:
39
40         Timer (unsigned int interval,
41                const Glib::RefPtr<Glib::MainContext>& main_context);
42
43         unsigned int get_interval () const;
44
45         void set_interval (unsigned int new_interval);
46
47         virtual unsigned int connection_count () const = 0;
48
49 protected:
50
51         virtual ~Timer() { }
52
53         void start ();
54
55         void stop ();
56
57         virtual bool on_elapsed () = 0;
58
59 private:
60
61         Timer(const Timer&);
62         Timer& operator= (const Timer&);
63
64 private:
65
66         static gboolean _timeout_handler (void *data);
67
68         bool timeout_handler ();
69
70         GSource*                               m_timeout_source;
71
72         unsigned int                           m_timeout_interval;
73
74         const Glib::RefPtr<Glib::MainContext>  m_main_context;
75
76 };
77
78 class LIBPBD_API StandardTimer : public Timer
79 {
80 public:
81
82         StandardTimer (unsigned int interval,
83                        const Glib::RefPtr<Glib::MainContext>& main_context = Glib::MainContext::get_default());
84
85         sigc::connection connect (const sigc::slot<void>& slot);
86
87         virtual unsigned int connection_count () const
88         { return m_signal.size (); }
89
90 protected:
91
92         virtual bool on_elapsed ();
93
94         sigc::signal<void>                  m_signal;
95
96 };
97
98 class LIBPBD_API BlinkTimer : public Timer
99 {
100 public:
101
102         BlinkTimer (unsigned int interval,
103                     const Glib::RefPtr<Glib::MainContext>& main_context = Glib::MainContext::get_default());
104
105
106         sigc::connection connect (const sigc::slot<void, bool>& slot);
107
108         virtual unsigned int connection_count () const
109         { return m_blink_signal.size (); }
110
111 protected:
112
113         virtual bool on_elapsed ();
114
115         sigc::signal<void, bool> m_blink_signal;
116
117 };
118
119 } // namespace PBD
120
121 #endif // __libpbd_timer_h__