1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Copyright (C) 2011,2014 Vicente J. Botet Escriba
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)
15 // <boost/thread/future.hpp>
19 // void promise::set_value(R&& r);
21 #define BOOST_THREAD_VERSION 3
23 #include <boost/thread/future.hpp>
24 #include <boost/detail/lightweight_test.hpp>
25 #include <boost/static_assert.hpp>
37 BOOST_THREAD_MOVABLE_ONLY(A)
39 A(BOOST_THREAD_RV_REF(A) rhs)
49 A& operator=(BOOST_THREAD_RV_REF(A) rhs)
70 BOOST_THREAD_MOVABLE_ONLY(movable2)
71 movable2() : value_(1){}
72 movable2(int i) : value_(i){}
74 //Move constructor and assignment
75 movable2(BOOST_RV_REF(movable2) m)
76 { value_ = m.value_; m.value_ = 0; }
78 movable2 & operator=(BOOST_THREAD_RV_REF(movable2) m)
79 { value_ = m.value_; m.value_ = 0; return *this; }
81 bool moved() const //Observer
84 int value() const //Observer
89 movable2 move_return_function2(int i) {
95 #if defined BOOST_NO_CXX11_RVALUE_REFERENCES
96 BOOST_STATIC_ASSERT((boost::is_copy_constructible<movable2>::value == false));
97 BOOST_STATIC_ASSERT((boost::has_move_emulation_enabled<movable2>::value == true));
98 BOOST_STATIC_ASSERT((boost::is_copy_constructible<A>::value == false));
99 BOOST_STATIC_ASSERT((boost::has_move_emulation_enabled<A>::value == true));
106 boost::future<T> f = p.get_future();
109 p.set_value(boost::move(i));
125 boost::future<T> f = p.get_future();
128 p.set_value_deferred(boost::move(i));
129 BOOST_TEST(!f.is_ready());
147 boost::future<T> f = p.get_future();
166 boost::future<T> f = p.get_future();
169 p.set_value_deferred((T()));
185 boost::future<T> f = p.get_future();
186 p.set_value(boost::move(i));
187 BOOST_TEST(f.get().value == 3);
191 p.set_value(boost::move(j));
194 catch (const boost::future_error& e)
196 BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied));
206 boost::promise<movable2> p;
207 boost::future<movable2> f = p.get_future();
208 p.set_value(move_return_function2(3));
209 BOOST_TEST(f.get().value_ == 3);
213 p.set_value(boost::move(j));
216 catch (const boost::future_error& e)
218 BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied));
228 boost::future<A> f = p.get_future();
229 p.set_value(make(3));
230 BOOST_TEST(f.get().value == 3);
234 p.set_value(boost::move(j));
237 catch (const boost::future_error& e)
239 BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied));
251 boost::future<T> f = p.get_future();
252 p.set_value(boost::move(i));
253 BOOST_TEST(i.value == 0);
254 boost::promise<T> p2(boost::move(p));
255 BOOST_TEST(f.get().value == 3);
262 boost::future<T> f = p.get_future();
263 p.set_value(boost::move(i));
264 BOOST_TEST(i.value == 0);
265 boost::promise<T> p2(boost::move(p));
266 boost::future<T> f2(boost::move(f));
267 BOOST_TEST(f2.get().value == 3);
274 p.set_value(boost::move(i));
275 BOOST_TEST(i.value == 0);
276 boost::promise<T> p2(boost::move(p));
277 boost::future<T> f = p2.get_future();
278 BOOST_TEST(f.get().value == 3);
283 typedef boost::future<int> T;
284 boost::promise<int> pi;
285 T fi=pi.get_future();
289 boost::future<T> f = p.get_future();
290 p.set_value(boost::move(fi));
291 boost::future<T> f2(boost::move(f));
292 BOOST_TEST(f2.get().get() == 3);
295 return boost::report_errors();