mirror of
https://github.com/boostorg/exception.git
synced 2025-07-16 05:52:14 +02:00
enable_current_exception.hpp decoupled from atomic_count.hpp
[SVN r48439]
This commit is contained in:
@ -16,12 +16,19 @@ boost
|
||||
{
|
||||
class clone_base;
|
||||
|
||||
struct
|
||||
new_clone
|
||||
{
|
||||
clone_base const * c_;
|
||||
void (*d_)(clone_base const *);
|
||||
};
|
||||
|
||||
class
|
||||
cloning_base
|
||||
{
|
||||
public:
|
||||
|
||||
virtual clone_base const * clone() const = 0;
|
||||
virtual new_clone clone() const = 0;
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -8,7 +8,6 @@
|
||||
|
||||
#include <boost/exception/exception.hpp>
|
||||
#include <boost/exception/detail/cloning_base.hpp>
|
||||
#include <boost/detail/atomic_count.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <new>
|
||||
|
||||
@ -31,39 +30,8 @@ boost
|
||||
{
|
||||
}
|
||||
|
||||
class
|
||||
clone_base:
|
||||
public counted_base
|
||||
{
|
||||
public:
|
||||
|
||||
virtual void rethrow() const=0;
|
||||
};
|
||||
|
||||
struct
|
||||
bad_alloc_impl:
|
||||
public clone_base,
|
||||
public std::bad_alloc
|
||||
{
|
||||
void
|
||||
add_ref() const
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
release() const
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
rethrow() const
|
||||
{
|
||||
throw *this;
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
clone_base * make_clone( T const & );
|
||||
new_clone make_clone( T const & );
|
||||
|
||||
template <class T>
|
||||
class
|
||||
@ -80,15 +48,46 @@ boost
|
||||
copy_boost_exception(this,&x);
|
||||
}
|
||||
|
||||
~clone_impl() throw()
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
clone_base const *
|
||||
new_clone
|
||||
clone() const
|
||||
{
|
||||
return make_clone<T>(*this);
|
||||
}
|
||||
};
|
||||
|
||||
class
|
||||
clone_base
|
||||
{
|
||||
public:
|
||||
|
||||
virtual void rethrow() const=0;
|
||||
virtual ~clone_base() throw()=0;
|
||||
};
|
||||
|
||||
inline
|
||||
clone_base::
|
||||
~clone_base() throw()
|
||||
{
|
||||
}
|
||||
|
||||
struct
|
||||
bad_alloc_impl:
|
||||
public clone_base,
|
||||
public std::bad_alloc
|
||||
{
|
||||
void
|
||||
rethrow() const
|
||||
{
|
||||
throw *this;
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class
|
||||
exception_clone:
|
||||
@ -99,27 +98,15 @@ boost
|
||||
|
||||
explicit
|
||||
exception_clone( T const & x ):
|
||||
T(x),
|
||||
count_(0)
|
||||
T(x)
|
||||
{
|
||||
copy_boost_exception(this,&x);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
mutable detail::atomic_count count_;
|
||||
|
||||
void
|
||||
add_ref() const
|
||||
~exception_clone() throw()
|
||||
{
|
||||
++count_;
|
||||
}
|
||||
|
||||
void
|
||||
release() const
|
||||
{
|
||||
if( !--count_ )
|
||||
delete this;
|
||||
}
|
||||
|
||||
void
|
||||
@ -129,27 +116,44 @@ boost
|
||||
}
|
||||
};
|
||||
|
||||
inline
|
||||
void
|
||||
delete_clone( clone_base const * c )
|
||||
{
|
||||
BOOST_ASSERT(c!=0);
|
||||
delete c;
|
||||
}
|
||||
|
||||
inline
|
||||
void
|
||||
delete_clone_noop( clone_base const * )
|
||||
{
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline
|
||||
clone_base *
|
||||
new_clone
|
||||
make_clone( T const & x )
|
||||
{
|
||||
new_clone tmp = {0,0};
|
||||
try
|
||||
{
|
||||
return new exception_clone<T>(x);
|
||||
tmp.c_=new exception_clone<T>(x);
|
||||
tmp.d_=&delete_clone;
|
||||
}
|
||||
catch(
|
||||
std::bad_alloc & )
|
||||
{
|
||||
static bad_alloc_impl bad_alloc;
|
||||
return &bad_alloc;
|
||||
tmp.c_=&bad_alloc;
|
||||
tmp.d_=&delete_clone_noop;
|
||||
}
|
||||
catch(
|
||||
... )
|
||||
{
|
||||
BOOST_ASSERT(0);
|
||||
return 0;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,13 +8,76 @@
|
||||
|
||||
#include <boost/exception/enable_current_exception.hpp>
|
||||
#include <boost/exception/detail/get_boost_exception.hpp>
|
||||
#include <boost/exception/detail/cloning_base.hpp>
|
||||
#include <boost/detail/atomic_count.hpp>
|
||||
#include <stdexcept>
|
||||
#include <new>
|
||||
|
||||
namespace
|
||||
boost
|
||||
{
|
||||
namespace
|
||||
exception_detail
|
||||
{
|
||||
class
|
||||
counted_clone:
|
||||
public counted_base
|
||||
{
|
||||
public:
|
||||
|
||||
counted_clone():
|
||||
count_(0),
|
||||
clone_(0)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
set( new_clone const & nc )
|
||||
{
|
||||
clone_ = nc.c_;
|
||||
clone_deleter_ = nc.d_;
|
||||
BOOST_ASSERT(clone_!=0);
|
||||
BOOST_ASSERT(clone_deleter_!=0);
|
||||
}
|
||||
|
||||
void
|
||||
rethrow() const
|
||||
{
|
||||
BOOST_ASSERT(clone_!=0);
|
||||
clone_->rethrow();
|
||||
}
|
||||
|
||||
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_ )
|
||||
clone_deleter_(clone_);
|
||||
}
|
||||
|
||||
void
|
||||
add_ref() const
|
||||
{
|
||||
++count_;
|
||||
}
|
||||
|
||||
void
|
||||
release() const
|
||||
{
|
||||
if( !--count_ )
|
||||
delete this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
typedef intrusive_ptr<exception_detail::counted_clone const> exception_ptr;
|
||||
|
||||
class
|
||||
unknown_exception:
|
||||
public exception,
|
||||
@ -37,8 +100,6 @@ boost
|
||||
}
|
||||
};
|
||||
|
||||
typedef intrusive_ptr<exception_detail::clone_base const> exception_ptr;
|
||||
|
||||
namespace
|
||||
exception_detail
|
||||
{
|
||||
@ -72,17 +133,21 @@ boost
|
||||
exception_ptr
|
||||
current_exception_std_exception( T const & e1 )
|
||||
{
|
||||
intrusive_ptr<exception_detail::counted_clone> x(new exception_detail::counted_clone);
|
||||
if( boost::exception const * e2 = get_boost_exception(&e1) )
|
||||
return exception_ptr(exception_detail::make_clone(current_exception_std_exception_wrapper<T>(e1,*e2)));
|
||||
x->set(exception_detail::make_clone(current_exception_std_exception_wrapper<T>(e1,*e2)));
|
||||
else
|
||||
return exception_ptr(exception_detail::make_clone(current_exception_std_exception_wrapper<T>(e1)));
|
||||
x->set(exception_detail::make_clone(current_exception_std_exception_wrapper<T>(e1)));
|
||||
return x;
|
||||
}
|
||||
|
||||
inline
|
||||
exception_ptr
|
||||
current_exception_unknown_exception()
|
||||
{
|
||||
return exception_ptr(exception_detail::make_clone(unknown_exception()));
|
||||
intrusive_ptr<exception_detail::counted_clone> x(new exception_detail::counted_clone);
|
||||
x->set(exception_detail::make_clone(unknown_exception()));
|
||||
return x;
|
||||
}
|
||||
|
||||
inline
|
||||
@ -90,7 +155,11 @@ boost
|
||||
current_exception_unknown_std_exception( std::exception const & e )
|
||||
{
|
||||
if( boost::exception const * be = get_boost_exception(&e) )
|
||||
return exception_ptr(exception_detail::make_clone(unknown_exception(*be)));
|
||||
{
|
||||
intrusive_ptr<exception_detail::counted_clone> x(new exception_detail::counted_clone);
|
||||
x->set(exception_detail::make_clone(unknown_exception(*be)));
|
||||
return x;
|
||||
}
|
||||
else
|
||||
return current_exception_unknown_exception();
|
||||
}
|
||||
@ -99,7 +168,9 @@ boost
|
||||
exception_ptr
|
||||
current_exception_unknown_boost_exception( boost::exception const & e )
|
||||
{
|
||||
return exception_ptr(exception_detail::make_clone(unknown_exception(e)));
|
||||
intrusive_ptr<exception_detail::counted_clone> x(new exception_detail::counted_clone);
|
||||
x->set(exception_detail::make_clone(unknown_exception(e)));
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
@ -114,9 +185,9 @@ boost
|
||||
catch(
|
||||
exception_detail::cloning_base & e )
|
||||
{
|
||||
exception_detail::clone_base const * c = e.clone();
|
||||
BOOST_ASSERT(c!=0);
|
||||
return exception_ptr(c);
|
||||
intrusive_ptr<exception_detail::counted_clone> x(new exception_detail::counted_clone);
|
||||
x->set(e.clone());
|
||||
return x;
|
||||
}
|
||||
catch(
|
||||
std::invalid_argument & e )
|
||||
|
Reference in New Issue
Block a user