Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r59259 - trunk/libs/exception/test
From: emil_at_[hidden]
Date: 2010-01-24 16:18:29


Author: emildotchevski
Date: 2010-01-24 16:18:29 EST (Sun, 24 Jan 2010)
New Revision: 59259
URL: http://svn.boost.org/trac/boost/changeset/59259

Log:
Adding another test for exception_ptr.
Added:
   trunk/libs/exception/test/exception_ptr_test.cpp (contents, props changed)
Text files modified:
   trunk/libs/exception/test/Jamfile.v2 | 1 +
   1 files changed, 1 insertions(+), 0 deletions(-)

Modified: trunk/libs/exception/test/Jamfile.v2
==============================================================================
--- trunk/libs/exception/test/Jamfile.v2 (original)
+++ trunk/libs/exception/test/Jamfile.v2 2010-01-24 16:18:29 EST (Sun, 24 Jan 2010)
@@ -36,6 +36,7 @@
 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 ;

Added: trunk/libs/exception/test/exception_ptr_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/exception/test/exception_ptr_test.cpp 2010-01-24 16:18:29 EST (Sun, 24 Jan 2010)
@@ -0,0 +1,108 @@
+//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>
+
+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(
+ ... )
+ {
+ BOOST_ERROR(boost::current_exception_diagnostic_information().c_str());
+ return 42;
+ }
+ }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk