E-MailRelay
Public Types | 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 Types

enum class  Type { Null , Rethrow , Log }
 

Public Member Functions

 ExceptionSink (Type=Type::Rethrow, ExceptionSource *source=nullptr) noexcept
 Constructor. 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 pointers. More...
 
bool set () const noexcept
 Returns true if eh() is not null. More...
 

Detailed Description

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

The EventHandlerList 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 syntactic sugar to force factory methods to supply an ExceptionSource pointer that points to the most-derived ServerPeer class.

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 etc. and to the event-handler list:

m_timer(..., es)
{
EventHandlerList::instance().add( es , ... ) ;
}
ExceptionSink(Type=Type::Rethrow, ExceptionSource *source=nullptr) noexcept
Constructor.
ServerPeer(ExceptionSink, const ServerPeerInfo &, const LineBufferConfig &)
Constructor.
Definition: gserverpeer.cpp:28

Definition at line 99 of file gexceptionsink.h.

Member Enumeration Documentation

◆ Type

enum class GNet::ExceptionSink::Type
strong

Definition at line 102 of file gexceptionsink.h.

Constructor & Destructor Documentation

◆ ExceptionSink() [1/3]

GNet::ExceptionSink::ExceptionSink ( Type  type = Type::Rethrow,
ExceptionSource source = nullptr 
)
explicitnoexcept

Constructor.

Definition at line 41 of file gexceptionsink.cpp.

◆ ExceptionSink() [2/3]

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

Constructor.

The ExceptionHandler reference must remain valid as the ExceptionSink is copied around.

Definition at line 46 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.

Definition at line 52 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 EventHandlerList and TimerList. Exceptions thrown out of the onException() implementation are allowed to propagate.

Definition at line 68 of file gexceptionsink.cpp.

◆ eh()

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

Returns the exception handler pointer.

Definition at line 58 of file gexceptionsink.cpp.

◆ esrc()

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

Returns the exception source pointer.

Definition at line 63 of file gexceptionsink.cpp.

◆ reset()

void GNet::ExceptionSink::reset ( )
noexcept

Resets the pointers.

Definition at line 76 of file gexceptionsink.cpp.

◆ set()

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

Returns true if eh() is not null.

Definition at line 82 of file gexceptionsink.cpp.


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