when* -> collect*
[folly.git] / folly / experimental / fibers / WhenN.h
1 /*
2  * Copyright 2015 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 { namespace fibers {
19
20 /**
21  * Schedules several tasks and blocks until n of these tasks are completed.
22  * If any of these n tasks throws an exception, this exception will be
23  * re-thrown, but only when n tasks are complete. If several tasks throw
24  * exceptions one of them will be re-thrown.
25  *
26  * @param first Range of tasks to be scheduled
27  * @param last
28  * @param n Number of tasks to wait for
29  *
30  * @return vector of pairs (task index, return value of task)
31  */
32 template <class InputIterator>
33 typename std::vector<
34   typename std::enable_if<
35     !std::is_same<
36       typename std::result_of<
37         typename std::iterator_traits<InputIterator>::value_type()>::type,
38       void>::value,
39     typename std::pair<
40       size_t,
41       typename std::result_of<
42         typename std::iterator_traits<InputIterator>::value_type()>::type>
43     >::type
44   >
45 inline collectN(InputIterator first, InputIterator last, size_t n);
46
47 /**
48  * collectN specialization for functions returning void
49  *
50  * @param first Range of tasks to be scheduled
51  * @param last
52  * @param n Number of tasks to wait for
53  *
54  * @return vector of completed task indices
55  */
56 template <class InputIterator>
57 typename std::enable_if<
58   std::is_same<
59     typename std::result_of<
60       typename std::iterator_traits<InputIterator>::value_type()>::type, void
61     >::value, std::vector<size_t>>::type
62 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<
77   typename std::enable_if<
78     !std::is_same<
79       typename std::result_of<
80         typename std::iterator_traits<InputIterator>::value_type()>::type,
81       void>::value,
82     typename std::result_of<
83       typename std::iterator_traits<InputIterator>::value_type()>::type>::type
84   >
85 inline collectAll(InputIterator first, InputIterator last);
86
87 /**
88  * collectAll specialization for functions returning void
89  *
90  * @param first Range of tasks to be scheduled
91  * @param last
92  */
93 template <class InputIterator>
94 typename std::enable_if<
95   std::is_same<
96     typename std::result_of<
97       typename std::iterator_traits<InputIterator>::value_type()>::type, void
98     >::value, void>::type
99 inline collectAll(InputIterator first, InputIterator last);
100
101 /**
102  * Schedules several tasks and blocks until one of them is completed.
103  * If this task throws an exception, this exception will be re-thrown.
104  * Exceptions thrown by all other tasks will be ignored.
105  *
106  * @param first Range of tasks to be scheduled
107  * @param last
108  *
109  * @return pair of index of the first completed task and its return value
110  */
111 template <class InputIterator>
112 typename std::enable_if<
113   !std::is_same<
114     typename std::result_of<
115       typename std::iterator_traits<InputIterator>::value_type()>::type, void
116     >::value,
117   typename std::pair<
118     size_t,
119     typename std::result_of<
120       typename std::iterator_traits<InputIterator>::value_type()>::type>
121   >::type
122 inline collectAny(InputIterator first, InputIterator last);
123
124 /**
125  * WhenAny specialization for functions returning void.
126  *
127  * @param first Range of tasks to be scheduled
128  * @param last
129  *
130  * @return index of the first completed task
131  */
132 template <class InputIterator>
133 typename std::enable_if<
134   std::is_same<
135     typename std::result_of<
136       typename std::iterator_traits<InputIterator>::value_type()>::type, void
137     >::value, size_t>::type
138 inline collectAny(InputIterator first, InputIterator last);
139
140 }}
141
142 #include <folly/experimental/fibers/WhenN-inl.h>