forked from boostorg/exception
Ticket #3848 (thanks Nikki Chumakov) and (unrelated) exception_ptr refactoring.
[SVN r59364]
This commit is contained in:
@ -28,6 +28,8 @@
|
|||||||
namespace
|
namespace
|
||||||
boost
|
boost
|
||||||
{
|
{
|
||||||
|
typedef shared_ptr<exception_detail::clone_base const> exception_ptr;
|
||||||
|
|
||||||
#ifndef BOOST_NO_RTTI
|
#ifndef BOOST_NO_RTTI
|
||||||
typedef error_info<struct tag_original_exception_type,std::type_info const *> original_exception_type;
|
typedef error_info<struct tag_original_exception_type,std::type_info const *> original_exception_type;
|
||||||
|
|
||||||
@ -39,86 +41,35 @@ boost
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class exception_ptr;
|
namespace
|
||||||
exception_ptr current_exception();
|
exception_detail
|
||||||
void rethrow_exception( exception_ptr const & );
|
{
|
||||||
|
inline
|
||||||
class
|
static
|
||||||
exception_ptr
|
exception_ptr
|
||||||
|
exception_ptr_bad_alloc()
|
||||||
{
|
{
|
||||||
typedef bool exception_ptr::*unspecified_bool_type;
|
static
|
||||||
friend exception_ptr current_exception();
|
|
||||||
friend void rethrow_exception( exception_ptr const & );
|
|
||||||
|
|
||||||
shared_ptr<exception_detail::clone_base const> c_;
|
|
||||||
bool bad_alloc_;
|
|
||||||
|
|
||||||
struct
|
struct
|
||||||
bad_alloc_tag
|
bad_alloc_:
|
||||||
|
std::bad_alloc,
|
||||||
|
exception_detail::clone_base
|
||||||
{
|
{
|
||||||
};
|
clone_base const *
|
||||||
|
clone() const
|
||||||
explicit
|
|
||||||
exception_ptr( bad_alloc_tag ):
|
|
||||||
bad_alloc_(true)
|
|
||||||
{
|
{
|
||||||
|
return new bad_alloc_(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit
|
|
||||||
exception_ptr( shared_ptr<exception_detail::clone_base const> const & c ):
|
|
||||||
c_(c),
|
|
||||||
bad_alloc_(false)
|
|
||||||
{
|
|
||||||
BOOST_ASSERT(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
rethrow() const
|
rethrow() const
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(*this);
|
throw*this;
|
||||||
if( bad_alloc_ )
|
|
||||||
throw enable_current_exception(std::bad_alloc());
|
|
||||||
else
|
|
||||||
c_->rethrow();
|
|
||||||
}
|
}
|
||||||
|
} e;
|
||||||
bool
|
return exception_ptr(exception_ptr(),&e);
|
||||||
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
|
class
|
||||||
unknown_exception:
|
unknown_exception:
|
||||||
public exception,
|
public exception,
|
||||||
@ -256,7 +207,7 @@ boost
|
|||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
inline
|
inline
|
||||||
shared_ptr<clone_base const>
|
exception_ptr
|
||||||
current_exception_std_exception( T const & e1 )
|
current_exception_std_exception( T const & e1 )
|
||||||
{
|
{
|
||||||
if( boost::exception const * e2 = get_boost_exception(&e1) )
|
if( boost::exception const * e2 = get_boost_exception(&e1) )
|
||||||
@ -266,21 +217,21 @@ boost
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline
|
inline
|
||||||
shared_ptr<clone_base const>
|
exception_ptr
|
||||||
current_exception_unknown_exception()
|
current_exception_unknown_exception()
|
||||||
{
|
{
|
||||||
return shared_ptr<unknown_exception const>(new unknown_exception());
|
return shared_ptr<unknown_exception const>(new unknown_exception());
|
||||||
}
|
}
|
||||||
|
|
||||||
inline
|
inline
|
||||||
shared_ptr<clone_base const>
|
exception_ptr
|
||||||
current_exception_unknown_boost_exception( boost::exception const & e )
|
current_exception_unknown_boost_exception( boost::exception const & e )
|
||||||
{
|
{
|
||||||
return shared_ptr<unknown_exception const>(new unknown_exception(e));
|
return shared_ptr<unknown_exception const>(new unknown_exception(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline
|
inline
|
||||||
shared_ptr<clone_base const>
|
exception_ptr
|
||||||
current_exception_unknown_std_exception( std::exception const & e )
|
current_exception_unknown_std_exception( std::exception const & e )
|
||||||
{
|
{
|
||||||
if( boost::exception const * be = get_boost_exception(&e) )
|
if( boost::exception const * be = get_boost_exception(&e) )
|
||||||
@ -290,7 +241,7 @@ boost
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline
|
inline
|
||||||
shared_ptr<clone_base const>
|
exception_ptr
|
||||||
current_exception_impl()
|
current_exception_impl()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -300,7 +251,7 @@ boost
|
|||||||
catch(
|
catch(
|
||||||
exception_detail::clone_base & e )
|
exception_detail::clone_base & e )
|
||||||
{
|
{
|
||||||
return shared_ptr<exception_detail::clone_base const>(e.clone());
|
return exception_ptr(e.clone());
|
||||||
}
|
}
|
||||||
catch(
|
catch(
|
||||||
std::domain_error & e )
|
std::domain_error & e )
|
||||||
@ -396,24 +347,28 @@ boost
|
|||||||
exception_ptr
|
exception_ptr
|
||||||
current_exception()
|
current_exception()
|
||||||
{
|
{
|
||||||
|
exception_ptr ret;
|
||||||
|
BOOST_ASSERT(!ret);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return exception_ptr(exception_detail::current_exception_impl());
|
ret=exception_detail::current_exception_impl();
|
||||||
}
|
}
|
||||||
catch(
|
catch(
|
||||||
std::bad_alloc & )
|
std::bad_alloc & )
|
||||||
{
|
{
|
||||||
|
ret=exception_detail::exception_ptr_bad_alloc();
|
||||||
}
|
}
|
||||||
catch(
|
catch(
|
||||||
... )
|
... )
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return exception_ptr(exception_detail::current_exception_std_exception(std::bad_exception()));
|
ret=exception_detail::current_exception_std_exception(std::bad_exception());
|
||||||
}
|
}
|
||||||
catch(
|
catch(
|
||||||
std::bad_alloc & )
|
std::bad_alloc & )
|
||||||
{
|
{
|
||||||
|
ret=exception_detail::exception_ptr_bad_alloc();
|
||||||
}
|
}
|
||||||
catch(
|
catch(
|
||||||
... )
|
... )
|
||||||
@ -421,7 +376,8 @@ boost
|
|||||||
BOOST_ASSERT(0);
|
BOOST_ASSERT(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return exception_ptr(exception_ptr::bad_alloc_tag());
|
BOOST_ASSERT(ret);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
@ -444,7 +400,8 @@ boost
|
|||||||
void
|
void
|
||||||
rethrow_exception( exception_ptr const & p )
|
rethrow_exception( exception_ptr const & p )
|
||||||
{
|
{
|
||||||
p.rethrow();
|
BOOST_ASSERT(p);
|
||||||
|
p->rethrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline
|
inline
|
||||||
|
@ -9,8 +9,10 @@
|
|||||||
namespace
|
namespace
|
||||||
boost
|
boost
|
||||||
{
|
{
|
||||||
|
namespace exception_detail { class clone_base; };
|
||||||
template <class Tag,class T> class error_info;
|
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;
|
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 set( shared_ptr<error_info_base> const &, type_info_ const & ) = 0;
|
||||||
virtual void add_ref() const = 0;
|
virtual void add_ref() const = 0;
|
||||||
virtual void release() const = 0;
|
virtual void release() const = 0;
|
||||||
|
virtual refcount_ptr<exception_detail::error_info_container> clone() const = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
@ -184,6 +185,8 @@ boost
|
|||||||
struct get_info<throw_line>;
|
struct get_info<throw_line>;
|
||||||
|
|
||||||
char const * get_diagnostic_information( exception const &, char const * );
|
char const * get_diagnostic_information( exception const &, char const * );
|
||||||
|
|
||||||
|
void copy_boost_exception( exception *, exception const * );
|
||||||
}
|
}
|
||||||
|
|
||||||
class
|
class
|
||||||
@ -240,6 +243,7 @@ boost
|
|||||||
friend struct exception_detail::get_info<throw_function>;
|
friend struct exception_detail::get_info<throw_function>;
|
||||||
friend struct exception_detail::get_info<throw_file>;
|
friend struct exception_detail::get_info<throw_file>;
|
||||||
friend struct exception_detail::get_info<throw_line>;
|
friend struct exception_detail::get_info<throw_line>;
|
||||||
|
friend void exception_detail::copy_boost_exception( exception *, exception const * );
|
||||||
#endif
|
#endif
|
||||||
mutable exception_detail::refcount_ptr<exception_detail::error_info_container> data_;
|
mutable exception_detail::refcount_ptr<exception_detail::error_info_container> data_;
|
||||||
mutable char const * throw_function_;
|
mutable char const * throw_function_;
|
||||||
@ -363,7 +367,13 @@ boost
|
|||||||
void
|
void
|
||||||
copy_boost_exception( exception * a, exception const * b )
|
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
|
inline
|
||||||
|
@ -143,6 +143,14 @@ boost
|
|||||||
if( !--count_ )
|
if( !--count_ )
|
||||||
delete this;
|
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ run 2-throw_exception_no_exceptions_test.cpp ;
|
|||||||
run 3-throw_exception_no_integration_test.cpp ;
|
run 3-throw_exception_no_integration_test.cpp ;
|
||||||
run 4-throw_exception_no_both_test.cpp ;
|
run 4-throw_exception_no_both_test.cpp ;
|
||||||
run cloning_test.cpp ;
|
run cloning_test.cpp ;
|
||||||
run copy_exception_test.cpp ;
|
run copy_exception_test.cpp /boost//thread : : : <threading>multi ;
|
||||||
run unknown_exception_test.cpp ;
|
run unknown_exception_test.cpp ;
|
||||||
run exception_test.cpp ;
|
run exception_test.cpp ;
|
||||||
run enable_error_info_test.cpp helper1.cpp ;
|
run enable_error_info_test.cpp helper1.cpp ;
|
||||||
|
@ -4,25 +4,106 @@
|
|||||||
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
|
||||||
#include <boost/exception_ptr.hpp>
|
#include <boost/exception_ptr.hpp>
|
||||||
|
#include <boost/exception/get_error_info.hpp>
|
||||||
|
#include <boost/thread.hpp>
|
||||||
#include <boost/detail/lightweight_test.hpp>
|
#include <boost/detail/lightweight_test.hpp>
|
||||||
|
|
||||||
|
typedef boost::error_info<struct tag_answer,int> answer;
|
||||||
|
|
||||||
struct
|
struct
|
||||||
test_exception:
|
err:
|
||||||
std::exception
|
virtual boost::exception,
|
||||||
|
virtual std::exception
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
int
|
class
|
||||||
main()
|
future
|
||||||
{
|
{
|
||||||
boost::exception_ptr p = boost::copy_exception(test_exception());
|
public:
|
||||||
|
|
||||||
|
future ():
|
||||||
|
ready_ (false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
set_exception( boost::exception_ptr const & e )
|
||||||
|
{
|
||||||
|
boost::unique_lock<boost::mutex> lck (mux_);
|
||||||
|
exc_ = e;
|
||||||
|
ready_ = true;
|
||||||
|
cond_.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
get_exception() const
|
||||||
|
{
|
||||||
|
boost::unique_lock<boost::mutex> lck (mux_);
|
||||||
|
while (! ready_)
|
||||||
|
cond_.wait (lck);
|
||||||
|
rethrow_exception (exc_);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
bool ready_;
|
||||||
|
boost::exception_ptr exc_;
|
||||||
|
mutable boost::mutex mux_;
|
||||||
|
mutable boost::condition_variable cond_;
|
||||||
|
};
|
||||||
|
|
||||||
|
void
|
||||||
|
producer( future & f )
|
||||||
|
{
|
||||||
|
f.set_exception (boost::copy_exception (err () << answer(42)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
consumer()
|
||||||
|
{
|
||||||
|
future f;
|
||||||
|
boost::thread thr (boost::bind (&producer, boost::ref (f)));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
f.get_exception ();
|
||||||
|
}
|
||||||
|
catch(
|
||||||
|
err & e )
|
||||||
|
{
|
||||||
|
int const * ans=boost::get_error_info<answer>(e);
|
||||||
|
BOOST_TEST(ans && *ans==42);
|
||||||
|
}
|
||||||
|
thr.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
consume()
|
||||||
|
{
|
||||||
|
for( int i=0; i!=100; ++i )
|
||||||
|
consumer();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
thread_test()
|
||||||
|
{
|
||||||
|
boost::thread_group grp;
|
||||||
|
for( int i=0; i!=50; ++i )
|
||||||
|
grp.create_thread(&consume);
|
||||||
|
grp.join_all ();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
simple_test()
|
||||||
|
{
|
||||||
|
boost::exception_ptr p = boost::copy_exception(err());
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
rethrow_exception(p);
|
rethrow_exception(p);
|
||||||
BOOST_TEST(false);
|
BOOST_TEST(false);
|
||||||
}
|
}
|
||||||
catch(
|
catch(
|
||||||
test_exception & )
|
err & )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
catch(
|
catch(
|
||||||
@ -30,5 +111,12 @@ main()
|
|||||||
{
|
{
|
||||||
BOOST_TEST(false);
|
BOOST_TEST(false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
simple_test();
|
||||||
|
thread_test();
|
||||||
return boost::report_errors();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user