E-MailRelay
Public Member Functions | Static Public Member Functions | List of all members
GNet::ExceptionSink Class Reference

A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer. More...

#include <gexceptionsink.h>

Public Member Functions

 ExceptionSink () noexcept
 Default constructor for an exception handler that rethrows. More...
 
 ExceptionSink (ExceptionHandler &eh, ExceptionSource *source) noexcept
 Constructor. More...
 
 ExceptionSink (ExceptionHandler *eh, ExceptionSource *source) noexcept
 Constructor. More...
 
 ExceptionSink (std::nullptr_t, ExceptionSource *)=delete
 Deleted override to prohibit a null ExceptionHandler.
 
ExceptionHandlereh () const noexcept
 Returns the exception handler pointer. More...
 
ExceptionSourceesrc () const noexcept
 Returns the exception source pointer. More...
 
void call (std::exception &e, bool done)
 Calls the exception handler's onException() method. More...
 
void reset () noexcept
 Resets the object as if default constructed. More...
 
bool set () const noexcept
 Returns true if eh() is not null. More...
 

Static Public Member Functions

static ExceptionSink logOnly ()
 A factory function for an exception handler that logs the exception as an error but does not re-throw. More...
 
static ExceptionSink rethrow ()
 A factory function for an exception handler that rethrows. More...
 

Detailed Description

A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.

The EventEmitter and TimerList classes associate an event handler and ExceptionSink with each event source (file descriptor or timer). If the event handler throws an exception then the associated ExceptionHandler's onException() method is called, via ExceptionSink::call().

An onException() implementation normally just rethrows the exception to terminate the event loop, but sometimes the exception can be handled less drastically, perhaps by deleting the object identified as the exception source.

For example, Server objects create and contain ServerPeer objects:

class ServerPeer : public ExceptionSource { ... } ;
class Server : private ExceptionHandler
{
void init() { m_peer = newServerPeer(...) ; }
void onException( ExceptionSource * source ... ) override
{
assert( source == m_peer ) ;
delete source ;
}
ServerPeer * m_peer ;
} ;

The ExceptionSinkUnbound class is used as a device to force factory methods to plumb-in an ExceptionSource pointer to the newly-created object as soon as its address is available (ie. before the constructor body runs).

class FooServerPeer : public ServerPeer
{
public:
FooServerPeer * newServerPeer( ExceptionSinkUnbound esu , ... ) override
{
return new FooServerPeer( esu , ... ) ;
}
private:
FooServerPeer( ExceptionSinkUnbound esu , ... ) :
ServerPeer( esu.bind(this) , ... )
{
}
} ;

So then the ServerPeer constructor has a bound ExceptionSink that it can pass to its timers and other event-handling objects:

m_timer(*this,...,es) ,
m_other(es,...)
{
}
ExceptionSink() noexcept
Default constructor for an exception handler that rethrows.
ServerPeer(ExceptionSink, ServerPeerInfo &&, const LineBuffer::Config &)
Constructor.
Definition: gserverpeer.cpp:30

Definition at line 100 of file gexceptionsink.h.

Constructor & Destructor Documentation

◆ ExceptionSink() [1/3]

GNet::ExceptionSink::ExceptionSink ( )
defaultnoexcept

Default constructor for an exception handler that rethrows.

Postcondition: !set()

◆ ExceptionSink() [2/3]

GNet::ExceptionSink::ExceptionSink ( ExceptionHandler eh,
ExceptionSource source 
)
noexcept

Constructor.

The ExceptionHandler reference must remain valid as the ExceptionSink is copied around. Postcondition: set()

Definition at line 47 of file gexceptionsink.cpp.

◆ ExceptionSink() [3/3]

GNet::ExceptionSink::ExceptionSink ( ExceptionHandler eh,
ExceptionSource source 
)
noexcept

Constructor.

The ExceptionHandler pointer must remain valid as the ExceptionSink is copied around. Precondition: eh != nullptr Postcondition: set()

Definition at line 53 of file gexceptionsink.cpp.

Member Function Documentation

◆ call()

void GNet::ExceptionSink::call ( std::exception &  e,
bool  done 
)

Calls the exception handler's onException() method.

Used by EventEmitter and TimerList when handling an exception thrown from an event handler. Precondition: set()

Definition at line 82 of file gexceptionsink.cpp.

◆ eh()

GNet::ExceptionHandler * GNet::ExceptionSink::eh ( ) const
noexcept

Returns the exception handler pointer.

Definition at line 72 of file gexceptionsink.cpp.

◆ esrc()

GNet::ExceptionSource * GNet::ExceptionSink::esrc ( ) const
noexcept

Returns the exception source pointer.

Definition at line 77 of file gexceptionsink.cpp.

◆ logOnly()

GNet::ExceptionSink GNet::ExceptionSink::logOnly ( )
static

A factory function for an exception handler that logs the exception as an error but does not re-throw.

This can be a convenient alternative to a try/catch block for code that might throw but should not terminate a long-running server process.

Definition at line 59 of file gexceptionsink.cpp.

◆ reset()

void GNet::ExceptionSink::reset ( )
noexcept

Resets the object as if default constructed.

Postcondition: !set()

Definition at line 88 of file gexceptionsink.cpp.

◆ rethrow()

GNet::ExceptionSink GNet::ExceptionSink::rethrow ( )
static

A factory function for an exception handler that rethrows.

Definition at line 66 of file gexceptionsink.cpp.

◆ set()

bool GNet::ExceptionSink::set ( ) const
noexcept

Returns true if eh() is not null.

Definition at line 94 of file gexceptionsink.cpp.


The documentation for this class was generated from the following files: