1 #ifndef oxygentimer_h
2 #define oxygentimer_h
3 /*
4 * this file is part of the oxygen gtk engine
5 * Copyright (c) 2010 Hugo Pereira Da Costa <hugo.pereira@free.fr>
6 *
7 * This  library is free  software; you can  redistribute it and/or
8 * modify it  under  the terms  of the  GNU Lesser  General  Public
9 * License  as published  by the Free  Software  Foundation; either
10 * version 2 of the License, or(at your option ) any later version.
11 *
12 * This library is distributed  in the hope that it will be useful,
13 * but  WITHOUT ANY WARRANTY; without even  the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License  along  with  this library;  if not,  write to  the Free
19 * Software Foundation, Inc., 51  Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
21 */
22 
23 #include <glib.h>
24 
25 namespace Oxygen
26 {
27     //! handles gtk timeouts
28     /*! make sure the timer is properly reset at destruction */
29     class Timer
30     {
31 
32         public:
33 
34         //! constructor
Timer(void)35         Timer( void ):
36             _timerId( 0 ),
37             _func( 0L ),
38             _data( 0L )
39         {}
40 
41         //! copy constructor
42         /*! actually does not copy anything, and prints a warning if the other timer is running */
Timer(const Timer & other)43         Timer( const Timer& other ):
44             _timerId( 0 ),
45             _func( 0L ),
46             _data( 0L )
47         {
48             if( other.isRunning() )
49             { g_warning( "Oxygen::Timer::Timer - Copy constructor on running timer called." ); }
50 
51         }
52 
53         //! destructor
~Timer(void)54         virtual ~Timer( void )
55         { if( _timerId ) g_source_remove( _timerId ); }
56 
57         //! start
58         void start( int, GSourceFunc, gpointer );
59 
60         //! stop
stop(void)61         void stop( void )
62         {
63             if( _timerId ) g_source_remove( _timerId );
64             reset();
65         }
66 
67         //! true if running
isRunning(void)68         bool isRunning( void ) const
69         { return _timerId != 0; }
70 
71         protected:
72 
73         //! reset
reset(void)74         void reset( void )
75         {
76             _timerId = 0;
77             _data = 0;
78             _func = 0;
79         }
80 
81         //! delayed update
82         static gboolean timeOut( gpointer );
83 
84         private:
85 
86         // assignment operator is private
87         Timer& operator = (const Timer& )
88         { return *this; }
89 
90         //! timer id
91         int _timerId;
92 
93         //! source function
94         GSourceFunc _func;
95 
96         //! data
97         gpointer _data;
98 
99     };
100 }
101 
102 
103 #endif
104