N2179 compliance (pending documentation update)

[SVN r44438]
This commit is contained in:
Emil Dotchevski
2008-04-15 18:24:46 +00:00
parent 08607bca72
commit c61e655ada
9 changed files with 225 additions and 33 deletions

View File

@ -5,7 +5,7 @@
//This example shows how to enable cloning when throwing a boost::exception. //This example shows how to enable cloning when throwing a boost::exception.
#include <boost/exception/enable_exception_cloning.hpp> #include <boost/exception/enable_current_exception.hpp>
#include <boost/exception/info.hpp> #include <boost/exception/info.hpp>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
@ -18,5 +18,6 @@ void
file_read( FILE * f, void * buffer, size_t size ) file_read( FILE * f, void * buffer, size_t size )
{ {
if( size!=fread(buffer,1,size,f) ) if( size!=fread(buffer,1,size,f) )
throw boost::enable_exception_cloning(file_read_error()) << errno_info(errno); throw boost::enable_current_exception(file_read_error()) <<
errno_info(errno);
} }

View File

@ -20,13 +20,13 @@ worker_thread( boost::exception_ptr & error )
error = boost::exception_ptr(); error = boost::exception_ptr();
} }
catch( catch(
boost::exception & e ) ... )
{ {
error = boost::clone_exception(e); error = boost::current_exception();
} }
} }
// // ...continued
void void
work() work()

View File

@ -6,10 +6,10 @@
#ifndef UUID_FA5836A2CADA11DC8CD47C8555D89593 #ifndef UUID_FA5836A2CADA11DC8CD47C8555D89593
#define UUID_FA5836A2CADA11DC8CD47C8555D89593 #define UUID_FA5836A2CADA11DC8CD47C8555D89593
#include <boost/exception/enable_exception_cloning.hpp> #include <boost/exception/enable_current_exception.hpp>
#include <boost/exception/exception.hpp> #include <boost/exception/exception.hpp>
#include <boost/exception/detail/cloning_base.hpp> #include <boost/exception/detail/cloning_base.hpp>
#include <exception> #include <stdexcept>
namespace namespace
boost boost
@ -21,31 +21,165 @@ boost
{ {
public: public:
explicit
unknown_exception() unknown_exception()
{ {
} }
explicit explicit
unknown_exception( boost::exception const & x ): unknown_exception( boost::exception const & e ):
boost::exception(x) boost::exception(e)
{ {
} }
}; };
typedef intrusive_ptr<exception_detail::clone_base const> exception_ptr; typedef intrusive_ptr<exception_detail::clone_base const> exception_ptr;
namespace
exception_detail
{
template <class T>
class
current_exception_std_exception_wrapper:
public T,
public boost::exception
{
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)
{
}
};
template <class T>
exception_ptr
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)));
else
return exception_ptr(exception_detail::make_clone(current_exception_std_exception_wrapper<T>(e1)));
}
inline
exception_ptr
current_exception_unknown_exception()
{
return exception_ptr(exception_detail::make_clone(unknown_exception()));
}
inline
exception_ptr
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)));
else
return current_exception_unknown_exception();
}
inline
exception_ptr
current_exception_unknown_boost_exception( boost::exception const & e )
{
return exception_ptr(exception_detail::make_clone(unknown_exception(e)));
}
}
inline
exception_ptr
current_exception()
{
try
{
throw;
}
catch(
exception_detail::cloning_base & e )
{
exception_detail::clone_base const * c = e.clone();
BOOST_ASSERT(c!=0);
return exception_ptr(c);
}
catch(
... )
{
}
try
{
throw;
}
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();
}
}
template <class T> template <class T>
exception_ptr exception_ptr
clone_exception( T const & e ) copy_exception( T const & e )
{ {
if( boost::exception_detail::cloning_base const * cb = dynamic_cast<boost::exception_detail::cloning_base const *>(&e) ) try
if( exception_detail::clone_base const * c = cb->clone() ) {
return exception_ptr(c); throw enable_current_exception(e);
if( boost::exception const * be = dynamic_cast<boost::exception const *>(&e) ) }
return exception_ptr(exception_detail::make_clone(unknown_exception(*be))); catch( ... )
else {
return exception_ptr(exception_detail::make_clone(unknown_exception())); return current_exception();
}
} }
inline inline

