E-MailRelay

geventstate.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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 geventstate.cpp
19///
20
21#include "gdef.h"
22#include "gnetdone.h"
23#include "geventstate.h"
24#include "geventloop.h"
25#include "glog.h"
26#include "gassert.h"
27#include <exception>
28
29 namespace GNet
30{
31 namespace EventStateImp
32 {
33 struct LogExceptionHandler : ExceptionHandler /// A GNet::ExceptionHandler that just logs.
34 {
35 void onException( ExceptionSource * , std::exception & e , bool net_done ) override
36 {
37 if( !net_done )
38 G_LOG( "GNet::EventState: exception: " << e.what() ) ;
39 }
40 } ;
41 }
42}
43
44 GNet::EventState::EventState( Private , ExceptionHandler * eh , ExceptionSource * esrc ) noexcept :
45 m_eh(eh) ,
46 m_esrc(esrc)
47{
48}
49
50 GNet::EventState GNet::EventState::create( std::nothrow_t )
51{
52 static EventStateImp::LogExceptionHandler log_only_exception_handler ;
53 EventState es( Private() , nullptr , nullptr ) ;
54 es.m_eh = &log_only_exception_handler ;
55 return es ;
56}
57
58 GNet::EventState GNet::EventState::create()
59{
60 return { Private() , nullptr , nullptr } ;
61}
62
63 GNet::EventState GNet::EventState::logging( EventLogging * logging ) const noexcept
64{
65 // note that in normal usage the logging pointer will be valid but its ctor may not have run
66 G_ASSERT( logging != nullptr ) ;
67 EventState copy( *this ) ;
68 copy.m_logging = logging ;
69 return copy ;
70}
71
72 GNet::EventState GNet::EventState::esrc( Private , ExceptionSource * esrc ) const noexcept
73{
74 EventState es( *this ) ;
75 es.m_esrc = esrc ;
76 return es ;
77}
78
79 void GNet::EventState::doOnException( std::exception & e , bool done )
80{
81 G_ASSERT( m_eh != nullptr ) ; // precondition -- see EventEmitter and TimerList
82 m_eh->onException( m_esrc , e , done ) ;
83}
84
85 void GNet::EventState::disarm() noexcept
86{
87 m_eh = nullptr ;
88 m_esrc = nullptr ;
89}
90
91// ==
92
93 GNet::EventStateUnbound::EventStateUnbound( EventState es ) noexcept :
94 m_es(es)
95{
96 m_es.esrc( EventState::Private() , nullptr ) ;
97}
98
99 GNet::EventState GNet::EventStateUnbound::bind( ExceptionSource * esrc ) const noexcept
100{
101 return m_es.esrc( EventState::Private() , esrc ) ;
102}
103
GNet::EventLogging
An interface for GNet classes that define a logging context string.
Definition: geventlogging.h:47
GNet::EventStateUnbound::EventStateUnbound
EventStateUnbound(EventState) noexcept
Constructor. See also EventState::unbound().
Definition: geventstate.cpp:93
GNet::EventState
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
GNet::EventState::esrc
EventState esrc(Private, ExceptionSource *) const noexcept
Returns a copy of this object with the ExceptionSource pointer set.
Definition: geventstate.cpp:72
GNet::EventState::EventState
EventState(Private, ExceptionHandler *eh, ExceptionSource *source) noexcept
Constructor used by event loops etc.
Definition: geventstate.cpp:44
GNet::EventState::doOnException
void doOnException(std::exception &e, bool done)
Calls the exception handler's onException() method.
Definition: geventstate.cpp:79
GNet::EventState::create
static EventState create()
A factory function for an exception handler that rethrows.
Definition: geventstate.cpp:58
GNet::EventState::disarm
void disarm() noexcept
Resets the exception handler.
Definition: geventstate.cpp:85
GNet::EventState::logging
EventLogging * logging() const noexcept
Returns the event logging pointer.
Definition: geventstate.h:281
GNet::EventState::esrc
ExceptionSource * esrc() const noexcept
Returns the exception source pointer.
Definition: geventstate.h:269
GNet::ExceptionHandler
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket/future event...
Definition: gexceptionhandler.h:44
GNet::ExceptionHandler::onException
virtual void onException(ExceptionSource *source, std::exception &e, bool done)=0
Called by the event loop when an exception is thrown out of an event loop callback.
GNet::ExceptionSource
A mixin base class that identifies the source of an exception when delivered to GNet::ExceptionHandle...
Definition: gexceptionsource.h:39
gassert.h
geventloop.h
geventstate.h
glog.h
gnetdone.h
GNet
Network classes.
Definition: gdef.h:1243
GNet::EventState::Private
Overload discriminator for GNet::EventState.
Definition: geventstate.h:134