updating from trunk.

[SVN r60338]
This commit is contained in:
Emil Dotchevski
2010-03-08 08:18:25 +00:00
parent 879f416926
commit 029bc12c85
19 changed files with 474 additions and 230 deletions

View File

@ -24,7 +24,7 @@ run 2-throw_exception_no_exceptions_test.cpp ;
run 3-throw_exception_no_integration_test.cpp ;
run 4-throw_exception_no_both_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 exception_test.cpp ;
run enable_error_info_test.cpp helper1.cpp ;
@ -36,6 +36,7 @@ run refcount_ptr_test.cpp ;
run current_exception_cast_test.cpp ;
run no_exceptions_test.cpp : : : <exception-handling>off ;
run errinfos_test.cpp ;
run exception_ptr_test.cpp /boost//thread : : : <threading>multi ;
compile-fail exception_fail.cpp ;
compile-fail throw_exception_fail.cpp ;
compile-fail error_info_const_fail.cpp ;

View File

@ -4,25 +4,106 @@
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/exception_ptr.hpp>
#include <boost/exception/get_error_info.hpp>
#include <boost/thread.hpp>
#include <boost/detail/lightweight_test.hpp>
typedef boost::error_info<struct tag_answer,int> answer;
struct
test_exception:
std::exception
err:
virtual boost::exception,
virtual std::exception
{
};
int
main()
class
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
{
rethrow_exception(p);
BOOST_TEST(false);
}
catch(
test_exception & )
err & )
{
}
catch(
@ -30,5 +111,12 @@ main()
{
BOOST_TEST(false);
}
}
int
main()
{
simple_test();
thread_test();
return boost::report_errors();
}

View File

@ -34,7 +34,7 @@ main()
e <<
errinfo_api_function("failed_api_function") <<
errinfo_at_line(42) <<
errinfo_errno(errno) <<
errinfo_errno(0) <<
errinfo_file_handle(weak_ptr<FILE>()) <<
errinfo_file_name("filename.txt") <<
errinfo_file_open_mode("rb");

View File

@ -5,6 +5,7 @@
#include <boost/exception/get_error_info.hpp>
#include <boost/exception/info_tuple.hpp>
#include <boost/exception_ptr.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <boost/detail/workaround.hpp>
@ -302,7 +303,7 @@ test_add_tuple()
}
void
test_lifetime()
test_lifetime1()
{
int count=0;
try
@ -323,6 +324,19 @@ test_lifetime()
BOOST_TEST(!count);
}
void
test_lifetime2()
{
int count=0;
{
boost::exception_ptr ep;
test_exception e; e<<test_7(user_data(count));
ep=boost::copy_exception(e);
BOOST_TEST(count>0);
}
BOOST_TEST(!count);
}
bool
is_const( int const * )
{
@ -354,7 +368,8 @@ main()
test_basic_throw_catch();
test_catch_add_info();
test_add_tuple();
test_lifetime();
test_lifetime1();
test_lifetime2();
test_const();
return boost::report_errors();
}

112
test/exception_ptr_test.cpp Normal file
View File

@ -0,0 +1,112 @@
//Copyright (c) 2006-2009 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_ptr.hpp>
#include <boost/exception/info.hpp>
#include <boost/exception/get_error_info.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>
class thread_handle;
boost::shared_ptr<thread_handle> create_thread( boost::function<void()> const & f );
void join( thread_handle & t );
class
thread_handle
{
thread_handle( thread_handle const & );
thread_handle & operator=( thread_handle const & );
boost::exception_ptr err_;
boost::thread t_;
static
void
thread_wrapper( boost::function<void()> const & f, boost::exception_ptr & ep )
{
BOOST_ASSERT(!ep);
try
{
f();
}
catch(...)
{
ep = boost::current_exception();
}
}
explicit
thread_handle( boost::function<void()> const & f ):
t_(boost::bind(thread_wrapper,f,err_))
{
}
friend boost::shared_ptr<thread_handle> create_thread( boost::function<void()> const & f );
friend void join( thread_handle & t );
};
boost::shared_ptr<thread_handle>
create_thread( boost::function<void()> const & f )
{
boost::shared_ptr<thread_handle> t( new thread_handle(f) );
return t;
}
void
join( thread_handle & t )
{
t.t_.join();
if( t.err_ )
rethrow_exception(t.err_);
}
struct exc: boost::exception, std::exception { };
typedef boost::error_info<struct answer_,int> answer;
void
thread_func()
{
BOOST_THROW_EXCEPTION(exc() << answer(42));
}
void
check( boost::shared_ptr<thread_handle> const & t )
{
try
{
join(*t);
}
catch(
exc & e )
{
int const * a = boost::get_error_info<answer>(e);
BOOST_TEST(a && *a==42);
}
}
int
main()
{
try
{
std::vector< boost::shared_ptr<thread_handle> > threads;
std::generate_n(std::inserter(threads,threads.end()),256,boost::bind(create_thread,thread_func));
std::for_each(threads.begin(),threads.end(),check);
return boost::report_errors();
}
catch(
... )
{
std::cerr <<
"Caught unexpected exception.\n"
"Output from current_exception_diagnostic_information:\n" <<
boost::current_exception_diagnostic_information() << std::endl;
return 42;
}
}

View File

@ -4,7 +4,6 @@
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/exception/detail/is_output_streamable.hpp>
#include <boost/detail/lightweight_test.hpp>
namespace
n1
@ -31,11 +30,20 @@ n2
}
}
template <bool Test>
struct test;
template <>
struct
test<true>
{
};
int
main()
{
BOOST_TEST( !boost::is_output_streamable<n1::c1>::value );
BOOST_TEST( boost::is_output_streamable<n2::c2>::value );
BOOST_TEST( boost::is_output_streamable<int>::value );
return boost::report_errors();
test<!boost::is_output_streamable<n1::c1>::value>();
test<boost::is_output_streamable<n2::c2>::value>();
test<boost::is_output_streamable<int>::value>();
return 0;
}