2 // Copyright Oliver Kowalke 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_FIBERS_PROMISE_HPP
8 #define BOOST_FIBERS_PROMISE_HPP
14 #include <boost/config.hpp>
16 #include <boost/fiber/detail/convert.hpp>
17 #include <boost/fiber/exceptions.hpp>
18 #include <boost/fiber/future/detail/shared_state.hpp>
19 #include <boost/fiber/future/detail/shared_state_object.hpp>
20 #include <boost/fiber/future/future.hpp>
26 template< typename R >
28 typedef typename shared_state< R >::ptr_t ptr_t;
30 bool obtained_{ false };
34 promise_base{ std::allocator_arg, std::allocator< promise_base >{} } {
37 template< typename Allocator >
38 promise_base( std::allocator_arg_t, Allocator alloc) {
39 typedef detail::shared_state_object< R, Allocator > object_t;
40 typedef std::allocator_traits< typename object_t::allocator_t > traits_t;
41 typename object_t::allocator_t a{ alloc };
42 typename traits_t::pointer ptr{ traits_t::allocate( a, 1) };
45 traits_t::construct( a, ptr, a);
47 traits_t::deallocate( a, ptr, 1);
50 future_.reset( convert( ptr) );
55 future_->owner_destroyed();
59 promise_base( promise_base const&) = delete;
60 promise_base & operator=( promise_base const&) = delete;
62 promise_base( promise_base && other) noexcept :
63 obtained_{ other.obtained_ },
64 future_{ std::move( other.future_) } {
65 other.obtained_ = false;
68 promise_base & operator=( promise_base && other) noexcept {
69 if ( this == & other) return * this;
70 promise_base tmp{ std::move( other) };
75 future< R > get_future() {
77 throw future_already_retrieved{};
80 throw promise_uninitialized{};
83 return future< R >{ future_ };
86 void swap( promise_base & other) noexcept {
87 std::swap( obtained_, other.obtained_);
88 future_.swap( other.future_);
91 void set_exception( std::exception_ptr p) {
93 throw promise_uninitialized{};
95 future_->set_exception( p);
101 template< typename R >
102 class promise : private detail::promise_base< R > {
104 typedef detail::promise_base< R > base_t;
109 template< typename Allocator >
110 promise( std::allocator_arg_t, Allocator alloc) :
111 base_t{ std::allocator_arg, alloc } {
114 promise( promise const&) = delete;
115 promise & operator=( promise const&) = delete;
117 promise( promise && other) noexcept = default;
118 promise & operator=( promise && other) = default;
120 void set_value( R const& value) {
121 if ( ! base_t::future_) {
122 throw promise_uninitialized{};
124 base_t::future_->set_value( value);
127 void set_value( R && value) {
128 if ( ! base_t::future_) {
129 throw promise_uninitialized{};
131 base_t::future_->set_value( std::move( value) );
134 void swap( promise & other) noexcept {
135 base_t::swap( other);
138 using base_t::get_future;
139 using base_t::set_exception;
142 template< typename R >
143 class promise< R & > : private detail::promise_base< R & > {
145 typedef detail::promise_base< R & > base_t;
150 template< typename Allocator >
151 promise( std::allocator_arg_t, Allocator alloc) :
152 base_t{ std::allocator_arg, alloc } {
155 promise( promise const&) = delete;
156 promise & operator=( promise const&) = delete;
158 promise( promise && other) noexcept = default;
159 promise & operator=( promise && other) noexcept = default;
161 void set_value( R & value) {
162 if ( ! base_t::future_) {
163 throw promise_uninitialized{};
165 base_t::future_->set_value( value);
168 void swap( promise & other) noexcept {
169 base_t::swap( other);
172 using base_t::get_future;
173 using base_t::set_exception;
177 class promise< void > : private detail::promise_base< void > {
179 typedef detail::promise_base< void > base_t;
184 template< typename Allocator >
185 promise( std::allocator_arg_t, Allocator alloc) :
186 base_t{ std::allocator_arg, alloc } {
189 promise( promise const&) = delete;
190 promise & operator=( promise const&) = delete;
192 promise( promise && other) noexcept = default;
193 promise & operator=( promise && other) noexcept = default;
197 if ( ! base_t::future_) {
198 throw promise_uninitialized{};
200 base_t::future_->set_value();
204 void swap( promise & other) noexcept {
205 base_t::swap( other);
208 using base_t::get_future;
209 using base_t::set_exception;
212 template< typename R >
213 void swap( promise< R > & l, promise< R > & r) noexcept {
219 #endif // BOOST_FIBERS_PROMISE_HPP