VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gtimer.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 ) ;
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  return m_time.s == 1 ;
48 }
49 
50 bool GNet::TimerBase::expired( G::EpochTime & now ) const
51 {
52  if( !active() )
53  {
54  return false ;
55  }
56  else if( immediate() )
57  {
58  return true ;
59  }
60  else
61  {
62  if( now.s == 0 ) now = G::DateTime::now() ;
63  return m_time <= now ;
64  }
65 }
66 
67 void GNet::TimerBase::startTimer( unsigned int time , unsigned int time_us )
68 {
69  m_time = (time==0U && time_us==0U) ? G::EpochTime(1) : ( G::DateTime::now() + G::EpochTime(time,time_us) ) ;
70  TimerList::instance().update( *this ) ;
71 }
72 
73 void GNet::TimerBase::startTimer( const G::EpochTime & interval_time )
74 {
75  m_time = (interval_time.s==0 && interval_time.us==0U) ? G::EpochTime(1) : ( G::DateTime::now() + interval_time ) ;
76  TimerList::instance().update( *this ) ;
77 }
78 
80 {
81  if( active() )
82  {
83  m_time = G::EpochTime(0) ;
84  TimerList::instance().update( *this ) ;
85  }
86 }
87 
88 void GNet::TimerBase::doTimeout()
89 {
90  G_ASSERT( active() ) ;
91  m_time = G::EpochTime(0) ;
92  try
93  {
94  onTimeout() ;
95  }
96  catch( std::exception & e )
97  {
98  G_DEBUG( "GNet::TimerBase::doTimeout: exception from timeout handler being passed back: " << e.what() ) ;
99  onException( e ) ;
100  }
101 }
102 
103 G::EpochTime GNet::TimerBase::t() const
104 {
105  return m_time ;
106 }
107 
108 /// \file gtimer.cpp
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
TimerBase()
Default constructor.
Definition: gtimer.cpp:27
Overload discriminator class for TimerList.
Definition: gtimerlist.h:60
static EpochTime now()
Returns the current epoch time.
static TimerList & instance()
Singleton access. Throws an exception if none.
Definition: gtimerlist.cpp:140
void add(TimerBase &)
Adds a timer.
Definition: gtimerlist.cpp:57
void cancelTimer()
Cancels the timer.
Definition: gtimer.cpp:79
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:67
virtual ~TimerBase()
Destructor.
Definition: gtimer.cpp:33
void update(TimerBase &)
Called when a timer changes its value.
Definition: gtimerlist.cpp:77
bool immediate() const
Returns true if the timer is active() and zero-length.
Definition: gtimer.cpp:45
void remove(TimerBase &)
Removes a timer from the list.
Definition: gtimerlist.cpp:63