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 // ===
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 namespace GNet
33 {
34 	class TimerList ;
35 	class AbstractTimer ;
36 }
37 
38 /// \class GNet::TimerList
39 /// A singleton which maintains a list of all Timer
40 /// objects, and interfaces to the event loop on their behalf.
41 ///
42 class GNet::TimerList
43 {
44 public:
45 	G_EXCEPTION( NoInstance , "no TimerList instance" ) ;
46 	/// Overload discriminator class for TimerList.
47 	class NoThrow
48 		{} ;
49 
50 	TimerList() ;
51 		///< Default constructor.
52 
53 	~TimerList() ;
54 		///< Destructor. Any expired timers have their timeouts
55 		///< called.
56 
57 	void add( AbstractTimer & ) ;
58 		///< Adds a timer. Used by Timer::Timer().
59 
60 	void remove( AbstractTimer & ) ;
61 		///< Removes a timer from the list. Used by
62 		///< Timer::~Timer().
63 
64 	void update( G::DateTime::EpochTime previous_soonest ) ;
65 		///< Called when one of the list's timers has changed.
66 
67 	G::DateTime::EpochTime soonest() const ;
68 		///< Returns the time of the first timer to expire,
69 		///< or zero if none.
70 
71 	unsigned int interval( bool & infinite ) const ;
72 		///< Returns the interval to the next timer expiry.
73 		///< The 'infinite' value is set to true if there
74 		///< are no timers running.
75 
76 	void doTimeouts() ;
77 		///< Triggers the timeout callbacks of any expired
78 		///< timers. Called by the event loop (GNet::EventLoop).
79 
80 	static TimerList * instance( const NoThrow & ) ;
81 		///< Singleton access. Returns NULL if none.
82 
83 	static TimerList & instance() ;
84 		///< Singleton access. Throws an exception if none.
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
106