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