Define boost::wrapexcept when BOOST_NO_EXCEPTIONS is defined, to enable boost::make_exception_ptr

This commit is contained in:
Peter Dimov
2020-12-20 02:03:11 +02:00
parent 397bc3d675
commit f3b43b5679
5 changed files with 126 additions and 33 deletions

View File

@ -20,6 +20,7 @@
// http://www.boost.org/libs/throw_exception
//
#include <boost/exception/exception.hpp>
#include <boost/assert/source_location.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
@ -32,29 +33,12 @@
namespace boost
{
// All boost exceptions are required to derive from std::exception,
// to ensure compatibility with BOOST_NO_EXCEPTIONS.
inline void throw_exception_assert_compatibility( std::exception const & ) {}
} // namespace boost
#if defined( BOOST_NO_EXCEPTIONS )
namespace boost
{
BOOST_NORETURN void throw_exception( std::exception const & e ); // user defined
BOOST_NORETURN void throw_exception( std::exception const & e, boost::source_location const & loc ); // user defined
} // namespace boost
#else // !defined( BOOST_NO_EXCEPTIONS )
#include <boost/exception/exception.hpp>
namespace boost
{
#endif
// boost::wrapexcept<E>
@ -134,17 +118,29 @@ public:
virtual void rethrow() const BOOST_OVERRIDE
{
#if defined( BOOST_NO_EXCEPTIONS )
boost::throw_exception( *this );
#else
throw *this;
#endif
}
};
} // namespace boost
// All boost exceptions are required to derive from std::exception,
// to ensure compatibility with BOOST_NO_EXCEPTIONS.
inline void throw_exception_assert_compatibility( std::exception const & ) {}
// boost::throw_exception
#if !defined( BOOST_NO_EXCEPTIONS )
#if defined( BOOST_EXCEPTION_DISABLE )
namespace boost
{
template<class E> BOOST_NORETURN void throw_exception( E const & e )
{
throw_exception_assert_compatibility( e );
@ -157,13 +153,7 @@ template<class E> BOOST_NORETURN void throw_exception( E const & e, boost::sourc
throw e;
}
} // namespace boost
#else // !defined( BOOST_EXCEPTION_DISABLE )
namespace boost
{
// boost::throw_exception
#else // defined( BOOST_EXCEPTION_DISABLE )
template<class E> BOOST_NORETURN void throw_exception( E const & e )
{
@ -177,12 +167,12 @@ template<class E> BOOST_NORETURN void throw_exception( E const & e, boost::sourc
throw wrapexcept<E>( e, loc );
}
#endif // defined( BOOST_EXCEPTION_DISABLE )
#endif // !defined( BOOST_NO_EXCEPTIONS )
} // namespace boost
#endif // # if defined( BOOST_EXCEPTION_DISABLE )
#endif // # if defined( BOOST_NO_EXCEPTIONS )
// BOOST_THROW_EXCEPTION
#define BOOST_THROW_EXCEPTION(x) ::boost::throw_exception(x, BOOST_CURRENT_LOCATION)

View File

@ -37,6 +37,10 @@ run throw_from_library_test.cpp lib1_throw lib2_throw lib3_throw : : : <link>sha
run throw_exception_nx_test.cpp : : : <exception-handling>off ;
run throw_exception_nx_test2.cpp : : : <exception-handling>off ;
run throw_exception_nx_test3.cpp : : : <exception-handling>off ;
run make_exception_ptr_test.cpp ;
run make_exception_ptr_test2.cpp ;
run make_exception_ptr_nx_test.cpp : : : <exception-handling>off ;
run make_exception_ptr_nx_test2.cpp : : : <exception-handling>off ;

View File

@ -0,0 +1,40 @@
// Copyright 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning(disable: 4702) // unreachable code
# pragma warning(disable: 4577) // noexcept used without /EHsc
#endif
//#include <boost/exception_ptr.hpp>
#include <boost/throw_exception.hpp>
#include <boost/exception/exception.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
typedef boost::shared_ptr<boost::exception_detail::clone_base const> exception_ptr;
template<class E> exception_ptr make_exception_ptr( E const& e )
{
return boost::make_shared< boost::wrapexcept<E> >( e );
}
class my_exception: public std::exception {};
int main()
{
::make_exception_ptr( my_exception() );
}
namespace boost
{
// shared_ptr needs this
void throw_exception( std::exception const & )
{
std::exit( 1 );
}
} // namespace boost

View File

@ -0,0 +1,42 @@
// Copyright 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning(disable: 4702) // unreachable code
# pragma warning(disable: 4577) // noexcept used without /EHsc
#endif
#define BOOST_EXCEPTION_DISABLE
//#include <boost/exception_ptr.hpp>
#include <boost/throw_exception.hpp>
#include <boost/exception/exception.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
typedef boost::shared_ptr<boost::exception_detail::clone_base const> exception_ptr;
template<class E> exception_ptr make_exception_ptr( E const& e )
{
return boost::make_shared< boost::wrapexcept<E> >( e );
}
class my_exception: public std::exception {};
int main()
{
::make_exception_ptr( my_exception() );
}
namespace boost
{
// shared_ptr needs this
void throw_exception( std::exception const & )
{
std::exit( 1 );
}
} // namespace boost

View File

@ -0,0 +1,17 @@
// Copyright 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning(disable: 4702) // unreachable code
# pragma warning(disable: 4577) // noexcept used without /EHsc
#endif
// Make sure that simple inclusion does not
// require boost::throw_exception to be defined
#include <boost/throw_exception.hpp>
int main()
{
}