View File

@ -139,7 +139,7 @@ boost
template <class T> template <class T>
exception_detail::clone_impl<T> exception_detail::clone_impl<T>
enable_exception_cloning( T const & x ) enable_current_exception( T const & x )
{ {
return exception_detail::clone_impl<T>(x); return exception_detail::clone_impl<T>(x);
} }

View File

@ -16,6 +16,7 @@ compile-fail to_string_fail.cpp ;
#exception #exception
run cloning_test.cpp ; run cloning_test.cpp ;
run copy_exception_test.cpp ;
run unknown_exception_test.cpp ; run unknown_exception_test.cpp ;
run exception_test.cpp ; run exception_test.cpp ;
run boost_error_info_test.cpp ; run boost_error_info_test.cpp ;

View File

@ -17,12 +17,12 @@ main()
{ {
try try
{ {
throw boost::enable_exception_cloning(test_exception()); throw boost::enable_current_exception(test_exception());
} }
catch( catch(
std::exception & x ) ... )
{ {
boost::exception_ptr p = boost::clone_exception(x); boost::exception_ptr p = boost::current_exception();
try try
{ {
rethrow_exception(p); rethrow_exception(p);

View File

@ -0,0 +1,34 @@
//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)
#include <boost/exception/cloning.hpp>
#include <boost/detail/lightweight_test.hpp>
struct
test_exception:
std::exception
{
};
int
main()
{
boost::exception_ptr p = boost::copy_exception(test_exception());
try
{
rethrow_exception(p);
BOOST_TEST(false);
}
catch(
test_exception & )
{
}
catch(
... )
{
BOOST_TEST(false);
}
return boost::report_errors();
}

View File

@ -35,9 +35,9 @@ tester()
BOOST_ASSERT(false); BOOST_ASSERT(false);
} }
catch( catch(
std::exception & x ) ... )
{ {
boost::exception_ptr p = boost::clone_exception(x); boost::exception_ptr p = boost::current_exception();
try try
{ {
rethrow_exception(p); rethrow_exception(p);
@ -55,11 +55,6 @@ tester()
BOOST_TEST(false); BOOST_TEST(false);
} }
} }
catch(
... )
{
BOOST_TEST(false);
}
} }
int int

View File

@ -40,9 +40,9 @@ main()
throw_boost_exception(); throw_boost_exception();
} }
catch( catch(
boost::exception & x ) ... )
{ {
boost::exception_ptr ep=boost::clone_exception(x); boost::exception_ptr ep=boost::current_exception();
try try
{ {
rethrow_exception(ep); rethrow_exception(ep);
@ -53,6 +53,20 @@ main()
BOOST_TEST( 42==*boost::get_error_info<test>(x) ); BOOST_TEST( 42==*boost::get_error_info<test>(x) );
} }
catch( catch(
... )
{
BOOST_TEST(false);
}
try
{
rethrow_exception(ep);
}
catch(
boost::exception & x )
{
BOOST_TEST( 42==*boost::get_error_info<test>(x) );
}
catch(
... ) ... )
{ {
BOOST_TEST(false); BOOST_TEST(false);
@ -63,9 +77,9 @@ main()
throw_unknown_exception(); throw_unknown_exception();
} }
catch( catch(
std::exception & x ) ... )
{ {
boost::exception_ptr ep=boost::clone_exception(x); boost::exception_ptr ep=boost::current_exception();
try try
{ {
rethrow_exception(ep); rethrow_exception(ep);
@ -75,6 +89,19 @@ main()
{ {
} }
catch( catch(
... )
{
BOOST_TEST(false);
}
try
{
rethrow_exception(ep);
}
catch(
boost::exception & )
{
}
catch(
... ) ... )
{ {
BOOST_TEST(false); BOOST_TEST(false);