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
29namespace 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
45 m_eh(eh) ,
46 m_esrc(esrc)
47{
48}
49
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
59{
60 return { Private() , nullptr , nullptr } ;
61}
62
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
73{
74 EventState es( *this ) ;
75 es.m_esrc = esrc ;
76 return es ;
77}
78
79void 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
86{
87 m_eh = nullptr ;
88 m_esrc = nullptr ;
89}
90
91// ==
92
94 m_es(es)
95{
96 m_es.esrc( EventState::Private() , nullptr ) ;
97}
98
99GNet::EventState GNet::EventStateUnbound::bind( ExceptionSource * esrc ) const noexcept
100{
101 return m_es.esrc( EventState::Private() , esrc ) ;
102}
103
An interface for GNet classes that define a logging context string.
Definition: geventlogging.h:47
EventStateUnbound(EventState) noexcept
Constructor. See also EventState::unbound().
Definition: geventstate.cpp:93
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
EventState esrc(Private, ExceptionSource *) const noexcept
Returns a copy of this object with the ExceptionSource pointer set.
Definition: geventstate.cpp:72
EventState(Private, ExceptionHandler *eh, ExceptionSource *source) noexcept
Constructor used by event loops etc.
Definition: geventstate.cpp:44
void doOnException(std::exception &e, bool done)
Calls the exception handler's onException() method.
Definition: geventstate.cpp:79
static EventState create()
A factory function for an exception handler that rethrows.
Definition: geventstate.cpp:58
void disarm() noexcept
Resets the exception handler.
Definition: geventstate.cpp:85
EventLogging * logging() const noexcept
Returns the event logging pointer.
Definition: geventstate.h:281
ExceptionSource * esrc() const noexcept
Returns the exception source pointer.
Definition: geventstate.h:269
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket/future event...
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.
A mixin base class that identifies the source of an exception when delivered to GNet::ExceptionHandle...
Network classes.
Definition: gdef.h:1243
Overload discriminator for GNet::EventState.
Definition: geventstate.h:134