Boost.Test > Components > The Execution Monitor > boost::execution_monitor
Boost Test logo

boost::execution_monitor

boost::execution_monitor uniformly detects and reports the occurrence of several types of signals and exceptions, reducing various errors to a uniform boost::execution_exception that is returned to a caller.

class execution_monitor {
public:
    int  execute( unit_test::callback0<int> const& F, bool catch_system_errors = true, int timeout = 0 );

    template<typename Exception, typename ExceptionTranslator>
    void register_exception_translator( ExceptionTranslator const& tr, boost::type<Exception>* = 0 );
}; // exception monitor

execution_monitor::execute( unit_test::callback0<int> const& F, bool catch_system_errors, int timeout )

Effects:

Calls the zero arity function F inside a try/catch block which may also include other unspecified platform dependent error detection code. Throws boost::execution_exception on an uncaught C++ exception, a timeout alarm and, a hardware or software signal, trap, or other exception. execution_monitor::execute() doesn't consider it an error for the function F to return a non-zero value.

Arguments:

F - zero arity function to be monitored
catch_system_exception - boolean flag that indicates whether system level errors/signals needs to be traped. If this flag is false program will crash if system level error occur. Default value is true.
timeout - a time-out value that specifies seconds that elapse before a timeout error occurs. Note though, that in current implementation timeout support is limited and may be ignored on some platforms. By default there is no timeout for the monitored call.

Returns:

The integer value returned by the function F or boost::exit_exception_failure in the case of caught exception.

execution_monitor::register_exception_translator<Exception>( ExceptionTranslator const& tr )

Effects:

Registers translator function tr for an exception of type Exception. Translators gets chained, so you can register as many as you want. The Exception type needs to be specified explicitly as the member function template argument. The translator function gets called when an exception of type Exception is thrown from within the monitored function. The translator recieves a thrown exception object as its first argument. Result value of translator is ignored and no exception is reported if this function exits normally. But you could always rethrow the exception or throw a different one.

Arguments:

tr - translator function for an exception Exception. Translator type ExceptionTranslator should present single function:

arbitrary-type operator()( Exception const& ) const

Example:

void translate_my_exception1( my_exception1 const& ) { /* ... */ } 
the_monitor.register_exception_translator<my_exception1>( translate_my_exception1 );