VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gtimerlist.h
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 /// \file gtimerlist.h
19 ///
20 
21 #ifndef G_NET_TIMER_LIST_H
22 #define G_NET_TIMER_LIST_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gdatetime.h"
27 #include "geventhandler.h"
28 #include "gexception.h"
29 #include <list>
30 
31 namespace GNet
32 {
33  class TimerList ;
34  class TimerBase ;
35 }
36 
37 /// \class GNet::TimerList
38 /// A singleton which maintains a list of all Timer objects, and interfaces
39 /// to the event loop on their behalf.
40 ///
41 /// Event loops that can do a timed-wait should call TimerList::interval()
42 /// to determine how long to wait before the first G::Timer goes off.
43 /// If the timed-wait times-out or if the interval was zero then they must
44 /// call TimerList::doTimeouts().
45 ///
46 /// Event loops that cannot do timed waits efficiently (ie. the traditional
47 /// Windows message queue) have their setTimeout() method called by this
48 /// class and they are expected to call doTimeouts() when the event-loop
49 /// timer expires.
50 ///
51 /// In both models there can be a race where this class sees no expired
52 /// timers in doTimeouts(), perhaps because the system clock is being
53 /// stretched. However, the next interval() or setTimer() time will be
54 /// very small and the race will resolve itself naturally.
55 ///
57 {
58 public:
59  G_EXCEPTION( NoInstance , "no TimerList instance" ) ;
60  class NoThrow /// Overload discriminator class for TimerList.
61  {} ;
62 
63  TimerList() ;
64  ///< Default constructor.
65 
66  ~TimerList() ;
67  ///< Destructor. Any expired timers have their timeouts called.
68 
69  void add( TimerBase & ) ;
70  ///< Adds a timer.
71 
72  void remove( TimerBase & ) ;
73  ///< Removes a timer from the list.
74 
75  void update( TimerBase & ) ;
76  ///< Called when a timer changes its value.
77 
78  G::EpochTime interval( bool & infinite ) const ;
79  ///< Returns the interval to the first timer expiry. The 'infinite'
80  ///< value is set to true if there are no timers running.
81 
82  void doTimeouts() ;
83  ///< Triggers the timeout callbacks of any expired timers.
84  ///< Called by the event loop (GNet::EventLoop).
85 
86  static TimerList * instance( const NoThrow & ) ;
87  ///< Singleton access. Returns nullptr if none.
88 
89  static TimerList & instance() ;
90  ///< Singleton access. Throws an exception if none.
91 
92  std::string report() const ;
93  ///< Returns a line of text reporting the status of the timer list.
94  ///< Used in debugging and diagnostics.
95 
96 private:
97  TimerList( const TimerList & ) ;
98  void operator=( const TimerList & ) ;
99  G::EpochTime soonestTime() const ;
100  TimerBase * findSoonest() ;
101  void collectGarbage() ;
102  void setTimeout() ;
103 
104 private:
105  typedef std::list<TimerBase*> List ;
106  static TimerList * m_this ;
107  TimerBase * m_soonest ;
108  bool m_run_on_destruction ;
109  List m_list ;
110 } ;
111 
112 #endif
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
TimerList()
Default constructor.
Definition: gtimerlist.cpp:32
Overload discriminator class for TimerList.
Definition: gtimerlist.h:60
A singleton which maintains a list of all Timer objects, and interfaces to the event loop on their be...
Definition: gtimerlist.h:56
static TimerList & instance()
Singleton access. Throws an exception if none.
Definition: gtimerlist.cpp:140
G::EpochTime interval(bool &infinite) const
Returns the interval to the first timer expiry.
Definition: gtimerlist.cpp:114
void add(TimerBase &)
Adds a timer.
Definition: gtimerlist.cpp:57
void doTimeouts()
Triggers the timeout callbacks of any expired timers.
Definition: gtimerlist.cpp:148
~TimerList()
Destructor. Any expired timers have their timeouts called.
Definition: gtimerlist.cpp:40
void update(TimerBase &)
Called when a timer changes its value.
Definition: gtimerlist.cpp:77
An interface used by GNet::TimerList to keep track of pending timeouts and to deliver timeout events...
Definition: gtimer.h:41
std::string report() const
Returns a line of text reporting the status of the timer list.
Definition: gtimerlist.cpp:180