gtimerlist.h
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 // ===
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 
32 namespace GNet
33 {
34  class TimerList ;
35  class AbstractTimer ;
36 }
37 
43 {
44 public:
45  G_EXCEPTION( NoInstance , "no TimerList instance" ) ;
47  class NoThrow
48  {} ;
49 
50  TimerList() ;
52 
53  ~TimerList() ;
56 
57  void add( AbstractTimer & ) ;
59 
60  void remove( AbstractTimer & ) ;
63 
64  void update( G::DateTime::EpochTime previous_soonest ) ;
66 
70 
71  unsigned int interval( bool & infinite ) const ;
75 
76  void doTimeouts() ;
79 
80  static TimerList * instance( const NoThrow & ) ;
82 
83  static TimerList & instance() ;
85 
86 private:
87  TimerList( const TimerList & ) ; // not implemented
88  void operator=( const TimerList & ) ; // not implemented
89  void collectGarbage() ;
90  G::DateTime::EpochTime soonest( int ) const ; // fast overload
91  void update() ;
92  bool valid() const ;
93 
94 private:
95  static TimerList * m_this ;
96  typedef std::list<AbstractTimer*> List ;
97  bool m_run_on_destruction ;
98  List m_list ;
99  bool m_list_changed ;
100  bool m_empty_set_timeout_hint ;
101  bool m_soonest_changed ; // mutable
102  G::DateTime::EpochTime m_soonest ;
103 } ;
104 
105 #endif
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
TimerList()
Default constructor.
Definition: gtimerlist.cpp:29
Overload discriminator class for TimerList.
Definition: gtimerlist.h:47
A singleton which maintains a list of all Timer objects, and interfaces to the event loop on their be...
Definition: gtimerlist.h:42
unsigned int interval(bool &infinite) const
Returns the interval to the next timer expiry.
Definition: gtimerlist.cpp:141
static TimerList & instance()
Singleton access. Throws an exception if none.
Definition: gtimerlist.cpp:161
void doTimeouts()
Triggers the timeout callbacks of any expired timers.
Definition: gtimerlist.cpp:169
~TimerList()
Destructor.
Definition: gtimerlist.cpp:40
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
A timer base class that calls a pure virtual method on expiry.
Definition: gtimer.h:41