• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // class packaged_task<R>
17 
18 // void operator()();
19 
20 
21 #define BOOST_THREAD_VERSION 4
22 #if BOOST_THREAD_VERSION == 4
23 #define BOOST_THREAD_DETAIL_SIGNATURE double()
24 #else
25 #define BOOST_THREAD_DETAIL_SIGNATURE double
26 #endif
27 
28 #include <boost/thread/future.hpp>
29 #include <boost/detail/lightweight_test.hpp>
30 
31 class A
32 {
33   long data_;
34 
35 public:
A(long i)36   explicit A(long i) :
37     data_(i)
38   {
39   }
40 
operator ()() const41   long operator()() const
42   {
43     return data_;
44   }
operator ()(long i,long j) const45   long operator()(long i, long j) const
46   {
47     if (j == 'z') throw A(6);
48     return data_ + i + j;
49   }
50 };
51 
main()52 int main()
53 {
54   {
55     boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(A(5));
56     boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
57     //p(3, 'a');
58     p();
59     BOOST_TEST(f.get() == 5.0);
60     p.reset();
61     //p(4, 'a');
62     p();
63     f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
64     BOOST_TEST(f.get() == 5.0);
65   }
66   {
67     boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p;
68     try
69     {
70       p.reset();
71       BOOST_TEST(false);
72     }
73     catch (const boost::future_error& e)
74     {
75       BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::no_state));
76     }
77   }
78 
79   return boost::report_errors();
80 }
81 
82