mirror of
https://github.com/boostorg/exception.git
synced 2025-07-22 00:42:04 +02:00
Ticket #3848 (thanks Nikki Chumakov) and (unrelated) exception_ptr refactoring.
[SVN r59364]
This commit is contained in:
@ -28,6 +28,8 @@
|
||||
namespace
|
||||
boost
|
||||
{
|
||||
typedef shared_ptr<exception_detail::clone_base const> exception_ptr;
|
||||
|
||||
#ifndef BOOST_NO_RTTI
|
||||
typedef error_info<struct tag_original_exception_type,std::type_info const *> original_exception_type;
|
||||
|
||||
@ -39,85 +41,34 @@ boost
|
||||
}
|
||||
#endif
|
||||
|
||||
class exception_ptr;
|
||||
exception_ptr current_exception();
|
||||
void rethrow_exception( exception_ptr const & );
|
||||
|
||||
class
|
||||
exception_ptr
|
||||
namespace
|
||||
exception_detail
|
||||
{
|
||||
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)
|
||||
inline
|
||||
static
|
||||
exception_ptr
|
||||
exception_ptr_bad_alloc()
|
||||
{
|
||||
static
|
||||
struct
|
||||
bad_alloc_:
|
||||
std::bad_alloc,
|
||||
exception_detail::clone_base
|
||||
{
|
||||
clone_base const *
|
||||
clone() const
|
||||
{
|
||||
return new bad_alloc_(*this);
|
||||
}
|
||||
void
|
||||
rethrow() const
|
||||
{
|
||||
throw*this;
|
||||
}
|
||||
} e;
|
||||
return exception_ptr(exception_ptr(),&e);
|
||||
}
|
||||
|
||||
explicit
|
||||
exception_ptr( shared_ptr<exception_detail::clone_base const> const & c ):
|
||||
c_(c),
|
||||
bad_alloc_(false)
|
||||
{
|
||||
BOOST_ASSERT(c);
|
||||
}
|
||||
|
||||
void
|
||||
rethrow() const
|
||||
{
|
||||
BOOST_ASSERT(*this);
|
||||
if( bad_alloc_ )
|
||||
throw enable_current_exception(std::bad_alloc());
|
||||
else
|
||||
c_->rethrow();
|
||||
}
|
||||
|
||||
bool
|
||||
empty() const
|
||||
{
|
||||
return !bad_alloc_ && !c_;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
exception_ptr():
|
||||
bad_alloc_(false)
|
||||
{
|
||||
}
|
||||
|
||||
~exception_ptr() throw()
|
||||
{
|
||||
}
|
||||
|
||||
operator unspecified_bool_type() const
|
||||
{
|
||||
return empty() ? 0 : &exception_ptr::bad_alloc_;
|
||||
}
|
||||
|
||||
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:
|
||||
@ -256,7 +207,7 @@ boost
|
||||
|
||||
template <class T>
|
||||
inline
|
||||
shared_ptr<clone_base const>
|
||||
exception_ptr
|
||||
current_exception_std_exception( T const & e1 )
|
||||
{
|
||||
if( boost::exception const * e2 = get_boost_exception(&e1) )
|
||||
@ -266,21 +217,21 @@ boost
|
||||
}
|
||||
|
||||
inline
|
||||
shared_ptr<clone_base const>
|
||||
exception_ptr
|
||||
current_exception_unknown_exception()
|
||||
{
|
||||
return shared_ptr<unknown_exception const>(new unknown_exception());
|
||||
}
|
||||
|
||||
inline
|
||||
shared_ptr<clone_base const>
|
||||
exception_ptr
|
||||
current_exception_unknown_boost_exception( boost::exception const & e )
|
||||
{
|
||||
return shared_ptr<unknown_exception const>(new unknown_exception(e));
|
||||
}
|
||||
|
||||
inline
|
||||
shared_ptr<clone_base const>
|
||||
exception_ptr
|
||||
current_exception_unknown_std_exception( std::exception const & e )
|
||||
{
|
||||
if( boost::exception const * be = get_boost_exception(&e) )
|
||||
@ -290,7 +241,7 @@ boost
|
||||
}
|
||||
|
||||
inline
|
||||
shared_ptr<clone_base const>
|
||||
exception_ptr
|
||||
current_exception_impl()
|
||||
{
|
||||
try
|
||||
@ -300,7 +251,7 @@ boost
|
||||
catch(
|
||||
exception_detail::clone_base & e )
|
||||
{
|
||||
return shared_ptr<exception_detail::clone_base const>(e.clone());
|
||||
return exception_ptr(e.clone());
|
||||
}
|
||||
catch(
|
||||
std::domain_error & e )
|
||||
@ -396,24 +347,28 @@ boost
|
||||
exception_ptr
|
||||
current_exception()
|
||||
{
|
||||
exception_ptr ret;
|
||||
BOOST_ASSERT(!ret);
|
||||
try
|
||||
{
|
||||
return exception_ptr(exception_detail::current_exception_impl());
|
||||
ret=exception_detail::current_exception_impl();
|
||||
}
|
||||
catch(
|
||||
std::bad_alloc & )
|
||||
{
|
||||
ret=exception_detail::exception_ptr_bad_alloc();
|
||||
}
|
||||
catch(
|
||||
... )
|
||||
{
|
||||
try
|
||||
{
|
||||
return exception_ptr(exception_detail::current_exception_std_exception(std::bad_exception()));
|
||||
ret=exception_detail::current_exception_std_exception(std::bad_exception());
|
||||
}
|
||||
catch(
|
||||
std::bad_alloc & )
|
||||
{
|
||||
ret=exception_detail::exception_ptr_bad_alloc();
|
||||
}
|
||||
catch(
|
||||
... )
|
||||
@ -421,7 +376,8 @@ boost
|
||||
BOOST_ASSERT(0);
|
||||
}
|
||||
}
|
||||
return exception_ptr(exception_ptr::bad_alloc_tag());
|
||||
BOOST_ASSERT(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
@ -444,7 +400,8 @@ boost
|
||||
void
|
||||
rethrow_exception( exception_ptr const & p )
|
||||
{
|
||||
p.rethrow();
|
||||
BOOST_ASSERT(p);
|
||||
p->rethrow();
|
||||
}
|
||||
|
||||
inline
|
||||
|
@ -9,8 +9,10 @@
|
||||
namespace
|
||||
boost
|
||||
{
|
||||
namespace exception_detail { class clone_base; };
|
||||
template <class Tag,class T> class error_info;
|
||||
class exception_ptr;
|
||||
template <class T> class shared_ptr;
|
||||
typedef shared_ptr<exception_detail::clone_base const> exception_ptr;
|
||||
typedef error_info<struct errinfo_nested_exception_,exception_ptr> errinfo_nested_exception;
|
||||
}
|
||||
|
||||
|
@ -163,6 +163,7 @@ boost
|
||||
virtual void set( shared_ptr<error_info_base> const &, type_info_ const & ) = 0;
|
||||
virtual void add_ref() const = 0;
|
||||
virtual void release() const = 0;
|
||||
virtual refcount_ptr<exception_detail::error_info_container> clone() const = 0;
|
||||
|
||||
protected:
|
||||
|
||||
@ -184,6 +185,8 @@ boost
|
||||
struct get_info<throw_line>;
|
||||
|
||||
char const * get_diagnostic_information( exception const &, char const * );
|
||||
|
||||
void copy_boost_exception( exception *, exception const * );
|
||||
}
|
||||
|
||||
class
|
||||
@ -240,6 +243,7 @@ boost
|
||||
friend struct exception_detail::get_info<throw_function>;
|
||||
friend struct exception_detail::get_info<throw_file>;
|
||||
friend struct exception_detail::get_info<throw_line>;
|
||||
friend void exception_detail::copy_boost_exception( exception *, exception const * );
|
||||
#endif
|
||||
mutable exception_detail::refcount_ptr<exception_detail::error_info_container> data_;
|
||||
mutable char const * throw_function_;
|
||||
@ -363,7 +367,13 @@ boost
|
||||
void
|
||||
copy_boost_exception( exception * a, exception const * b )
|
||||
{
|
||||
*a = *b;
|
||||
refcount_ptr<error_info_container> data;
|
||||
if( error_info_container * d=b->data_.get() )
|
||||
data = d->clone();
|
||||
a->throw_file_ = b->throw_file_;
|
||||
a->throw_line_ = b->throw_line_;
|
||||
a->throw_function_ = b->throw_function_;
|
||||
a->data_ = data;
|
||||
}
|
||||
|
||||
inline
|
||||
|
@ -143,6 +143,14 @@ boost
|
||||
if( !--count_ )
|
||||
delete this;
|
||||
}
|
||||
|
||||
refcount_ptr<exception_detail::error_info_container>
|
||||
clone() const
|
||||
{
|
||||
refcount_ptr<exception_detail::error_info_container> c;
|
||||
c.adopt(new exception_detail::error_info_container_impl(*this));
|
||||
return c;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user