]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/exception/test/exception_ptr_test.cpp
1 //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
3 //Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/exception_ptr.hpp>
7 #include <boost/exception/info.hpp>
8 #include <boost/exception/get_error_info.hpp>
9 #include <boost/exception/diagnostic_information.hpp>
10 #include <boost/function.hpp>
11 #include <boost/bind.hpp>
12 #include <boost/thread.hpp>
13 #include <boost/detail/atomic_count.hpp>
14 #include <boost/detail/lightweight_test.hpp>
18 boost::shared_ptr
<thread_handle
> create_thread( boost::function
<void()> const & f
);
19 void join( thread_handle
& t
);
24 thread_handle( thread_handle
const & );
25 thread_handle
& operator=( thread_handle
const & );
27 boost::exception_ptr err_
;
32 thread_wrapper( boost::function
<void()> const & f
, boost::exception_ptr
& ep
)
41 ep
= boost::current_exception();
46 thread_handle( boost::function
<void()> const & f
):
47 t_(boost::bind(thread_wrapper
,f
,boost::ref(err_
)))
51 friend boost::shared_ptr
<thread_handle
> create_thread( boost::function
<void()> const & f
);
52 friend void join( thread_handle
& t
);
55 boost::shared_ptr
<thread_handle
>
56 create_thread( boost::function
<void()> const & f
)
58 boost::shared_ptr
<thread_handle
> t( new thread_handle(f
) );
63 join( thread_handle
& t
)
67 rethrow_exception(t
.err_
);
70 boost::detail::atomic_count
exc_count(0);
74 virtual boost::exception
,
75 virtual std::exception
97 exc
& operator=( exc
const & );
100 typedef boost::error_info
<struct answer_
,int> answer
;
105 BOOST_THROW_EXCEPTION(exc() << answer(42));
109 check( boost::shared_ptr
<thread_handle
> const & t
)
119 int const * a
= boost::get_error_info
<answer
>(e
);
120 BOOST_TEST(a
&& *a
==42);
128 boost::exception_ptr p
;
131 BOOST_THROW_EXCEPTION(exc() << answer(42));
132 BOOST_ERROR("BOOST_THROW_EXCEPTION didn't throw");
137 p1
=boost::get_error_info
<answer
>(e
);
138 p
=boost::current_exception();
144 boost::rethrow_exception(p
);
145 BOOST_ERROR("rethrow_exception didn't throw");
150 int const * p2
=boost::get_error_info
<answer
>(e
);
151 BOOST_TEST(p2
!=0 && *p2
==42);
160 BOOST_TEST(++exc_count
==1);
163 std::vector
< boost::shared_ptr
<thread_handle
> > threads
;
164 std::generate_n(std::inserter(threads
,threads
.end()),1,boost::bind(create_thread
,thread_func
));
165 std::for_each(threads
.begin(),threads
.end(),check
);
166 return boost::report_errors();
172 "Caught unexpected exception.\n"
173 "Output from current_exception_diagnostic_information:\n" <<
174 boost::current_exception_diagnostic_information() << std::endl
;
177 BOOST_TEST(!--exc_count
);