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 // template <class F, class Allocator>
20 //     explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
21 
22 
23 #define BOOST_THREAD_VERSION 4
24 #if BOOST_THREAD_VERSION == 4
25 #define BOOST_THREAD_DETAIL_SIGNATURE double()
26 #else
27 #define BOOST_THREAD_DETAIL_SIGNATURE double
28 #endif
29 
30 #include <boost/thread/detail/config.hpp>
31 #include <boost/thread/future.hpp>
32 #include <boost/detail/lightweight_test.hpp>
33 
34 
35 #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS
36 #include "../test_allocator.hpp"
37 
fct()38 double fct()
39 {
40   return 5.0;
41 }
lfct()42 long lfct()
43 {
44   return 5;
45 }
46 
47 class A
48 {
49   long data_;
50 
51 public:
52   BOOST_THREAD_COPYABLE_AND_MOVABLE(A)
53   static int n_moves;
54   static int n_copies;
55   static int n_instances;
56   static int n_destroy;
57 
A(long i)58   explicit A(long i) : data_(i)
59   {
60     ++n_instances;
61   }
A(BOOST_THREAD_RV_REF (A)a)62   A(BOOST_THREAD_RV_REF(A) a) : data_(BOOST_THREAD_RV(a).data_)
63   {
64     ++n_instances;
65     ++n_moves; BOOST_THREAD_RV(a).data_ = -1;
66   }
operator =(BOOST_THREAD_RV_REF (A)a)67   A& operator=(BOOST_THREAD_RV_REF(A) a)
68   {
69     data_ = BOOST_THREAD_RV(a).data_;
70     BOOST_THREAD_RV(a).data_ = -1;
71     ++n_moves;
72     return *this;
73   }
A(const A & a)74   A(const A& a) : data_(a.data_)
75   {
76     ++n_instances;
77     ++n_copies;
78   }
operator =(BOOST_THREAD_COPY_ASSIGN_REF (A)a)79   A& operator=(BOOST_THREAD_COPY_ASSIGN_REF(A) a)
80   {
81     data_ = a.data_;
82     ++n_copies;
83     return *this;
84   }
~A()85   ~A()
86   {
87     --n_instances;
88     ++n_destroy;
89   }
90 
operator ()() const91   long operator()() const
92   { return data_;}
operator ()(long i,long j) const93   long operator()(long i, long j) const
94   { return data_ + i + j;}
95 };
96 
97 int A::n_moves = 0;
98 int A::n_copies = 0;
99 int A::n_instances = 0;
100 int A::n_destroy = 0;
101 
main()102 int main()
103 {
104   {
105     boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(boost::allocator_arg,
106         test_allocator<A>(), BOOST_THREAD_MAKE_RV_REF(A(5)));
107     BOOST_TEST(test_alloc_base::count > 0);
108     BOOST_TEST(p.valid());
109     boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
110     //p(3, 'a');
111     p();
112     BOOST_TEST(f.get() == 5.0);
113   }
114   BOOST_TEST(A::n_copies == 0);
115   BOOST_TEST(A::n_moves > 0);
116   BOOST_TEST(A::n_instances == 0);
117   BOOST_TEST(A::n_destroy > 0);
118   BOOST_TEST(test_alloc_base::count == 0);
119   A::n_copies = 0;
120   A::n_moves = 0;
121   {
122     A a(5);
123     boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(boost::allocator_arg,
124         test_allocator<A>(), a);
125     BOOST_TEST(test_alloc_base::count > 0);
126     BOOST_TEST(p.valid());
127     boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
128     //p(3, 'a');
129     p();
130     BOOST_TEST(f.get() == 5.0);
131   }
132   //BOOST_TEST(A::n_copies > 0);
133   //BOOST_TEST(A::n_moves > 0);
134   BOOST_TEST(test_alloc_base::count == 0);
135   A::n_copies = 0;
136   A::n_moves = 0;
137   {
138     const A a(5);
139     boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(boost::allocator_arg,
140         test_allocator<A>(), a);
141     BOOST_TEST(test_alloc_base::count > 0);
142     BOOST_TEST(p.valid());
143     boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
144     //p(3, 'a');
145     p();
146     BOOST_TEST(f.get() == 5.0);
147   }
148   //BOOST_TEST(A::n_copies > 0);
149   //BOOST_TEST(A::n_moves > 0);
150   BOOST_TEST(test_alloc_base::count == 0);
151   {
152     boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(boost::allocator_arg,
153         test_allocator<A>(), fct);
154     BOOST_TEST(test_alloc_base::count > 0);
155     BOOST_TEST(p.valid());
156     boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
157     //p(3, 'a');
158     p();
159     BOOST_TEST(f.get() == 5.0);
160   }
161   {
162     boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(boost::allocator_arg,
163         test_allocator<A>(), &lfct);
164     BOOST_TEST(test_alloc_base::count > 0);
165     BOOST_TEST(p.valid());
166     boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
167     //p(3, 'a');
168     p();
169     BOOST_TEST(f.get() == 5.0);
170   }
171 
172   return boost::report_errors();
173 }
174 
175 #else
main()176 int main()
177 {
178   return boost::report_errors();
179 }
180 #endif
181 
182