E-MailRelay

gexceptionsink.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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 "gassert.h"
26
27 namespace GNet
28{
29 namespace ExceptionSinkImp /// An implementation namespace for GNet::ExceptionSink.
30 {
31 struct RethrowExceptionHandler : public GNet::ExceptionHandler /// An GNet::ExceptionHandler that rethrows.
32 {
33 void onException( GNet::ExceptionSource * , std::exception & , bool ) override
34 {
35 throw ;
36 }
37 } rethrow_exception_handler ;
38 }
39}
40
41 GNet::ExceptionSink::ExceptionSink( Type type , ExceptionSource * ) noexcept :
42 m_eh(type==Type::Null?nullptr:&ExceptionSinkImp::rethrow_exception_handler)
43{
44}
45
46 GNet::ExceptionSink::ExceptionSink( ExceptionHandler & eh , ExceptionSource * esrc ) noexcept :
47 m_eh(&eh) ,
48 m_esrc(esrc)
49{
50}
51
52 GNet::ExceptionSink::ExceptionSink( ExceptionHandler * eh , ExceptionSource * esrc ) noexcept :
53 m_eh(eh) ,
54 m_esrc(esrc)
55{
56}
57
58 GNet::ExceptionHandler * GNet::ExceptionSink::eh() const noexcept
59{
60 return m_eh ;
61}
62
63 GNet::ExceptionSource * GNet::ExceptionSink::esrc() const noexcept
64{
65 return m_esrc ;
66}
67
68 void GNet::ExceptionSink::call( std::exception & e , bool done )
69{
70 if( m_eh != nullptr )
71 {
72 m_eh->onException( m_esrc , e , done ) ;
73 }
74}
75
76 void GNet::ExceptionSink::reset() noexcept
77{
78 m_eh = nullptr ;
79 m_esrc = nullptr ;
80}
81
82 bool GNet::ExceptionSink::set() const noexcept
83{
84 return m_eh != nullptr ;
85}
86
87// ==
88
89 GNet::ExceptionSinkUnbound::ExceptionSinkUnbound( ExceptionHandler & eh ) :
90 m_eh(&eh)
91{
92}
93
94 GNet::ExceptionSinkUnbound::ExceptionSinkUnbound( ExceptionHandler * eh ) :
95 m_eh(eh)
96{
97 G_ASSERT( eh != nullptr ) ;
98}
99
100 GNet::ExceptionSink GNet::ExceptionSinkUnbound::bind( ExceptionSource * source ) const
101{
102 return ExceptionSink{ m_eh , source } ;
103}
104
GNet::ExceptionHandler
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket/future event...
Definition: gexceptionhandler.h:44
GNet::ExceptionSinkUnbound::ExceptionSinkUnbound
ExceptionSinkUnbound(ExceptionHandler *eh)
Constructor.
Definition: gexceptionsink.cpp:94
GNet::ExceptionSinkUnbound::bind
ExceptionSink bind(ExceptionSource *source) const
Returns a sink object with the source pointer set.
Definition: gexceptionsink.cpp:100
GNet::ExceptionSink
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
Definition: gexceptionsink.h:100
GNet::ExceptionSink::eh
ExceptionHandler * eh() const noexcept
Returns the exception handler pointer.
Definition: gexceptionsink.cpp:58
GNet::ExceptionSink::esrc
ExceptionSource * esrc() const noexcept
Returns the exception source pointer.
Definition: gexceptionsink.cpp:63
GNet::ExceptionSink::ExceptionSink
ExceptionSink(Type=Type::Rethrow, ExceptionSource *source=nullptr) noexcept
Constructor.
Definition: gexceptionsink.cpp:41
GNet::ExceptionSink::call
void call(std::exception &e, bool done)
Calls the exception handler's onException() method.
Definition: gexceptionsink.cpp:68
GNet::ExceptionSink::reset
void reset() noexcept
Resets the pointers.
Definition: gexceptionsink.cpp:76
GNet::ExceptionSink::set
bool set() const noexcept
Returns true if eh() is not null.
Definition: gexceptionsink.cpp:82
GNet::ExceptionSource
A mixin base class that identifies the source of an exception when delivered to GNet::ExceptionHandle...
Definition: gexceptionsource.h:40
gassert.h
geventloop.h
gexceptionsink.h
gnetdone.h
GNet
Network classes.
Definition: gdef.h:1115
GNet::ExceptionSinkImp::RethrowExceptionHandler
An GNet::ExceptionHandler that rethrows.
Definition: gexceptionsink.cpp:32
GNet::ExceptionSinkImp::RethrowExceptionHandler::onException
void onException(GNet::ExceptionSource *, std::exception &, bool) override
Called by the event loop when an exception is thrown out of an event loop callback.
Definition: gexceptionsink.cpp:33