VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gtimer.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gtimer.h
19 ///
20 
21 #ifndef G_NET_TIMER_H
22 #define G_NET_TIMER_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gdatetime.h"
27 #include "geventhandler.h"
28 #include "gexception.h"
29 #include "gtimerlist.h"
30 
31 namespace GNet
32 {
33  class TimerBase ;
34 }
35 
36 /// \class GNet::TimerBase
37 /// An interface used by GNet::TimerList to keep track of pending timeouts
38 /// and to deliver timeout events. The public methods to start and cancel
39 /// the timer are normally used via GNet::Timer<>.
40 ///
42 {
43 public:
44  virtual ~TimerBase() ;
45  ///< Destructor.
46 
47  void startTimer( unsigned int interval_s , unsigned int interval_us = 0U ) ;
48  ///< Starts the timer so that it goes off after the
49  ///< given time interval.
50 
51  void startTimer( const G::EpochTime & interval_time ) ;
52  ///< Overload for an interval expressed as an G::EpochTime.
53 
54  void cancelTimer() ;
55  ///< Cancels the timer.
56 
57  bool active() const ;
58  ///< Returns true if the timer is started and not cancelled.
59 
60  bool immediate() const ;
61  ///< Returns true if the timer is active() and zero-length.
62 
63 protected:
64  TimerBase() ;
65  ///< Default constructor.
66 
67  virtual void onTimeout() = 0 ;
68  ///< Called when the timer expires (or soon after).
69 
70 private:
71  TimerBase( const TimerBase & ) ; // not implemented
72  void operator=( const TimerBase & ) ; // not implemented
73 
74 private:
75  friend class TimerList ;
76  void doTimeout() ; // friendship -- catches exceptions, but may rethrow
77  G::EpochTime t() const ; // friendship
78  bool expired( G::EpochTime & ) const ; // friendship
79 
80 private:
81  G::EpochTime m_time ;
82 } ;
83 
84 inline
86 {
87  return m_time.s != 0 ;
88 }
89 
90 namespace GNet
91 {
92 
93 /// \class Timer
94 /// A timer class template in which the timeout is delivered to the specified
95 /// method. Any exception thrown out of the timeout handler is delivered to
96 /// the specified EventHandler interface so that it can be handled or rethrown.
97 ///
98 /// Eg:
99 /// \code
100 /// struct Foo : public EventExceptionHandler
101 /// {
102 /// Timer<Foo> m_timer ;
103 /// Foo() : m_timer(*this,&Foo::onTimeout,*this) {}
104 /// void onTimeout() {}
105 /// void onException( std::exception & ) { throw ; }
106 /// } ;
107 /// \endcode
108 ///
109 template <typename T>
110 class Timer : public TimerBase
111 {
112 public:
113  typedef void (T::*method_type)() ;
114 
115  Timer( T & t , method_type m , EventExceptionHandler & event_exception_handler ) ;
116  ///< Constructor. The EventExceptionHandler reference is required in case
117  ///< the timeout handler throws; the onException() implementation
118  ///< can simply rethrow to exit the event loop.
119 
120 protected:
121  virtual void onTimeout() override ;
122  ///< Override from GNet::TimerBase.
123 
124  virtual void onException( std::exception & ) override ;
125  ///< Override from GNet::EventExceptionHandler.
126 
127 private:
128  Timer( const Timer<T> & ) ; // not implemented
129  void operator=( const Timer<T> & ) ; // not implemented
130 
131 private:
132  T & m_t ;
133  method_type m_m ;
134  EventExceptionHandler & m_event_exception_handler ;
135 } ;
136 
137 template <typename T>
138 Timer<T>::Timer( T & t , method_type m , EventExceptionHandler & e ) :
139  m_t(t) ,
140  m_m(m) ,
141  m_event_exception_handler(e)
142 {
143 }
144 
145 template <typename T>
147 {
148  (m_t.*m_m)() ;
149 }
150 
151 template <typename T>
152 void Timer<T>::onException( std::exception & e )
153 {
154  m_event_exception_handler.onException( e ) ;
155 }
156 
157 }
158 
159 #endif
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events and t...
Definition: geventhandler.h:44
bool active() const
Returns true if the timer is started and not cancelled.
Definition: gtimer.h:85
TimerBase()
Default constructor.
Definition: gtimer.cpp:27
A singleton which maintains a list of all Timer objects, and interfaces to the event loop on their be...
Definition: gtimerlist.h:56
virtual void onTimeout()=0
Called when the timer expires (or soon after).
virtual void onTimeout() override
Override from GNet::TimerBase.
Definition: gtimer.h:146
void cancelTimer()
Cancels the timer.
Definition: gtimer.cpp:79
void startTimer(unsigned int interval_s, unsigned int interval_us=0U)
Starts the timer so that it goes off after the given time interval.
Definition: gtimer.cpp:67
virtual ~TimerBase()
Destructor.
Definition: gtimer.cpp:33
virtual void onException(std::exception &) override
Override from GNet::EventExceptionHandler.
Definition: gtimer.h:152
An interface used by GNet::TimerList to keep track of pending timeouts and to deliver timeout events...
Definition: gtimer.h:41
bool immediate() const
Returns true if the timer is active() and zero-length.
Definition: gtimer.cpp:45
Timer(T &t, method_type m, EventExceptionHandler &event_exception_handler)
Constructor.
Definition: gtimer.h:138
A timer class template in which the timeout is delivered to the specified method. ...
Definition: gtimer.h:110