1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // Copyright (C) 2011 Vicente J. Botet Escriba 11 // 12 // Distributed under the Boost Software License, Version 1.0. (See accompanying 13 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 14 15 // <boost/thread/future.hpp> 16 17 // class promise<R> 18 19 // void set_exception(exception_ptr p); 20 21 #define BOOST_THREAD_VERSION 3 22 23 #include <boost/thread/future.hpp> 24 #include <boost/detail/lightweight_test.hpp> 25 #include <boost/static_assert.hpp> 26 27 template <typename T> 28 struct wrap 29 { wrapwrap30 wrap(T const& v) : 31 value(v) 32 { 33 } 34 T value; 35 36 }; 37 38 template <typename T> make_exception_ptr(T v)39 boost::exception_ptr make_exception_ptr(T v) 40 { 41 return boost::copy_exception(wrap<T> (v)); 42 } 43 main()44int main() 45 { 46 47 { 48 typedef int T; 49 boost::promise<T> p; 50 boost::future<T> f = p.get_future(); 51 p.set_exception(::make_exception_ptr(3)); 52 try 53 { 54 f.get(); 55 BOOST_TEST(false); 56 } 57 catch (::wrap<int> i) 58 { 59 BOOST_TEST(i.value == 3); 60 } 61 try 62 { 63 p.set_exception(::make_exception_ptr(3)); 64 BOOST_TEST(false); 65 } 66 catch (const boost::future_error& e) 67 { 68 BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied)); 69 } 70 catch (...) 71 { 72 BOOST_TEST(false); 73 } 74 } 75 { 76 typedef int T; 77 boost::promise<T> p; 78 boost::future<T> f = p.get_future(); 79 p.set_exception_deferred(::make_exception_ptr(3)); 80 BOOST_TEST(!f.is_ready()); 81 p.notify_deferred(); 82 try 83 { 84 f.get(); 85 BOOST_TEST(false); 86 } 87 catch (::wrap<int> i) 88 { 89 BOOST_TEST(i.value == 3); 90 } 91 try 92 { 93 p.set_exception(::make_exception_ptr(3)); 94 BOOST_TEST(false); 95 } 96 catch (const boost::future_error& e) 97 { 98 BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied)); 99 } 100 catch (...) 101 { 102 BOOST_TEST(false); 103 } 104 } 105 return boost::report_errors(); 106 } 107 108