- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gtimer.cpp
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 // ===
17 //
18 // gtimer.cpp
19 //
20
21 #include "gdef.h"
22 #include "gtimer.h"
23 #include "gevent.h"
24 #include "gdebug.h"
25 #include "gassert.h"
26
27 namespace GNet
28 {
29 class TimerUpdate ;
30 }
31
35 class GNet::TimerUpdate
36 {
37 public:
38 TimerUpdate() ;
39 ~TimerUpdate() ;
40 private:
41 TimerUpdate( const TimerUpdate & ) ; // not implemented
42 void operator=( const TimerUpdate & ) ; // not implemented
43 private:
44 G::DateTime::EpochTime m_old_soonest ;
45 } ;
46
47 // ===
48
49 GNet::AbstractTimer::AbstractTimer() :
50 m_time(0UL)
51 {
52 G_ASSERT( TimerList::instance(TimerList::NoThrow()) != NULL ) ;
53 G_ASSERT( EventLoop::exists() ) ;
54 TimerList::instance().add( *this ) ;
55 // no List::update() here since this timer has not started
56 }
57
58 GNet::AbstractTimer::~AbstractTimer()
59 {
60 try
61 {
62 if( TimerList::instance(TimerList::NoThrow()) != NULL )
63 {
64 TimerUpdate update ;
65 TimerList::instance().remove( *this ) ;
66 // List::update() here
67 }
68 }
69 catch(...) // dtor
70 {
71 }
72 }
73
74 void GNet::AbstractTimer::startTimer( unsigned int time )
75 {
76 TimerUpdate update ;
77 m_time = G::DateTime::now() + time ;
78 // List::update() here
79 }
80
81 void GNet::AbstractTimer::cancelTimer()
82 {
83 TimerUpdate update ;
84 m_time = 0U ;
85 // List::update() here
86 }
87
88 void GNet::AbstractTimer::doTimeout()
89 {
90 G_ASSERT( m_time != 0U ) ;
91 m_time = 0U ;
92
93 try
94 {
95 onTimeout() ;
96 }
97 catch( std::exception & e ) // strategy
98 {
99 G_DEBUG( "GNet::AbstractTimer::doTimeout: exception from timeout handler being passed back: " << e.what() ) ;
100 onTimeoutException( e ) ;
101 }
102 }
103
104 G::DateTime::EpochTime GNet::AbstractTimer::t() const
105 {
106 return m_time ;
107 }
108
109 // ===
110
111 GNet::TimerUpdate::TimerUpdate()
112 {
113 m_old_soonest = TimerList::instance().soonest() ;
114 }
115
116 GNet::TimerUpdate::~TimerUpdate()
117 {
118 try
119 {
120 if( TimerList::instance(TimerList::NoThrow()) != NULL )
121 TimerList::instance().update( m_old_soonest ) ;
122 }
123 catch(...) // dtor
124 {
125 }
126 }
127
GNet::TimerUpdate
A private implementation class used by GNet::AbstractTimer.
Definition: gtimer.cpp:35
G::DateTime::now
static EpochTime now()
Returns the current epoch time.
Definition: gdatetime.cpp:34
GNet::TimerList::remove
void remove(AbstractTimer &)
Removes a timer from the list.
Definition: gtimerlist.cpp:63
GNet::AbstractTimer::~AbstractTimer
virtual ~AbstractTimer()
Destructor.
Definition: gtimer.cpp:58
GNet::TimerList::add
void add(AbstractTimer &)
Adds a timer. Used by Timer::Timer().
Definition: gtimerlist.cpp:57
G::DateTime::EpochTime
std::time_t EpochTime
Definition: gdatetime.h:41
GNet::TimerList::update
void update(G::DateTime::EpochTime previous_soonest)
Called when one of the list's timers has changed.
Definition: gtimerlist.cpp:75
GNet
Network classes.
GNet::TimerList::soonest
G::DateTime::EpochTime soonest() const
Returns the time of the first timer to expire, or zero if none.
Definition: gtimerlist.cpp:104
GNet::AbstractTimer::startTimer
void startTimer(unsigned int time)
Starts the timer.
Definition: gtimer.cpp:74
GNet::TimerList::NoThrow
Overload discriminator class for TimerList.
Definition: gtimerlist.h:47
GNet::TimerUpdate::~TimerUpdate
~TimerUpdate()
Definition: gtimer.cpp:116
GNet::TimerUpdate::TimerUpdate
TimerUpdate()
Definition: gtimer.cpp:111
GNet::AbstractTimer::AbstractTimer
AbstractTimer()
Default constructor.
Definition: gtimer.cpp:49
GNet::TimerList::instance
static TimerList & instance()
Singleton access. Throws an exception if none.
Definition: gtimerlist.cpp:161
G_ASSERT
#define G_ASSERT(test)
Definition: gassert.h:30
gassert.h
gtimer.h
gevent.h
G_DEBUG
#define G_DEBUG(expr)
Definition: glog.h:95
GNet::AbstractTimer::cancelTimer
void cancelTimer()
Cancels the timer.
Definition: gtimer.cpp:81
GNet::EventLoop::exists
static bool exists()
Returns true if an instance exists.
Definition: geventloop.cpp:51
gdebug.h