E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
27 GNet::TimerBase::TimerBase( ExceptionHandler & eh ) :
28 m_time(0)
29 {
30 TimerList::instance().add( *this , eh ) ;
31 }
32
33 GNet::TimerBase::~TimerBase()
34 {
35 try
36 {
37 if( TimerList::instance(TimerList::NoThrow()) != nullptr )
38 TimerList::instance().remove( *this ) ;
39 }
40 catch(...) // dtor
41 {
42 }
43 }
44
45 bool GNet::TimerBase::immediate() const
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
52 bool GNet::TimerBase::expired( G::EpochTime & now ) const
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
81 void GNet::TimerBase::cancelTimer()
82 {
83 if( active() )
84 {
85 m_time = G::EpochTime(0) ;
86 TimerList::instance().update( *this ) ;
87 }
88 }
89
90 void GNet::TimerBase::doTimeout()
91 {
92 G_ASSERT( active() ) ;
93 m_time = G::EpochTime(0) ;
94 onTimeout() ;
95 }
96
97 G::EpochTime GNet::TimerBase::t() const
98 {
99 return m_time ;
100 }
101
102 /// \file gtimer.cpp
G::EpochTime
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
GNet::TimerBase::doTimeout
void doTimeout()
Used by TimerList to execute the onTimeout() callback.
Definition: gtimer.cpp:90
GNet::TimerBase::t
G::EpochTime t() const
Used by TimerList to get the expiry epoch time.
Definition: gtimer.cpp:97
GNet::TimerList::NoThrow
Overload discriminator class for TimerList.
Definition: gtimerlist.h:65
GNet::TimerList::add
void add(TimerBase &, ExceptionHandler &)
Adds a timer.
Definition: gtimerlist.cpp:45
G::DateTime::now
static EpochTime now()
Returns the current epoch time.
Definition: gdatetime_unix.cpp:26
GNet::ExceptionHandler
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
Definition: gexceptionhandler.h:44
GNet::TimerList::instance
static TimerList & instance()
Singleton access. Throws an exception if none.
Definition: gtimerlist.cpp:145
gassert.h
gtimer.h
GNet::TimerBase::cancelTimer
void cancelTimer()
Cancels the timer.
Definition: gtimer.cpp:81
gevent.h
GNet::TimerBase::startTimer
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
GNet::TimerBase::~TimerBase
virtual ~TimerBase()
Destructor.
Definition: gtimer.cpp:33
GNet::TimerList::update
void update(TimerBase &)
Called when a timer changes its value.
Definition: gtimerlist.cpp:77
GNet::TimerBase::TimerBase
TimerBase(ExceptionHandler &)
Constructor.
Definition: gtimer.cpp:27
GNet::TimerBase::expired
bool expired(G::EpochTime &) const
Used by TimerList.
Definition: gtimer.cpp:52
GNet::TimerBase::immediate
bool immediate() const
Used by TimerList.
Definition: gtimer.cpp:45
GNet::TimerList::remove
void remove(TimerBase &)
Removes a timer from the list.
Definition: gtimerlist.cpp:51
gdebug.h