]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (C) 2010 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 | #include <boost/config.hpp> | |
7 | #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES | |
8 | ||
9 | #include <boost/detail/lightweight_test.hpp> | |
10 | #include <boost/thread/future.hpp> | |
11 | #include <boost/utility/result_of.hpp> | |
12 | #include <functional> | |
13 | ||
14 | struct async_func { | |
15 | virtual ~async_func() { } | |
16 | virtual void run() =0; | |
17 | }; | |
18 | ||
19 | template <typename Ret> | |
20 | class async_func_pt : public async_func { | |
21 | boost::packaged_task<Ret> f; | |
22 | public: | |
23 | void run() { | |
24 | std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
25 | f(); } | |
26 | async_func_pt (boost::packaged_task<Ret>&& f) : f(boost::move(f)) {} | |
27 | ~async_func_pt() { } | |
28 | boost::unique_future<Ret> get_future() { return f.get_future(); } | |
29 | }; | |
30 | ||
31 | void async_core (async_func* p) { | |
32 | std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
33 | p->run(); | |
34 | } | |
35 | ||
36 | ||
37 | ||
38 | ||
39 | template <typename F> | |
40 | boost::unique_future<typename boost::result_of< F() >::type> | |
41 | async (F&& f) | |
42 | { | |
43 | std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
44 | typedef typename boost::result_of< F() >::type RetType; | |
45 | std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
46 | async_func_pt<RetType>* p= new async_func_pt<RetType> (boost::packaged_task<RetType>(boost::forward<F>(f))); | |
47 | std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
48 | boost::unique_future<RetType> future_result= p->get_future(); | |
49 | std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
50 | async_core (p); | |
51 | std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
52 | return boost::move(future_result); | |
53 | } | |
54 | ||
55 | template <typename F, typename A1> | |
56 | boost::unique_future<typename boost::result_of< F(A1) >::type> | |
57 | async (F&& f, A1&& a1) | |
58 | { | |
59 | std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
60 | // This should be all it needs. But get a funny error deep inside Boost. | |
61 | // problem overloading with && ? | |
62 | return async (boost::bind(f,a1)); | |
63 | } | |
64 | ||
65 | ||
66 | int calculate_the_answer_to_life_the_universe_and_everything() | |
67 | { | |
68 | return 42; | |
69 | } | |
70 | ||
71 | ||
72 | size_t foo (const std::string& s) | |
73 | { | |
74 | return s.size(); | |
75 | } | |
76 | ||
77 | ||
78 | ||
79 | void test1() | |
80 | { | |
81 | // this one works | |
82 | // most fundimental form: | |
83 | std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
84 | boost::unique_future<int> fi= async (&calculate_the_answer_to_life_the_universe_and_everything); | |
85 | int i= fi.get(); | |
86 | BOOST_TEST (i== 42); | |
87 | ||
88 | ||
89 | // This one chokes at compile time | |
90 | std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
91 | boost::unique_future<size_t> fut_1= async (&foo, "Life"); | |
92 | ||
93 | BOOST_TEST (fut_1.get()== 4); | |
94 | } | |
95 | ||
96 | int main() | |
97 | { | |
98 | test1(); | |
99 | return boost::report_errors(); | |
100 | ||
101 | } | |
102 | ||
103 | #else | |
104 | int main() | |
105 | { | |
106 | return 0; | |
107 | } | |
108 | ||
109 | #endif | |
110 | /* | |
111 | * | |
112 | "/Users/viboes/clang/llvmCore-3.0-rc1.install/bin/clang++" | |
113 | -o "../../../bin.v2/libs/thread/test/test_ml.test/clang-darwin-3.0x/debug/threading-multi/test_ml" | |
114 | "../../../bin.v2/libs/thread/test/test_ml.test/clang-darwin-3.0x/debug/threading-multi/test_ml.o" | |
115 | "../../../bin.v2/libs/test/build/clang-darwin-3.0x/debug/link-static/threading-multi/libboost_unit_test_framework.a" | |
116 | "../../../bin.v2/libs/thread/build/clang-darwin-3.0x/debug/threading-multi/libboost_thread.dylib" | |
117 | "../../../bin.v2/libs/chrono/build/clang-darwin-3.0x/debug/threading-multi/libboost_chrono.dylib" | |
118 | "../../../bin.v2/libs/system/build/clang-darwin-3.0x/debug/threading-multi/libboost_system.dylib" -g | |
119 | */ | |
120 | ||
121 | ||
122 | /* | |
123 | * | |
124 | * #include <boost/test/unit_test.hpp> | |
125 | #include <boost/thread/future.hpp> | |
126 | #include <boost/utility/result_of.hpp> | |
127 | #include <functional> | |
128 | ||
129 | struct async_func { | |
130 | virtual ~async_func() { } | |
131 | virtual void run() =0; | |
132 | }; | |
133 | ||
134 | template <typename Ret> | |
135 | class async_func_pt : public async_func { | |
136 | boost::packaged_task<Ret> f; | |
137 | public: | |
138 | void run() override { f(); } | |
139 | async_func_pt (boost::packaged_task<Ret>&& f) : f(std::move(f)) {} | |
140 | ~async_func_pt() { } | |
141 | boost::unique_future<Ret> get_future() { return f.get_future(); } | |
142 | }; | |
143 | ||
144 | void async_core (async_func* p); | |
145 | ||
146 | ||
147 | ||
148 | ||
149 | template <typename F> | |
150 | boost::unique_future<typename boost::result_of< F() >::type> | |
151 | async (F&& f) | |
152 | { | |
153 | typedef typename boost::result_of< F() >::type RetType; | |
154 | async_func_pt<RetType>* p= new async_func_pt<RetType> (boost::packaged_task<RetType>(f)); | |
155 | boost::unique_future<RetType> future_result= p->get_future(); | |
156 | async_core (p); | |
157 | return std::move(future_result); | |
158 | } | |
159 | ||
160 | template <typename F, typename A1> | |
161 | boost::unique_future<typename boost::result_of< F(A1) >::type> | |
162 | async (F&& f, A1&& a1) | |
163 | { | |
164 | // This should be all it needs. But get a funny error deep inside Boost. | |
165 | // problem overloading with && ? | |
166 | return async (std::tr1::bind(f,a1)); | |
167 | } | |
168 | ||
169 | BOOST_AUTO_TEST_SUITE(thread_pool_tests) | |
170 | ||
171 | int calculate_the_answer_to_life_the_universe_and_everything() | |
172 | { | |
173 | return 42; | |
174 | } | |
175 | ||
176 | ||
177 | size_t foo (const std::string& s) | |
178 | { | |
179 | return s.size(); | |
180 | } | |
181 | ||
182 | ||
183 | ||
184 | BOOST_AUTO_TEST_CASE( async_test ) | |
185 | { | |
186 | // this one works | |
187 | // most fundimental form: | |
188 | boost::unique_future<int> fi= async (&calculate_the_answer_to_life_the_universe_and_everything); | |
189 | int i= fi.get(); | |
190 | BOOST_CHECK_EQUAL (i, 42); | |
191 | ||
192 | ||
193 | // This one chokes at compile time | |
194 | boost::unique_future<size_t> fut_1= async (&foo, "Life"); | |
195 | ||
196 | BOOST_CHECK_EQUAL (fut_1.get(), 4); | |
197 | } | |
198 | ||
199 | ||
200 | BOOST_AUTO_TEST_SUITE_END() | |
201 | */ |