1 | // Copyright (C) 2015 Vicente Botet |
2 | // |
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) |
5 | |
6 | #define BOOST_THREAD_VERSION 4 |
7 | #include <boost/config.hpp> |
8 | #if ! defined BOOST_NO_CXX11_DECLTYPE |
9 | #define BOOST_RESULT_OF_USE_DECLTYPE |
10 | #endif |
11 | #define BOOST_THREAD_PROVIDES_EXECUTORS |
12 | |
13 | #include <boost/thread/future.hpp> |
14 | #include <boost/static_assert.hpp> |
15 | #include <cassert> |
16 | #include <iostream> |
17 | #include <boost/thread/executors/basic_thread_pool.hpp> |
18 | |
19 | struct TestCallback |
20 | { |
21 | typedef boost::future<void> result_type; |
22 | |
23 | result_type operator()(boost::future<void> future) const |
24 | { |
25 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
26 | assert(future.is_ready()); |
27 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
28 | future.wait(); |
29 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
30 | return boost::make_ready_future(); |
31 | } |
32 | |
33 | result_type operator()(boost::future<boost::future<void> > future) const |
34 | { |
35 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
36 | assert(future.is_ready()); |
37 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
38 | assert(future.get().is_ready()); |
39 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
40 | //boost::future<void> ff = future.get(); |
41 | |
42 | return boost::make_ready_future(); |
43 | } |
44 | result_type operator()(boost::shared_future<void> future) const |
45 | { |
46 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
47 | assert(future.is_ready()); |
48 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
49 | future.wait(); |
50 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
51 | return boost::make_ready_future(); |
52 | } |
53 | |
54 | result_type operator()(boost::shared_future<boost::future<void> > future) const |
55 | { |
56 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
57 | assert(future.is_ready()); |
58 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
59 | assert(future.get().is_ready()); |
60 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
61 | //boost::future<void> ff = future.get(); |
62 | |
63 | return boost::make_ready_future(); |
64 | } |
65 | }; |
66 | |
67 | void p1() |
68 | { |
69 | } |
70 | |
71 | int main() |
72 | { |
73 | const int number_of_tests = 2; |
74 | (void)(number_of_tests); |
75 | |
76 | #if ! defined BOOST_NO_CXX11_DECLTYPE && ! defined BOOST_NO_CXX11_AUTO_DECLARATIONS |
77 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
78 | { |
79 | auto f1 = boost::make_ready_future().then(func: TestCallback()); |
80 | BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value); |
81 | f1.wait(); |
82 | } |
83 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
84 | for (int i=0; i< number_of_tests; i++) |
85 | { |
86 | auto f1 = boost::make_ready_future().then(func: TestCallback()); |
87 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
88 | BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value); |
89 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
90 | auto f2 = f1.unwrap(); |
91 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
92 | BOOST_STATIC_ASSERT(std::is_same<decltype(f2), boost::future<void> >::value); |
93 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
94 | f2.wait(); |
95 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
96 | } |
97 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
98 | for (int i=0; i< number_of_tests; i++) |
99 | { |
100 | auto f1 = boost::make_ready_future().then(func: TestCallback()); |
101 | BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value); |
102 | boost::future<void> f2 = f1.get(); |
103 | } |
104 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
105 | { |
106 | auto f1 = boost::make_ready_future().then(func: TestCallback()); |
107 | BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value); |
108 | auto f3 = f1.then(func: TestCallback()); |
109 | BOOST_STATIC_ASSERT(std::is_same<decltype(f3), boost::future<boost::future<void> > >::value); |
110 | f3.wait(); |
111 | } |
112 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
113 | for (int i=0; i< number_of_tests; i++) |
114 | { |
115 | auto f1 = boost::make_ready_future().then(func: TestCallback()); |
116 | BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value); |
117 | auto f2 = f1.unwrap(); |
118 | BOOST_STATIC_ASSERT(std::is_same<decltype(f2), boost::future<void> >::value); |
119 | auto f3 = f2.then(func: TestCallback()); |
120 | BOOST_STATIC_ASSERT(std::is_same<decltype(f3), boost::future<boost::future<void> > >::value); |
121 | f3.wait(); |
122 | } |
123 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
124 | for (int i=0; i< number_of_tests; i++) |
125 | { |
126 | boost::make_ready_future().then( |
127 | func: TestCallback()).unwrap().then(func: TestCallback()).get(); |
128 | } |
129 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
130 | for (int i=0; i< number_of_tests; i++) |
131 | { |
132 | boost::future<void> f = boost::async(f: p1); |
133 | f.then( |
134 | func: TestCallback()).unwrap().then(func: TestCallback()).get(); |
135 | } |
136 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
137 | for (int i=0; i< number_of_tests; i++) |
138 | { |
139 | auto f1 = boost::make_ready_future().then(func: TestCallback()); |
140 | BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value); |
141 | auto f3 = f1.then(func: TestCallback()); |
142 | BOOST_STATIC_ASSERT(std::is_same<decltype(f3), boost::future<boost::future<void> > >::value); |
143 | f3.wait(); |
144 | } |
145 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
146 | for (int i=0; i< number_of_tests; i++) |
147 | { |
148 | boost::basic_thread_pool executor; |
149 | auto f1 = boost::make_ready_future().then(ex&: executor, func: TestCallback()); |
150 | BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value); |
151 | auto f3 = f1.then(ex&: executor, func: TestCallback()); |
152 | BOOST_STATIC_ASSERT(std::is_same<decltype(f3), boost::future<boost::future<void> > >::value); |
153 | f3.wait(); |
154 | } |
155 | #if 1 |
156 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
157 | // fixme |
158 | for (int i=0; i< number_of_tests; i++) |
159 | { |
160 | boost::basic_thread_pool executor(2); |
161 | |
162 | auto f1 = boost::make_ready_future().then(ex&: executor, func: TestCallback()); |
163 | BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value); |
164 | std::cout << __FILE__ << "[" << __LINE__ << "] " << int(f1.valid()) << std::endl; |
165 | auto f2 = f1.unwrap(); |
166 | std::cout << __FILE__ << "[" << __LINE__ << "] " << int(f2.valid()) << std::endl; |
167 | |
168 | BOOST_STATIC_ASSERT(std::is_same<decltype(f2), boost::future<void> >::value); |
169 | auto f3 = f2.then(ex&: executor, func: TestCallback()); |
170 | BOOST_STATIC_ASSERT(std::is_same<decltype(f3), boost::future<boost::future<void> > >::value); |
171 | f3.wait(); |
172 | } |
173 | #endif |
174 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
175 | for (int i=0; i< number_of_tests; i++) |
176 | { |
177 | boost::basic_thread_pool executor; |
178 | |
179 | auto f1 = boost::make_ready_future().then(ex&: executor, func: TestCallback()); |
180 | BOOST_STATIC_ASSERT(std::is_same<decltype(f1), boost::future<boost::future<void> > >::value); |
181 | auto f2 = f1.unwrap(); |
182 | BOOST_STATIC_ASSERT(std::is_same<decltype(f2), boost::future<void> >::value); |
183 | auto f3 = f2.then(ex&: executor, func: TestCallback()); |
184 | BOOST_STATIC_ASSERT(std::is_same<decltype(f3), boost::future<boost::future<void> > >::value); |
185 | f3.wait(); |
186 | } |
187 | std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; |
188 | |
189 | #endif |
190 | return 0; |
191 | } |
192 | |