VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
geventloop.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 geventloop.h
19 ///
20 
21 #ifndef G_EVENT_LOOP_H
22 #define G_EVENT_LOOP_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "geventhandler.h"
27 #include "gexception.h"
28 #include "gdatetime.h"
29 #include "gdescriptor.h"
30 #include "gsignalsafe.h"
31 #include <list>
32 #include <string>
33 
34 namespace GNet
35 {
36  class EventLoop ;
37 }
38 
39 /// \class GNet::EventLoop
40 /// An abstract base class for a singleton that keeps track of open sockets
41 /// and their associated handlers. Derived classes are used to implement
42 /// different event loops, such as select() or WinSock.
43 ///
44 /// In practice sockets are added and removed from the class by calling
45 /// GNet::Socket::addReadHandler() etc rather than EventLoop::addRead().
46 /// This is to improve the encapsulation of the GNet::Descriptor data type
47 /// within Socket.
48 ///
49 /// The class has a static member for finding an instance, but instances
50 /// are not created automatically.
51 ///
53 {
54 public:
55  G_EXCEPTION( Error , "failed to initialise the event loop" ) ;
56  G_EXCEPTION( NoInstance , "no event loop instance" ) ;
57 
58 protected:
59  EventLoop() ;
60  ///< Constructor.
61 
62  struct Running /// RAII class that sets a boolean, for implementations of run()/running().
63  {
64  Running( bool & bref ) : m_bref(bref) { m_bref = true ; }
65  ~Running() { m_bref = false ; }
66  bool & m_bref ;
67  } ;
68 
69 public:
70  static EventLoop * create() ;
71  ///< A factory method which creates an instance of a derived
72  ///< class on the heap. Throws on error.
73 
74  static EventLoop & instance() ;
75  ///< Returns a reference to an instance of the class,
76  ///< if any. Throws if none. Does not do any instantiation
77  ///< itself.
78 
79  static bool exists() ;
80  ///< Returns true if an instance exists.
81 
82  static void stop( const G::SignalSafe & ) ;
83  ///< Calls quit() on instance().
84 
85  virtual ~EventLoop() ;
86  ///< Destructor.
87 
88  virtual std::string run() = 0 ;
89  ///< Runs the main event loop. Returns a quit() reason,
90  ///< if any.
91 
92  virtual bool running() const = 0 ;
93  ///< Returns true if called from within run().
94 
95  virtual void quit( std::string reason ) = 0 ;
96  ///< Causes run() to return (once the call stack has
97  ///< unwound). If there are multiple quit()s before
98  ///< run() returns then the latest reason is used.
99 
100  virtual void quit( const G::SignalSafe & ) = 0 ;
101  ///< A signal-safe overload to quit() the event loop.
102 
103  virtual void addRead( Descriptor fd , EventHandler & handler ) = 0 ;
104  ///< Adds the given event source descriptor and associated
105  ///< handler to the read list.
106  ///< See also Socket::addReadHandler().
107 
108  virtual void addWrite( Descriptor fd , EventHandler & handler ) = 0 ;
109  ///< Adds the given event source descriptor and associated
110  ///< handler to the write list.
111  ///< See also Socket::addWriteHandler().
112 
113  virtual void addException( Descriptor fd , EventHandler & handler ) = 0 ;
114  ///< Adds the given event source descriptor and associated
115  ///< handler to the exception list.
116  ///< See also Socket::addExceptionHandler().
117 
118  virtual void dropRead( Descriptor fd ) = 0 ;
119  ///< Removes the given event source descriptor from the
120  ///< list of read sources.
121  ///< See also Socket::dropReadHandler().
122 
123  virtual void dropWrite( Descriptor fd ) = 0 ;
124  ///< Removes the given event source descriptor from the
125  ///< list of write sources.
126  ///< See also Socket::dropWriteHandler().
127 
128  virtual void dropException( Descriptor fd ) = 0 ;
129  ///< Removes the given event source descriptor from the
130  ///< list of exception sources.
131  ///< See also Socket::dropExceptionHandler().
132 
133  virtual void setTimeout( G::EpochTime t ) = 0 ;
134  ///< Used by GNet::TimerList. Sets the absolute time at which
135  ///< TimerList::doTimeouts() is to be called. A zero time
136  ///< is used if there are no active timers.
137 
138  virtual std::string report() const = 0 ;
139  ///< Returns a line of text reporting the status of the event loop.
140  ///< Used in debugging and diagnostics.
141 
142 private:
143  static EventLoop * m_this ;
144 } ;
145 
146 #endif
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
virtual void setTimeout(G::EpochTime t)=0
Used by GNet::TimerList.
static void stop(const G::SignalSafe &)
Calls quit() on instance().
Definition: geventloop.cpp:56
virtual void addWrite(Descriptor fd, EventHandler &handler)=0
Adds the given event source descriptor and associated handler to the write list.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:36
An abstract base class for a singleton that keeps track of open sockets and their associated handlers...
Definition: geventloop.h:52
virtual void dropRead(Descriptor fd)=0
Removes the given event source descriptor from the list of read sources.
A class that encapsulates a network file descriptor and hides knowledge of its o/s-spefific error val...
Definition: gdescriptor.h:37
virtual void dropException(Descriptor fd)=0
Removes the given event source descriptor from the list of exception sources.
static EventLoop * create()
A factory method which creates an instance of a derived class on the heap.
virtual void addRead(Descriptor fd, EventHandler &handler)=0
Adds the given event source descriptor and associated handler to the read list.
virtual bool running() const =0
Returns true if called from within run().
virtual std::string run()=0
Runs the main event loop.
virtual void addException(Descriptor fd, EventHandler &handler)=0
Adds the given event source descriptor and associated handler to the exception list.
A base class for classes that handle asynchronous events from the event loop.
Definition: geventhandler.h:78
EventLoop()
Constructor.
Definition: geventloop.cpp:29
RAII class that sets a boolean, for implementations of run()/running().
Definition: geventloop.h:62
virtual std::string report() const =0
Returns a line of text reporting the status of the event loop.
static bool exists()
Returns true if an instance exists.
Definition: geventloop.cpp:51
virtual ~EventLoop()
Destructor.
Definition: geventloop.cpp:37
virtual void dropWrite(Descriptor fd)=0
Removes the given event source descriptor from the list of write sources.
static EventLoop & instance()
Returns a reference to an instance of the class, if any.
Definition: geventloop.cpp:43
virtual void quit(std::string reason)=0
Causes run() to return (once the call stack has unwound).