E-MailRelay
gexceptionsink.cpp
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 gexceptionsink.cpp
19///
20
21#include "gdef.h"
22#include "gnetdone.h"
23#include "gexceptionsink.h"
24#include "geventloop.h"
25#include "glog.h"
26#include "gassert.h"
27#include <exception>
28
29namespace GNet
30{
31 namespace ExceptionSinkImp
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::ExceptionSink: exception: " << e.what() ) ;
39 }
40 } ;
41 }
42}
43
45= default ;
46
48 m_eh(&eh) ,
49 m_esrc(esrc)
50{
51}
52
54 m_eh(eh) ,
55 m_esrc(esrc)
56{
57}
58
60{
61 static ExceptionSinkImp::LogExceptionHandler log_only_exception_handler ;
62 return { log_only_exception_handler , nullptr } ;
63}
64
65#ifndef G_LIB_SMALL
67{
68 return {} ;
69}
70#endif
71
73{
74 return m_eh ;
75}
76
78{
79 return m_esrc ;
80}
81
82void GNet::ExceptionSink::call( std::exception & e , bool done )
83{
84 G_ASSERT( m_eh != nullptr ) ; // precondition -- see EventEmitter and TimerList
85 m_eh->onException( m_esrc , e , done ) ;
86}
87
89{
90 m_eh = nullptr ;
91 m_esrc = nullptr ;
92}
93
94bool GNet::ExceptionSink::set() const noexcept
95{
96 return m_eh != nullptr ;
97}
98
99// ==
100
101#ifndef G_LIB_SMALL
103 m_eh(&eh)
104{
105}
106#endif
107
109 m_eh(eh)
110{
111 G_ASSERT( eh != nullptr ) ;
112}
113
115{
116 return { m_eh , source } ;
117}
118
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.
ExceptionSinkUnbound(ExceptionHandler *eh)
Constructor.
ExceptionSink bind(ExceptionSource *source) const
Returns a sink object with the source pointer set.
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
ExceptionHandler * eh() const noexcept
Returns the exception handler pointer.
ExceptionSource * esrc() const noexcept
Returns the exception source pointer.
void call(std::exception &e, bool done)
Calls the exception handler's onException() method.
void reset() noexcept
Resets the object as if default constructed.
bool set() const noexcept
Returns true if eh() is not null.
static ExceptionSink logOnly()
A factory function for an exception handler that logs the exception as an error but does not re-throw...
ExceptionSink() noexcept
Default constructor for an exception handler that rethrows.
static ExceptionSink rethrow()
A factory function for an exception handler that rethrows.
A mixin base class that identifies the source of an exception when delivered to GNet::ExceptionHandle...
Network classes.
Definition: gdef.h:1144