E-MailRelay
geventemitter.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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 geventemitter.h
19///
20
21#ifndef G_NET_EVENT_EMITTER_H
22#define G_NET_EVENT_EMITTER_H
23
24#include "gdef.h"
25#include "geventhandler.h"
26#include "gexceptionsink.h"
27
28namespace GNet
29{
30 class EventEmitter ;
31}
32
33//| \class GNet::EventEmitter
34/// An EventHandler and ExceptionSink tuple, with methods to raise an
35/// event and handle any exceptions. Used in EventLoop implementations.
36/// Also sets an appropriate G::LogOutput context while events are
37/// being handled.
38///
39/// The event loop should normally instantiate a read emitter and
40/// a write emitter for each new file descriptor; any existing
41/// emitters should be update()d rather than destructed and
42/// constructed, with garbage collection once all the events
43/// have been handled.
44///
46{
47public:
48 EventEmitter() noexcept ;
49 ///< Default constructor. The raise methods do nothing and
50 ///< consequently the exception sink is not used.
51 ///< Postcondition: !handler()
52
54 ///< Constructor.
55
57 ///< Calls the EventHandler readEvent() method.
58
60 ///< Calls the EventHandler writeEvent() method.
61
62 void raiseOtherEvent( Descriptor , EventHandler::Reason ) ;
63 ///< Calls the EventHandler otherEvent() method.
64
65 EventHandler * handler() const ;
66 ///< Returns the handler, as passed to the ctor.
67
68 ExceptionSink es() const ;
69 ///< Returns the exception sink, as passed to the ctor.
70
71 void update( EventHandler * , ExceptionSink ) noexcept ;
72 ///< Sets the event handler and the exception sink.
73
74 void reset() noexcept ;
75 ///< Resets the EventHandler so that the raise methods
76 ///< do nothing.
77 ///< Postcondition: !handler()
78
79 void disarm( ExceptionHandler * ) noexcept ;
80 ///< If the exception handler matches then reset it
81 ///< so that it is not called. Any exceptions will be
82 ///< thrown out of of the event loop and back to
83 ///< main().
84
85private:
86 void raiseEvent( void (EventHandler::*method)() , Descriptor ) ;
87 void raiseEvent( void (EventHandler::*method)(EventHandler::Reason) , Descriptor , EventHandler::Reason ) ;
88
89private:
90 EventHandler * m_handler {nullptr} ; // handler for the event
91 ExceptionSink m_es ; // handler for any thrown exception
92 ExceptionSink m_es_saved ; // in case disarm()ed but still needed
93} ;
94
95inline
97{
98 return m_handler ;
99}
100
101inline
103{
104 return m_es ;
105}
106
107#endif
A class that encapsulates a network socket file descriptor and an associated windows event handle.
Definition: gdescriptor.h:37
An EventHandler and ExceptionSink tuple, with methods to raise an event and handle any exceptions.
Definition: geventemitter.h:46
void raiseWriteEvent(Descriptor)
Calls the EventHandler writeEvent() method.
void update(EventHandler *, ExceptionSink) noexcept
Sets the event handler and the exception sink.
EventHandler * handler() const
Returns the handler, as passed to the ctor.
Definition: geventemitter.h:96
void reset() noexcept
Resets the EventHandler so that the raise methods do nothing.
void raiseReadEvent(Descriptor)
Calls the EventHandler readEvent() method.
void disarm(ExceptionHandler *) noexcept
If the exception handler matches then reset it so that it is not called.
EventEmitter() noexcept
Default constructor.
void raiseOtherEvent(Descriptor, EventHandler::Reason)
Calls the EventHandler otherEvent() method.
ExceptionSink es() const
Returns the exception sink, as passed to the ctor.
A base class for classes that have a file descriptor and handle asynchronous events from the event lo...
Definition: geventhandler.h:48
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket/future event...
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
Network classes.
Definition: gdef.h:1144