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 
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 
50  m_time(0UL)
51 {
54  TimerList::instance().add( *this ) ;
55  // no List::update() here since this timer has not started
56 }
57 
59 {
60  try
61  {
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 
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 
112 {
113  m_old_soonest = TimerList::instance().soonest() ;
114 }
115 
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 
A private implementation class used by GNet::AbstractTimer.
Definition: gtimer.cpp:35
static EpochTime now()
Returns the current epoch time.
Definition: gdatetime.cpp:34
void remove(AbstractTimer &)
Removes a timer from the list.
Definition: gtimerlist.cpp:63
virtual ~AbstractTimer()
Destructor.
Definition: gtimer.cpp:58
void add(AbstractTimer &)
Adds a timer. Used by Timer::Timer().
Definition: gtimerlist.cpp:57
std::time_t EpochTime
Definition: gdatetime.h:41
void update(G::DateTime::EpochTime previous_soonest)
Called when one of the list's timers has changed.
Definition: gtimerlist.cpp:75
Network classes.
G::DateTime::EpochTime soonest() const
Returns the time of the first timer to expire, or zero if none.
Definition: gtimerlist.cpp:104
void startTimer(unsigned int time)
Starts the timer.
Definition: gtimer.cpp:74
Overload discriminator class for TimerList.
Definition: gtimerlist.h:47
AbstractTimer()
Default constructor.
Definition: gtimer.cpp:49
static TimerList & instance()
Singleton access. Throws an exception if none.
Definition: gtimerlist.cpp:161
#define G_ASSERT(test)
Definition: gassert.h:30
#define G_DEBUG(expr)
Definition: glog.h:95
void cancelTimer()
Cancels the timer.
Definition: gtimer.cpp:81
static bool exists()
Returns true if an instance exists.
Definition: geventloop.cpp:51