Boost logo

Boost :

From: Chris Knight (chrisknight42_at_[hidden])
Date: 2007-10-06 08:29:16


> ...such as?
>
> Besides satisfying my own curiosity, the wider scope of a library that
> qualifies for the name "Exception" would still be interesting in the
> context of this review.

Asynchronous exception handling:

// rethrow/clone concepts
class exception
{
    virtual void rethrow() const = 0;
    virtual exception* clone() const = 0;
};

template<typename T>
class build_rethrowable_exception
{
    void rethrow() const { throw T(*this); }
    exception* clone() const { return new T(*this); }
};

// user defined exception
class my_exception : public build_rethrowable_exception<my_exception>
{
};

// example of asynchronous exception handling
class queue_item_visitor : public boost::static_visitor<>
{
    void operator()(const exception& ex)
    {
        ex.rethrow();
    }

    void operator()(const message& msg)
    {
        ...
    }
};

typedef boost::shared_ptr<exception> exception_ptr;
typedef boost::shared_ptr<message> message_ptr;
typedef boost::variant<exception_ptr, message_ptr> queue_item;
typedef boost::shared_ptr<queue_item> queue_item_ptr;
TSQueue<queue_item_ptr> s_queue;

thread1: // some thread responsible for reading messages
{
    while (...)
    {
        try
        {
            s_queue.push_back(queue_item_ptr(new queue_item(read_message())));
        }
        catch (const exception& ex)
        {
            s_queue.push_back(queue_item_ptr(new queue_item(exception_ptr(ex.clone()))));
        }
    }
}

thread2: // some thread responsible for processing messages
{
    while (...)
    {
        queue_item_ptr item(s_queue.pop_front());
        boost::apply_visitor(queue_item_visitor(), *item);
    }
}

_________________________________________________________________
Climb to the top of the charts!  Play Star Shuffle:  the word scramble challenge with star power.
http://club.live.com/star_shuffle.aspx?icid=starshuffle_wlmailtextlink_oct


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