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 packaged_task<R>
18 
19 // ~packaged_task();
20 
21 //#define BOOST_THREAD_VERSION 3
22 #define BOOST_THREAD_VERSION 4
23 
24 
25 #include <boost/thread/future.hpp>
26 #include <boost/thread/thread.hpp>
27 #include <boost/detail/lightweight_test.hpp>
28 
29 #if BOOST_THREAD_VERSION == 4
30 #define BOOST_THREAD_DETAIL_SIGNATURE double()
31 #else
32 #define BOOST_THREAD_DETAIL_SIGNATURE double
33 #endif
34 
35 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
36 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
37 #define BOOST_THREAD_DETAIL_SIGNATURE_2 double(int, char)
38 #define BOOST_THREAD_DETAIL_SIGNATURE_2_RES 5 + 3 +'a'
39 #else
40 #define BOOST_THREAD_DETAIL_SIGNATURE_2 double()
41 #define BOOST_THREAD_DETAIL_SIGNATURE_2_RES 5
42 #endif
43 #else
44 #define BOOST_THREAD_DETAIL_SIGNATURE_2 double
45 #define BOOST_THREAD_DETAIL_SIGNATURE_2_RES 5
46 #endif
47 
48 class A
49 {
50     long data_;
51 
52 public:
A(long i)53     explicit A(long i) : data_(i) {}
54 
operator ()() const55     long operator()() const {return data_;}
operator ()(long i,long j) const56     long operator()(long i, long j) const {return data_ + i + j;}
57 };
58 
func(boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE>)59 void func(boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> )
60 {
61 }
62 
func2(boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE_2> p)63 void func2(boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE_2> p)
64 {
65 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
66   p(3, 'a');
67 #else
68   p();
69 #endif
70 }
71 
main()72 int main()
73 {
74   {
75       boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(A(5));
76       boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
77 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
78       boost::thread(func, boost::move(p)).detach();
79 #else
80       boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE>* p2=new boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE>(boost::move(p));
81       delete p2;
82 #endif
83       try
84       {
85           f.get();
86           BOOST_TEST(false);
87       }
88       catch (const boost::future_error& e)
89       {
90           BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::broken_promise));
91       }
92   }
93   {
94       std::cout << __LINE__ << std::endl;
95       boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE_2> p(A(5));
96       boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
97 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
98       boost::thread(func2, boost::move(p)).detach();
99 #else
100       p();
101 #endif
102       std::cout << __LINE__ << std::endl;
103       BOOST_TEST(f.get() == BOOST_THREAD_DETAIL_SIGNATURE_2_RES);
104       std::cout << __LINE__ << std::endl;
105   }
106   return boost::report_errors();
107 }
108 
109