2008-03-04 01:41:17 +00:00
|
|
|
//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.
|
|
|
|
|
|
|
|
//Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
|
|
|
#ifndef UUID_FA5836A2CADA11DC8CD47C8555D89593
|
|
|
|
#define UUID_FA5836A2CADA11DC8CD47C8555D89593
|
|
|
|
|
2008-08-29 19:27:38 +00:00
|
|
|
#include <boost/exception/enable_current_exception.hpp>
|
2008-08-30 02:57:12 +00:00
|
|
|
#include <boost/exception/detail/type_info.hpp>
|
2008-08-29 04:56:02 +00:00
|
|
|
#include <boost/detail/atomic_count.hpp>
|
2008-04-15 18:24:46 +00:00
|
|
|
#include <stdexcept>
|
2008-04-29 17:17:45 +00:00
|
|
|
#include <new>
|
2008-03-04 01:41:17 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
boost
|
2008-04-08 21:29:37 +00:00
|
|
|
{
|
2008-08-29 04:56:02 +00:00
|
|
|
namespace
|
|
|
|
exception_detail
|
|
|
|
{
|
|
|
|
class
|
|
|
|
counted_clone:
|
|
|
|
public counted_base
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
counted_clone():
|
|
|
|
count_(0),
|
|
|
|
clone_(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-08-29 19:27:38 +00:00
|
|
|
set( clone_base const * c )
|
2008-08-29 04:56:02 +00:00
|
|
|
{
|
2008-08-29 19:27:38 +00:00
|
|
|
clone_ = c;
|
2008-08-29 04:56:02 +00:00
|
|
|
BOOST_ASSERT(clone_!=0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rethrow() const
|
|
|
|
{
|
2008-08-29 19:27:38 +00:00
|
|
|
if( clone_ )
|
|
|
|
clone_->rethrow();
|
|
|
|
else
|
|
|
|
throw enable_current_exception(std::bad_alloc());
|
2008-08-29 04:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
counted_clone( counted_clone const & );
|
|
|
|
counted_clone & operator=( counted_clone const & );
|
|
|
|
|
|
|
|
mutable detail::atomic_count count_;
|
|
|
|
clone_base const * clone_;
|
|
|
|
void (*clone_deleter_)(clone_base const *);
|
|
|
|
|
|
|
|
~counted_clone() throw()
|
|
|
|
{
|
|
|
|
if( clone_ )
|
2008-08-29 19:27:38 +00:00
|
|
|
delete clone_;
|
2008-08-29 04:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
add_ref() const
|
|
|
|
{
|
|
|
|
++count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
release() const
|
|
|
|
{
|
|
|
|
if( !--count_ )
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
};
|
2008-08-29 19:27:38 +00:00
|
|
|
|
|
|
|
struct
|
|
|
|
bad_alloc_tag
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
struct
|
|
|
|
bad_exception_tag
|
|
|
|
{
|
|
|
|
};
|
2008-08-29 04:56:02 +00:00
|
|
|
}
|
|
|
|
|
2008-08-29 19:27:38 +00:00
|
|
|
class exception_ptr;
|
|
|
|
void rethrow_exception( exception_ptr const & );
|
|
|
|
|
|
|
|
class
|
|
|
|
exception_ptr
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
friend void rethrow_exception( exception_ptr const & );
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
bad_alloc_caught,
|
|
|
|
clone_failed,
|
|
|
|
ok
|
|
|
|
} what_happened_;
|
|
|
|
|
|
|
|
intrusive_ptr<exception_detail::counted_clone> c_;
|
|
|
|
|
|
|
|
void
|
|
|
|
rethrow() const
|
|
|
|
{
|
|
|
|
switch(
|
|
|
|
what_happened_ )
|
|
|
|
{
|
|
|
|
case
|
|
|
|
bad_alloc_caught:
|
|
|
|
throw enable_current_exception(std::bad_alloc());
|
|
|
|
case
|
|
|
|
clone_failed:
|
|
|
|
throw enable_current_exception(std::bad_exception());
|
|
|
|
case
|
|
|
|
ok:
|
|
|
|
BOOST_ASSERT(c_.get()!=0);
|
|
|
|
c_->rethrow();
|
|
|
|
}
|
|
|
|
BOOST_ASSERT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef intrusive_ptr<exception_detail::counted_clone> exception_ptr::*unspecified_bool_type;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
explicit
|
|
|
|
exception_ptr( exception_detail::bad_alloc_tag ):
|
|
|
|
what_happened_(bad_alloc_caught)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit
|
|
|
|
exception_ptr( exception_detail::bad_exception_tag ):
|
|
|
|
what_happened_(clone_failed)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
exception_ptr():
|
|
|
|
what_happened_(ok)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit
|
|
|
|
exception_ptr( intrusive_ptr<exception_detail::counted_clone> const & c ):
|
|
|
|
what_happened_(ok),
|
|
|
|
c_(c)
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(c_.get()!=0);
|
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
bool
|
|
|
|
operator==( exception_ptr const & a, exception_ptr const & b )
|
|
|
|
{
|
|
|
|
return
|
|
|
|
a.what_happened_==ok &&
|
|
|
|
b.what_happened_==ok &&
|
|
|
|
a.c_==b.c_;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
bool
|
|
|
|
operator!=( exception_ptr const & a, exception_ptr const & b )
|
|
|
|
{
|
|
|
|
return !(a==b);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator unspecified_bool_type() const
|
|
|
|
{
|
|
|
|
return (what_happened_!=ok || c_) ? &exception_ptr::c_ : 0;
|
|
|
|
}
|
|
|
|
};
|
2008-08-29 04:56:02 +00:00
|
|
|
|
2008-04-08 21:29:37 +00:00
|
|
|
class
|
|
|
|
unknown_exception:
|
|
|
|
public exception,
|
2008-08-29 19:27:38 +00:00
|
|
|
public std::exception,
|
|
|
|
public exception_detail::clone_base
|
2008-04-08 21:29:37 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
unknown_exception()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit
|
2008-04-15 18:24:46 +00:00
|
|
|
unknown_exception( boost::exception const & e ):
|
|
|
|
boost::exception(e)
|
2008-04-08 21:29:37 +00:00
|
|
|
{
|
|
|
|
}
|
2008-04-21 22:42:54 +00:00
|
|
|
|
2008-07-09 00:18:09 +00:00
|
|
|
~unknown_exception() throw()
|
|
|
|
{
|
|
|
|
}
|
2008-08-29 19:27:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
clone_base const *
|
|
|
|
clone() const
|
|
|
|
{
|
|
|
|
return new unknown_exception(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rethrow() const
|
|
|
|
{
|
|
|
|
throw *this;
|
|
|
|
}
|
2008-04-08 21:29:37 +00:00
|
|
|
};
|
|
|
|
|
2008-04-15 18:24:46 +00:00
|
|
|
namespace
|
|
|
|
exception_detail
|
|
|
|
{
|
|
|
|
template <class T>
|
|
|
|
class
|
|
|
|
current_exception_std_exception_wrapper:
|
|
|
|
public T,
|
2008-08-29 19:27:38 +00:00
|
|
|
public boost::exception,
|
|
|
|
public clone_base
|
2008-04-15 18:24:46 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
explicit
|
|
|
|
current_exception_std_exception_wrapper( T const & e1 ):
|
|
|
|
T(e1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
current_exception_std_exception_wrapper( T const & e1, boost::exception const & e2 ):
|
|
|
|
T(e1),
|
|
|
|
boost::exception(e2)
|
|
|
|
{
|
|
|
|
}
|
2008-04-21 22:42:54 +00:00
|
|
|
|
2008-07-09 00:18:09 +00:00
|
|
|
~current_exception_std_exception_wrapper() throw()
|
|
|
|
{
|
|
|
|
}
|
2008-08-29 19:27:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
clone_base const *
|
|
|
|
clone() const
|
|
|
|
{
|
|
|
|
return new current_exception_std_exception_wrapper(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rethrow() const
|
|
|
|
{
|
|
|
|
throw *this;
|
|
|
|
}
|
2008-04-15 18:24:46 +00:00
|
|
|
};
|
|
|
|
|
2008-08-30 02:57:12 +00:00
|
|
|
#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
|
|
|
|
|
2008-04-15 18:24:46 +00:00
|
|
|
template <class T>
|
2008-07-09 00:18:09 +00:00
|
|
|
inline
|
2008-04-15 18:24:46 +00:00
|
|
|
exception_ptr
|
|
|
|
current_exception_std_exception( T const & e1 )
|
|
|
|
{
|
2008-08-29 04:56:02 +00:00
|
|
|
intrusive_ptr<exception_detail::counted_clone> x(new exception_detail::counted_clone);
|
2008-08-28 23:49:55 +00:00
|
|
|
if( boost::exception const * e2 = get_boost_exception(&e1) )
|
2008-08-29 19:27:38 +00:00
|
|
|
x->set(new current_exception_std_exception_wrapper<T>(e1,*e2));
|
2008-04-15 18:24:46 +00:00
|
|
|
else
|
2008-08-29 19:27:38 +00:00
|
|
|
x->set(new current_exception_std_exception_wrapper<T>(e1));
|
|
|
|
return exception_ptr(x);
|
2008-04-15 18:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
exception_ptr
|
|
|
|
current_exception_unknown_exception()
|
|
|
|
{
|
2008-08-29 04:56:02 +00:00
|
|
|
intrusive_ptr<exception_detail::counted_clone> x(new exception_detail::counted_clone);
|
2008-08-29 19:27:38 +00:00
|
|
|
x->set(new unknown_exception());
|
|
|
|
return exception_ptr(x);
|
2008-04-15 18:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
exception_ptr
|
|
|
|
current_exception_unknown_std_exception( std::exception const & e )
|
|
|
|
{
|
2008-08-28 23:49:55 +00:00
|
|
|
if( boost::exception const * be = get_boost_exception(&e) )
|
2008-08-29 04:56:02 +00:00
|
|
|
{
|
|
|
|
intrusive_ptr<exception_detail::counted_clone> x(new exception_detail::counted_clone);
|
2008-08-29 19:27:38 +00:00
|
|
|
x->set(new unknown_exception(*be));
|
|
|
|
return exception_ptr(x);
|
2008-08-29 04:56:02 +00:00
|
|
|
}
|
2008-04-15 18:24:46 +00:00
|
|
|
else
|
|
|
|
return current_exception_unknown_exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
exception_ptr
|
|
|
|
current_exception_unknown_boost_exception( boost::exception const & e )
|
|
|
|
{
|
2008-08-29 04:56:02 +00:00
|
|
|
intrusive_ptr<exception_detail::counted_clone> x(new exception_detail::counted_clone);
|
2008-08-29 19:27:38 +00:00
|
|
|
x->set(new unknown_exception(e));
|
|
|
|
return exception_ptr(x);
|
2008-04-15 18:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
exception_ptr
|
|
|
|
current_exception()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2008-08-29 19:27:38 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(
|
|
|
|
exception_detail::clone_base & e )
|
|
|
|
{
|
|
|
|
intrusive_ptr<exception_detail::counted_clone> x(new exception_detail::counted_clone);
|
|
|
|
x->set(e.clone());
|
|
|
|
return exception_ptr(x);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2008-08-30 02:57:12 +00:00
|
|
|
#ifndef BOOST_NO_TYPEID
|
2008-08-29 19:27:38 +00:00
|
|
|
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);
|
|
|
|
}
|
2008-08-30 02:57:12 +00:00
|
|
|
#endif
|
2008-08-29 19:27:38 +00:00
|
|
|
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();
|
|
|
|
}
|
2008-04-15 18:24:46 +00:00
|
|
|
}
|
|
|
|
catch(
|
2008-08-29 19:27:38 +00:00
|
|
|
std::bad_alloc & )
|
2008-04-15 18:24:46 +00:00
|
|
|
{
|
2008-08-29 19:27:38 +00:00
|
|
|
return exception_ptr( exception_detail::bad_alloc_tag() );
|
2008-04-15 18:24:46 +00:00
|
|
|
}
|
|
|
|
catch(
|
|
|
|
... )
|
|
|
|
{
|
2008-08-29 19:27:38 +00:00
|
|
|
return exception_ptr( exception_detail::bad_exception_tag() );
|
2008-04-15 18:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-08 21:29:37 +00:00
|
|
|
template <class T>
|
2008-07-09 00:18:09 +00:00
|
|
|
inline
|
2008-04-08 21:29:37 +00:00
|
|
|
exception_ptr
|
2008-04-15 18:24:46 +00:00
|
|
|
copy_exception( T const & e )
|
2008-04-08 21:29:37 +00:00
|
|
|
{
|
2008-04-15 18:24:46 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
throw enable_current_exception(e);
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
return current_exception();
|
|
|
|
}
|
2008-04-08 21:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
void
|
|
|
|
rethrow_exception( exception_ptr const & p )
|
|
|
|
{
|
2008-08-29 19:27:38 +00:00
|
|
|
p.rethrow();
|
2008-04-08 21:29:37 +00:00
|
|
|
}
|
|
|
|
}
|
2008-03-04 01:41:17 +00:00
|
|
|
|
|
|
|
#endif
|