E-MailRelay

gtimer.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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 "gexceptionsink.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///
40 class GNet::TimerBase
41{
42 protected:
43 explicit TimerBase( ExceptionSink es ) ;
44 ///< Constructor. The ExceptionSink receives an onException()
45 ///< call if the onTimeout() implementation throws.
46
47 public:
48 virtual ~TimerBase() ;
49 ///< Destructor.
50
51 void startTimer( unsigned int interval_s , unsigned int interval_us = 0U ) ;
52 ///< Starts or restarts the timer so that it expires
53 ///< after the given interval.
54
55 void startTimer( const G::TimeInterval & ) ;
56 ///< Starts or restarts the timer so that it expires
57 ///< after the given interval.
58
59 void cancelTimer() ;
60 ///< Cancels the timer. Does nothing if not running.
61
62 bool active() const noexcept ;
63 ///< Returns true if the timer is started and not cancelled.
64
65 bool immediate() const ;
66 ///< Used by TimerList. Returns true if the timer is active()
67 ///< and zero-length.
68
69 void doTimeout() ;
70 ///< Used by TimerList to execute the onTimeout() callback.
71
72 G::TimerTime t() const ;
73 ///< Used by TimerList to get the expiry epoch time. Zero-length
74 ///< timers return a value corresponding to some time in ancient
75 ///< history (1970).
76
77 void adjust( unsigned int us ) ;
78 ///< Used by TimerList to set the fractional part of the expiry
79 ///< time of immediate() timers so that t() is ordered by
80 ///< startTimer() time.
81
82 bool expired( G::TimerTime & ) const ;
83 ///< Used by TimerList. Returns true if expired when compared
84 ///< to the given epoch time.
85
86 protected:
87 virtual void onTimeout() = 0 ;
88 ///< Called when the timer expires (or soon after).
89
90 public:
91 TimerBase( const TimerBase & ) = delete ;
92 TimerBase( TimerBase && ) = delete ;
93 void operator=( const TimerBase & ) = delete ;
94 void operator=( TimerBase && ) = delete ;
95
96 private:
97 static G::TimerTime history() ;
98
99 private:
100 G::TimerTime m_time ;
101} ;
102
103 inline bool GNet::TimerBase::active() const noexcept
104{
105 return !m_time.isZero() ;
106}
107
108 namespace GNet
109{
110
111//| \class Timer
112/// A timer class template in which the timeout is delivered to the specified
113/// method. Any exception thrown out of the timeout handler is delivered to
114/// the specified ExceptionHandler interface so that it can be handled or
115/// rethrown.
116///
117/// Eg:
118/// \code
119/// struct Foo
120/// {
121/// Timer<Foo> m_timer ;
122/// Foo( ExceptionSink es ) : m_timer(*this,&Foo::onTimeout,es) {}
123/// void onTimeout() { throw "oops" ; }
124/// } ;
125/// \endcode
126///
127 template <typename T>
128 class Timer : private TimerBase
129{
130 public:
131 using method_type = void (T::*)() ;
132
133 Timer( T & t , method_type m , ExceptionSink ) ;
134 ///< Constructor.
135
136 void startTimer( unsigned int interval_s , unsigned int interval_us = 0U ) ;
137 ///< Starts or restarts the timer so that it expires
138 ///< after the given interval.
139
140 void startTimer( const G::TimeInterval & ) ;
141 ///< Starts or restarts the timer so that it expires
142 ///< after the given interval.
143
144 void cancelTimer() ;
145 ///< Cancels the timer. Does nothing if not running.
146
147 bool active() const noexcept ;
148 ///< Returns true if the timer is running.
149
150public:
151 ~Timer() override = default ;
152 Timer( const Timer<T> & ) = delete ;
153 Timer( Timer<T> && ) = delete ;
154 void operator=( const Timer<T> & ) = delete ;
155 void operator=( Timer<T> && ) = delete ;
156
157private: // overrides
158 void onTimeout() override ; // Override from GNet::TimerBase.
159
160private:
161 T & m_t ; // callback target object
162 method_type m_m ;
163} ;
164
165template <typename T>
166 Timer<T>::Timer( T & t , method_type m , GNet::ExceptionSink es ) :
167 TimerBase(es) ,
168 m_t(t) ,
169 m_m(m)
170{
171}
172
173 template <typename T>
174 void Timer<T>::startTimer( unsigned int s , unsigned int us )
175{
176 TimerBase::startTimer( s , us ) ;
177}
178
179 template <typename T>
180 void Timer<T>::startTimer( const G::TimeInterval & i )
181{
182 TimerBase::startTimer( i ) ;
183}
184
185 template <typename T>
186 void Timer<T>::cancelTimer()
187{
188 TimerBase::cancelTimer() ;
189}
190
191 template <typename T>
192 bool Timer<T>::active() const noexcept
193{
194 return TimerBase::active() ;
195}
196
197 template <typename T>
198 void Timer<T>::onTimeout()
199{
200 (m_t.*m_m)() ;
201}
202
203}
204
205#endif
GNet::ExceptionSink
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
Definition: gexceptionsink.h:100
GNet::TimerBase
An interface used by GNet::TimerList to keep track of pending timeouts and to deliver timeout events.
Definition: gtimer.h:41
GNet::TimerBase::doTimeout
void doTimeout()
Used by TimerList to execute the onTimeout() callback.
Definition: gtimer.cpp:104
GNet::TimerBase::immediate
bool immediate() const
Used by TimerList.
Definition: gtimer.cpp:83
GNet::TimerBase::cancelTimer
void cancelTimer()
Cancels the timer. Does nothing if not running.
Definition: gtimer.cpp:94
GNet::TimerBase::TimerBase
TimerBase(ExceptionSink es)
Constructor.
Definition: gtimer.cpp:29
GNet::TimerBase::onTimeout
virtual void onTimeout()=0
Called when the timer expires (or soon after).
GNet::TimerBase::startTimer
void startTimer(unsigned int interval_s, unsigned int interval_us=0U)
Starts or restarts the timer so that it expires after the given interval.
Definition: gtimer.cpp:64
GNet::TimerBase::expired
bool expired(G::TimerTime &) const
Used by TimerList.
Definition: gtimer.cpp:47
GNet::TimerBase::active
bool active() const noexcept
Returns true if the timer is started and not cancelled.
Definition: gtimer.h:103
GNet::TimerBase::t
G::TimerTime t() const
Used by TimerList to get the expiry epoch time.
Definition: gtimer.cpp:111
GNet::TimerBase::~TimerBase
virtual ~TimerBase()
Destructor.
Definition: gtimer.cpp:35
GNet::TimerBase::adjust
void adjust(unsigned int us)
Used by TimerList to set the fractional part of the expiry time of immediate() timers so that t() is ...
Definition: gtimer.cpp:88
GNet::Timer
A timer class template in which the timeout is delivered to the specified method.
Definition: gtimer.h:129
GNet::Timer::startTimer
void startTimer(unsigned int interval_s, unsigned int interval_us=0U)
Starts or restarts the timer so that it expires after the given interval.
Definition: gtimer.h:174
GNet::Timer::startTimer
void startTimer(const G::TimeInterval &)
Starts or restarts the timer so that it expires after the given interval.
Definition: gtimer.h:180
GNet::Timer::cancelTimer
void cancelTimer()
Cancels the timer. Does nothing if not running.
Definition: gtimer.h:186
GNet::Timer::active
bool active() const noexcept
Returns true if the timer is running.
Definition: gtimer.h:192
GNet::Timer::Timer
Timer(T &t, method_type m, ExceptionSink)
Constructor.
Definition: gtimer.h:166
G::TimeInterval
An interval between two G::SystemTime values or two G::TimerTime values.
Definition: gdatetime.h:289
G::TimerTime
A monotonically increasing subsecond-resolution timestamp, notionally unrelated to time_t.
Definition: gdatetime.h:213
G::TimerTime::isZero
bool isZero() const noexcept
Returns true if zero().
Definition: gdatetime.h:279
gdatetime.h
geventhandler.h
gexception.h
gexceptionsink.h
GNet
Network classes.
Definition: gdef.h:1115