adding Promise::await
[folly.git] / folly / experimental / fibers / WhenN.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17
18 namespace folly {
19 namespace fibers {
20
21 /**
22  * Schedules several tasks and blocks until n of these tasks are completed.
23  * If any of these n tasks throws an exception, this exception will be
24  * re-thrown, but only when n tasks are complete. If several tasks throw
25  * exceptions one of them will be re-thrown.
26  *
27  * @param first Range of tasks to be scheduled
28  * @param last
29  * @param n Number of tasks to wait for
30  *
31  * @return vector of pairs (task index, return value of task)
32  */
33 template <class InputIterator>
34 typename std::vector<
35     typename std::enable_if<
36         !std::is_same<
37             typename std::result_of<typename std::iterator_traits<
38                 InputIterator>::value_type()>::type,
39             void>::value,
40         typename std::pair<
41             size_t,
42             typename std::result_of<typename std::iterator_traits<
43                 InputIterator>::value_type()>::type>>::
44         type> inline collectN(InputIterator first, InputIterator last, size_t n);
45
46 /**
47  * collectN specialization for functions returning void
48  *
49  * @param first Range of tasks to be scheduled
50  * @param last
51  * @param n Number of tasks to wait for
52  *
53  * @return vector of completed task indices
54  */
55 template <class InputIterator>
56 typename std::enable_if<
57     std::is_same<
58         typename std::result_of<
59             typename std::iterator_traits<InputIterator>::value_type()>::type,
60         void>::value,
61     std::vector<size_t>>::
62     type inline collectN(InputIterator first, InputIterator last, size_t n);
63
64 /**
65  * Schedules several tasks and blocks until all of these tasks are completed.
66  * If any of the tasks throws an exception, this exception will be re-thrown,
67  * but only when all the tasks are complete. If several tasks throw exceptions
68  * one of them will be re-thrown.
69  *
70  * @param first Range of tasks to be scheduled
71  * @param last
72  *
73  * @return vector of values returned by tasks
74  */
75 template <class InputIterator>
76 typename std::vector<typename std::enable_if<
77     !std::is_same<
78         typename std::result_of<
79             typename std::iterator_traits<InputIterator>::value_type()>::type,
80         void>::value,
81     typename std::result_of<
82         typename std::iterator_traits<InputIterator>::value_type()>::
83         type>::type> inline collectAll(InputIterator first, InputIterator last);
84
85 /**
86  * collectAll specialization for functions returning void
87  *
88  * @param first Range of tasks to be scheduled
89  * @param last
90  */
91 template <class InputIterator>
92 typename std::enable_if<
93     std::is_same<
94         typename std::result_of<
95             typename std::iterator_traits<InputIterator>::value_type()>::type,
96         void>::value,
97     void>::type inline collectAll(InputIterator first, InputIterator last);
98
99 /**
100  * Schedules several tasks and blocks until one of them is completed.
101  * If this task throws an exception, this exception will be re-thrown.
102  * Exceptions thrown by all other tasks will be ignored.
103  *
104  * @param first Range of tasks to be scheduled
105  * @param last
106  *
107  * @return pair of index of the first completed task and its return value
108  */
109 template <class InputIterator>
110 typename std::enable_if<
111     !std::is_same<
112         typename std::result_of<
113             typename std::iterator_traits<InputIterator>::value_type()>::type,
114         void>::value,
115     typename std::pair<
116         size_t,
117         typename std::result_of<typename std::iterator_traits<
118             InputIterator>::value_type()>::type>>::
119     type inline collectAny(InputIterator first, InputIterator last);
120
121 /**
122  * WhenAny specialization for functions returning void.
123  *
124  * @param first Range of tasks to be scheduled
125  * @param last
126  *
127  * @return index of the first completed task
128  */
129 template <class InputIterator>
130 typename std::enable_if<
131     std::is_same<
132         typename std::result_of<
133             typename std::iterator_traits<InputIterator>::value_type()>::type,
134         void>::value,
135     size_t>::type inline collectAny(InputIterator first, InputIterator last);
136 }
137 }
138
139 #include <folly/experimental/fibers/WhenN-inl.h>