Boost Exception major refactoring: works with or without RTTI, vastly improved boost::throw_exception integration.

[SVN r48905]
This commit is contained in:
Emil Dotchevski
2008-09-19 20:29:26 +00:00
parent c9fcdf15b2
commit a9b5723d0d
73 changed files with 6532 additions and 5046 deletions

View File

@ -6,19 +6,80 @@
#ifndef UUID_FA5836A2CADA11DC8CD47C8555D89593
#define UUID_FA5836A2CADA11DC8CD47C8555D89593
#include <boost/exception/enable_current_exception.hpp>
#include <boost/exception/exception.hpp>
#include <boost/exception/detail/cloning_base.hpp>
#include <boost/exception/detail/type_info.hpp>
#include <boost/shared_ptr.hpp>
#include <stdexcept>
#include <new>
namespace
boost
{
class exception_ptr;
exception_ptr current_exception();
void rethrow_exception( exception_ptr const & );
class
exception_ptr
{
typedef bool exception_ptr::*unspecified_bool_type;
friend exception_ptr current_exception();
friend void rethrow_exception( exception_ptr const & );
shared_ptr<exception_detail::clone_base const> c_;
bool bad_alloc_;
struct
bad_alloc_tag
{
};
explicit
exception_ptr( bad_alloc_tag ):
bad_alloc_(true)
{
}
explicit
exception_ptr( shared_ptr<exception_detail::clone_base const> const & c ):
c_(c),
bad_alloc_(false)
{
BOOST_ASSERT(c);
}
public:
exception_ptr():
bad_alloc_(false)
{
}
operator unspecified_bool_type() const
{
return (bad_alloc_ || c_) ? &exception_ptr::bad_alloc_ : 0;
}
friend
bool
operator==( exception_ptr const & a, exception_ptr const & b )
{
return a.c_==b.c_ && a.bad_alloc_==b.bad_alloc_;
}
friend
bool
operator!=( exception_ptr const & a, exception_ptr const & b )
{
return !(a==b);
}
};
class
unknown_exception:
public exception,
public std::exception
public std::exception,
public exception_detail::clone_base
{
public:
@ -35,9 +96,21 @@ boost
~unknown_exception() throw()
{
}
};
typedef intrusive_ptr<exception_detail::clone_base const> exception_ptr;
private:
clone_base const *
clone() const
{
return new unknown_exception(*this);
}
void
rethrow() const
{
throw*this;
}
};
namespace
exception_detail
@ -46,7 +119,8 @@ boost
class
current_exception_std_exception_wrapper:
public T,
public boost::exception
public boost::exception,
public clone_base
{
public:
@ -65,41 +139,150 @@ boost
~current_exception_std_exception_wrapper() throw()
{
}
private:
clone_base const *
clone() const
{
return new current_exception_std_exception_wrapper(*this);
}
void
rethrow() const
{
throw *this;
}
};
#ifdef BOOST_NO_RTTI
template <class T>
exception const *
get_boost_exception( T const * )
{
try
{
throw;
}
catch(
exception & x )
{
return &x;
}
catch(...)
{
return 0;
}
}
#else
template <class T>
exception const *
get_boost_exception( T const * x )
{
return dynamic_cast<exception const *>(x);
}
#endif
template <class T>
inline
exception_ptr
shared_ptr<clone_base const>
current_exception_std_exception( T const & e1 )
{
if( boost::exception const * e2 = dynamic_cast<boost::exception const *>(&e1) )
return exception_ptr(exception_detail::make_clone(current_exception_std_exception_wrapper<T>(e1,*e2)));
if( boost::exception const * e2 = get_boost_exception(&e1) )
return shared_ptr<clone_base const>(new current_exception_std_exception_wrapper<T>(e1,*e2));
else
return exception_ptr(exception_detail::make_clone(current_exception_std_exception_wrapper<T>(e1)));
return shared_ptr<clone_base const>(new current_exception_std_exception_wrapper<T>(e1));
}
inline
exception_ptr
shared_ptr<clone_base const>
current_exception_unknown_exception()
{
return exception_ptr(exception_detail::make_clone(unknown_exception()));
return shared_ptr<clone_base const>(new unknown_exception());
}
inline
exception_ptr
shared_ptr<clone_base const>
current_exception_unknown_boost_exception( boost::exception const & e )
{
return shared_ptr<clone_base const>(new unknown_exception(e));
}
inline
shared_ptr<clone_base const>
current_exception_unknown_std_exception( std::exception const & e )
{
if( boost::exception const * be = dynamic_cast<boost::exception const *>(&e) )
return exception_ptr(exception_detail::make_clone(unknown_exception(*be)));
if( boost::exception const * be = get_boost_exception(&e) )
return current_exception_unknown_boost_exception(*be);
else
return current_exception_unknown_exception();
}
inline
exception_ptr
current_exception_unknown_boost_exception( boost::exception const & e )
shared_ptr<clone_base const>
current_exception_impl()
{
return exception_ptr(exception_detail::make_clone(unknown_exception(e)));
try
{
throw;
}
catch(
exception_detail::clone_base & e )
{
return shared_ptr<exception_detail::clone_base const>(e.clone());
}
catch(
std::invalid_argument & e )
{
return exception_detail::current_exception_std_exception(e);
}
catch(
std::out_of_range & e )
{
return exception_detail::current_exception_std_exception(e);
}
catch(
std::logic_error & e )
{
return exception_detail::current_exception_std_exception(e);
}
catch(
std::bad_alloc & e )
{
return exception_detail::current_exception_std_exception(e);
}
#ifndef BOOST_NO_TYPEID
catch(
std::bad_cast & e )
{
return exception_detail::current_exception_std_exception(e);
}
catch(
std::bad_typeid & e )
{
return exception_detail::current_exception_std_exception(e);
}
#endif
catch(
std::bad_exception & e )
{
return exception_detail::current_exception_std_exception(e);
}
catch(
std::exception & e )
{
return exception_detail::current_exception_unknown_std_exception(e);
}
catch(
boost::exception & e )
{
return exception_detail::current_exception_unknown_boost_exception(e);
}
catch(
... )
{
return exception_detail::current_exception_unknown_exception();
}
}
}
@ -109,65 +292,30 @@ boost
{
try
{
throw;
return exception_ptr(exception_detail::current_exception_impl());
}
catch(
exception_detail::cloning_base & e )
std::bad_alloc & )
{
exception_detail::clone_base const * c = e.clone();
BOOST_ASSERT(c!=0);
return exception_ptr(c);
}
catch(
std::invalid_argument & e )
{
return exception_detail::current_exception_std_exception(e);
}
catch(
std::out_of_range & e )
{
return exception_detail::current_exception_std_exception(e);
}
catch(
std::logic_error & e )
{
return exception_detail::current_exception_std_exception(e);
}
catch(
std::bad_alloc & e )
{
return exception_detail::current_exception_std_exception(e);
}
catch(
std::bad_cast & e )
{
return exception_detail::current_exception_std_exception(e);
}
catch(
std::bad_typeid & e )
{
return exception_detail::current_exception_std_exception(e);
}
catch(
std::bad_exception & e )
{
return exception_detail::current_exception_std_exception(e);
}
catch(
std::exception & e )
{
return exception_detail::current_exception_unknown_std_exception(e);
}
catch(
boost::exception & e )
{
return exception_detail::current_exception_unknown_boost_exception(e);
}
catch(
... )
{
return exception_detail::current_exception_unknown_exception();
try
{
return exception_ptr(exception_detail::current_exception_std_exception(std::bad_exception()));
}
catch(
std::bad_alloc & )
{
}
catch(
... )
{
BOOST_ASSERT(0);
}
}
return exception_ptr(exception_ptr::bad_alloc_tag());
}
template <class T>
@ -179,7 +327,8 @@ boost
{
throw enable_current_exception(e);
}
catch( ... )
catch(
... )
{
return current_exception();
}
@ -189,7 +338,11 @@ boost
void
rethrow_exception( exception_ptr const & p )
{
p->rethrow();
BOOST_ASSERT(p);
if( p.bad_alloc_ )
throw enable_current_exception(std::bad_alloc());
else
p.c_->rethrow();
}
}