E-MailRelay
gtimer.cpp
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 // 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 
28  m_time(0)
29 {
30  TimerList::instance().add( *this , eh ) ;
31 }
32 
34 {
35  try
36  {
37  if( TimerList::instance(TimerList::NoThrow()) != nullptr )
38  TimerList::instance().remove( *this ) ;
39  }
40  catch(...) // dtor
41  {
42  }
43 }
44 
46 {
47  // use a magic number for zero-length timers so that
48  // calls to now() can be optimised out
49  return m_time.s == 1 ;
50 }
51 
53 {
54  if( !active() )
55  {
56  return false ;
57  }
58  else if( immediate() )
59  {
60  return true ;
61  }
62  else
63  {
64  if( now.s == 0 ) now = G::DateTime::now() ;
65  return m_time <= now ;
66  }
67 }
68 
69 void GNet::TimerBase::startTimer( unsigned int time , unsigned int time_us )
70 {
71  m_time = (time==0U && time_us==0U) ? G::EpochTime(1) : ( G::DateTime::now() + G::EpochTime(time,time_us) ) ;
72  TimerList::instance().update( *this ) ;
73 }
74 
75 void GNet::TimerBase::startTimer( const G::EpochTime & interval_time )
76 {
77  m_time = (interval_time.s==0 && interval_time.us==0U) ? G::EpochTime(1) : ( G::DateTime::now() + interval_time ) ;
78  TimerList::instance().update( *this ) ;
79 }
80 
82 {
83  if( active() )
84  {
85  m_time = G::EpochTime(0) ;
86  TimerList::instance().update( *this ) ;
87  }
88 }
89 
91 {
92  G_ASSERT( active() ) ;
93  m_time = G::EpochTime(0) ;
94  onTimeout() ;
95 }
96 
98 {
99  return m_time ;
100 }
101 
102 /// \file gtimer.cpp
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
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
Overload discriminator class for TimerList.
Definition: gtimerlist.h:65
void add(TimerBase &, ExceptionHandler &)
Adds a timer.
Definition: gtimerlist.cpp:45
static EpochTime now()
Returns the current epoch time.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
static TimerList & instance()
Singleton access. Throws an exception if none.
Definition: gtimerlist.cpp:145
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
void update(TimerBase &)
Called when a timer changes its value.
Definition: gtimerlist.cpp:77
TimerBase(ExceptionHandler &)
Constructor.
Definition: gtimer.cpp:27
bool expired(G::EpochTime &) const
Used by TimerList.
Definition: gtimer.cpp:52
bool immediate() const
Used by TimerList.
Definition: gtimer.cpp:45
void remove(TimerBase &)
Removes a timer from the list.
Definition: gtimerlist.cpp:51