Boost logo

Boost :

From: Gennadiy Rozental (gennadiy.rozental_at_[hidden])
Date: 2005-02-17 13:39:38


"David Abrahams" <dave_at_[hidden]> wrote in message news:u8y5nt5qs.fsf_at_boost-consulting.com...
>
> Can it really be two years ago that I talked about refactoring the
> mechanism in Boost.Python?
>
> http://aspn.activestate.com/ASPN/Mail/Message/boost/1537162
>
> Well, I had to do it for a talk I'm giving, so here it is. It could
> easily be boost::detail-ized. Is there still interest?
>
>

I've implemented similar facilities in Boost.Test. Here is how latest (not cometted)version look like (abridged):

namespace detail {

class translate_exception_base {
public:
    // Constructor
    explicit translate_exception_base( boost::scoped_ptr<translate_exception_base>& next )

    // Destructor
    virtual ~translate_exception_base() {}

    virtual int operator()( unit_test::callback0<int> const& F ) = 0;

protected:
    // Data members
    boost::scoped_ptr<translate_exception_base> m_next;
};

template<typename Exception, typename ExceptionTranslator>
class translate_exception : public translate_exception_base
{
    typedef boost::scoped_ptr<translate_exception_base> base_ptr;
public:
    explicit translate_exception( ExceptionTranslator const& tr, base_ptr& next )
    : translate_exception_base( next ), m_translator( tr ) {}

    int operator()( unit_test::callback0<int> const& F )
    {
        try {
            return m_next ? (*m_next)( F ) : F();
        } catch( Exception const& e ) {
            m_translator( e );
            return boost::exit_exception_failure;
        }
    }

private:
    // Data members
    ExceptionTranslator m_translator;
};

}

class execution_monitor {
public:
    int execute( unit_test::callback0<int> const& F, bool catch_system_errors = true, int timeout = 0 );
    
    // register custom (user supplied) exception translator
    template<typename Exception, typename ExceptionTranslator>
    void register_exception_translator( ExceptionTranslator const& tr, boost::type<Exception>* = 0 );

private:
    // Data members
    boost::scoped_ptr<detail::translate_exception_base> m_custom_translators;
}; // execution_monitor

Usage look like this:

boost::execution_monitor ex_mon;

ex_mon.register_exception_translator<my_exception>( my_translator );

ex_mon.execute( function_to_monitor );

Isn't it what you are trying to do?

Gennadiy


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk