Boost logo

Boost :

From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2006-07-19 15:49:47


I don't know a practical value of this library except probably
a management of exception handlers order but I can suggest (rather
theoretical at this time) modification of your code to get rid of
code duplication.

Classes in your example appear in mpl::vector and inside operator():

struct my_exception_handler
{
     typedef boost::mpl::vector<std::logic_error, std::exception> exceptions;
     // HERE ^^^^^^^^^^^^^^^^

     void operator()(std::exception& oError);
     void operator()(std::logic_error& oError);
     // AND HERE ^^^^^^^^^^^^^^^^
     void operator()(void);
};

For compilers that support typeof, you could do this:

struct my_exception_handler
{
     void operator()(id<1>, std::exception& oError) const;
     void operator()(id<2>, std::logic_error& oError) const;
     void operator()(id<3> /* void */ ) const;
};

Note the first agruments id<1>, id<2> and id<3>.

You can get more information about how this magic works here:
http://thread.gmane.org/gmane.comp.lib.boost.devel/117532

-- 
Alexander Nasonov
Project Manager at Akmosoft ( http://www.akmosoft.com )
Blog: http://nasonov.blogspot.com
Email: $(FirstName) dot $(LastName) at gmail dot com
Jeremy Day wrote:
> All,
> 
> A message a few days ago, along with my current reading of the book 'C++
> Template Metaprogramming', piqued my interest in the idea of an "exception
> visitor".  I put together a little something, and I'm wondering if people
> might be interested in my (rather poor, at the moment) code.
> 
> Problem:
> 
> When using exceptions it is very easy to end up with code that looks
> something like this:
> 
> try
>   {
>   //Some code that might throw exceptions.
>   }
> catch(std::logic_error&)
>   {
>   //Handle the exception.
>   }
> catch(std::exception&)
>   {
>   //Handle exception.
>   }
> catch(...)
>   {
>   //Handle everything else
>   }
> 
> With my code you can transform the above into something along these lines:
> 
> struct my_exception_handler
>     {
>     typedef boost::mpl::vector<std::logic_error, std::exception> exceptions;
> 
>     void operator()(std::exception& oError)
>         {
>         std::cout << "my_exception_handler::operator()(std::exception&)" <<
> std::endl;
>         }
> 
>     void operator()(std::logic_error& oError)
>         {
>         std::cout << "my_exception_handler::operator()(std::logic_error&)"
> << std::endl;
>         }
> 
>     void operator()(void)
>         {
>         std::cout << "my_exception_handler::operator()(void)" << std::endl;
>         }
>     };
> 
> //Application code.
> 
> try
>   {
>   //Some code that might throw exceptions, such as:
>   throw std::logic_error("A sample logic error.");
>   }
> catch(...)
>   {
>   //Magic happens here.
>   catcher(my_exception_handler()); //This prints out
> "my_exception_handler::operator()(std::logic_error&)"
>   }
> 
> If anyone is interested I will post what I have for critique (I'm a relative
> newbie with MPL, so I will likely need a lot of help).  I look forward to
> any and all comments.
> 
> Jeremy
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

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