- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gtimer.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 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 // ===
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
32 namespace GNet
33 {
34 class AbstractTimer ;
35 }
36
41 class GNet::AbstractTimer
42 {
43 public:
44 virtual ~AbstractTimer() ;
46
47 void startTimer( unsigned int time ) ;
49
50 void cancelTimer() ;
52
53 protected:
54 AbstractTimer() ;
56
57 virtual void onTimeout() = 0 ;
59
60 virtual void onTimeoutException( std::exception & ) = 0 ;
66
67 private:
68 AbstractTimer( const AbstractTimer & ) ; // not implemented
69 void operator=( const AbstractTimer & ) ; // not implemented
70
71 private:
72 friend class TimerList ;
73 void doTimeout() ; // called by friendly TimerList
74 G::DateTime::EpochTime t() const ; // called by friendly TimerList
75
76 private:
77 G::DateTime::EpochTime m_time ;
78 } ;
79
81 namespace GNet
82 {
83
102 template <typename T>
103 class Timer : public AbstractTimer
104 {
105 public:
106 typedef void (T::*method_type)() ;
107
108 Timer( T & t , method_type m , EventHandler & exception_handler ) ;
111
112 protected:
113 virtual void onTimeout() ;
115
116 virtual void onTimeoutException( std::exception & ) ;
118
119 private:
120 Timer( const Timer<T> & ) ; // not implemented
121 void operator=( const Timer<T> & ) ; // not implemented
122
123 private:
124 T & m_t ;
125 method_type m_m ;
126 EventHandler & m_event_handler ;
127 } ;
128
129 template <typename T>
130 Timer<T>::Timer( T & t , method_type m , EventHandler & e ) :
131 m_t(t) ,
132 m_m(m) ,
133 m_event_handler(e)
134 {
135 }
136
137 template <typename T>
138 void Timer<T>::onTimeout()
139 {
140 (m_t.*m_m)() ;
141 }
142
143 template <typename T>
144 void Timer<T>::onTimeoutException( std::exception & e )
145 {
146 m_event_handler.onException( e ) ;
147 }
148
149 }
150
151 #endif
GNet::AbstractTimer::~AbstractTimer
virtual ~AbstractTimer()
Destructor.
Definition: gtimer.cpp:58
GNet::Timer::method_type
void(T::* method_type)()
Definition: gtimer.h:106
G::DateTime::EpochTime
std::time_t EpochTime
Definition: gdatetime.h:41
GNet::Timer::onTimeoutException
virtual void onTimeoutException(std::exception &)
Final override from GNet::AbstractTimer.
Definition: gtimer.h:144
GNet
Network classes.
GNet::Timer::onTimeout
virtual void onTimeout()
Final override from GNet::AbstractTimer.
Definition: gtimer.h:138
gtimerlist.h
GNet::AbstractTimer::startTimer
void startTimer(unsigned int time)
Starts the timer.
Definition: gtimer.cpp:74
GNet::TimerList
A singleton which maintains a list of all Timer objects, and interfaces to the event loop on their be...
Definition: gtimerlist.h:42
GNet::AbstractTimer::AbstractTimer
AbstractTimer()
Default constructor.
Definition: gtimer.cpp:49
geventhandler.h
GNet::EventHandler
A base class for classes that handle asynchronous socket events.
Definition: geventhandler.h:54
gdatetime.h
gexception.h
GNet::AbstractTimer::onTimeoutException
virtual void onTimeoutException(std::exception &)=0
Called by the event loop when the onTimeout() override throws.
GNet::AbstractTimer::cancelTimer
void cancelTimer()
Cancels the timer.
Definition: gtimer.cpp:81
GNet::Timer
A timer class template in which the timeout is delivered to the specified method. ...
Definition: gtimer.h:103
GNet::AbstractTimer::onTimeout
virtual void onTimeout()=0
Called when the timer expires (or soon after).
GNet::Timer::Timer
Timer(T &t, method_type m, EventHandler &exception_handler)
Constructor.
Definition: gtimer.h:130
GNet::AbstractTimer
A timer base class that calls a pure virtual method on expiry.
Definition: gtimer.h:41