source: mainline/uspace/lib/cpp/include/__bits/thread/shared_future.hpp@ 1621f91

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1621f91 was 1621f91, checked in by Jaroslav Jindrak <dzejrou@…>, 6 years ago

cpp: fix return types for future<R>::get and shared_future<R>::get

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * Copyright (c) 2019 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_BITS_THREAD_SHARED_FUTURE
30#define LIBCPP_BITS_THREAD_SHARED_FUTURE
31
32#include <__bits/thread/future.hpp>
33#include <__bits/thread/future_common.hpp>
34#include <type_traits>
35
36/**
37 * Note: We do synchronization directly on the shared
38 * state, this means that with the exception of
39 * some member functions (which are only defined
40 * for specializations anyway) our future and
41 * shared_future are basically identical. Because
42 * of that, we can use aux::future_base for
43 * shared_future as well.
44 */
45
46namespace std
47{
48 /**
49 * 30.6.7, class template shared_future:
50 */
51
52 template<class R>
53 class shared_future: public aux::future_base<aux::future_inner_t<R>>
54 {
55 public:
56 shared_future() noexcept = default;
57
58 shared_future(const shared_future&) = default;
59
60 shared_future(shared_future&&) noexcept = default;
61
62 shared_future(future<R>&& rhs)
63 : aux::future_base<aux::future_inner_t<R>>{move(rhs.state_)}
64 {
65 rhs.state_ = nullptr;
66 }
67
68 shared_future& operator=(const shared_future&) = default;
69
70 shared_future& operator=(shared_future&&) noexcept = default;
71
72 aux::future_return_shared_t<R> get() const
73 {
74 assert(this->state_);
75
76 this->wait();
77
78 if (this->state_->has_exception())
79 this->state_->throw_stored_exception();
80
81 /**
82 * Using constexpr if and the future_inner and future_result
83 * metafunctions we can actually avoid having to create specializations
84 * for R& and void in this case.
85 */
86 if constexpr (!is_same_v<R, void>)
87 {
88 if constexpr (is_reference_v<R>)
89 {
90 assert(this->state_->get());
91
92 return *this->state_->get();
93 }
94 else
95 return this->state_->get();
96 }
97 }
98 };
99}
100
101#endif
Note: See TracBrowser for help on using the repository browser.