QuantumLibrary
quantum_future_joiner_impl.h
1 /*
2 ** Copyright 2018 Bloomberg Finance L.P.
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 //NOTE: DO NOT INCLUDE DIRECTLY
17 
18 //##############################################################################################
19 //#################################### IMPLEMENTATIONS #########################################
20 //##############################################################################################
21 namespace Bloomberg {
22 namespace quantum {
23 
24 template <typename T>
25 template <class DISPATCHER, class>
26 ThreadFuturePtr<std::vector<T>>
27 FutureJoiner<T>::operator()(DISPATCHER& dispatcher, std::vector<ThreadContextPtr<T>>&& futures)
28 {
29  return join<ThreadContext>(ThreadContextTag{}, dispatcher, std::move(futures));
30 }
31 
32 template <typename T>
33 template <class DISPATCHER, class>
35 FutureJoiner<T>::operator()(DISPATCHER& dispatcher, std::vector<ThreadFuturePtr<T>>&& futures)
36 {
37  return join<ThreadFuture>(ThreadContextTag{}, dispatcher, std::move(futures));
38 }
39 
40 template <typename T>
41 template <class DISPATCHER, class>
43 FutureJoiner<T>::operator()(DISPATCHER& dispatcher, std::vector<CoroContextPtr<T>>&& futures)
44 {
45  return join<CoroContext>(CoroContextTag{}, dispatcher, std::move(futures));
46 }
47 
48 template <typename T>
49 template <class DISPATCHER, class>
51 FutureJoiner<T>::operator()(DISPATCHER& dispatcher, std::vector<CoroFuturePtr<T>>&& futures)
52 {
53  return join<CoroFuture>(CoroContextTag{}, dispatcher, std::move(futures));
54 }
55 
56 template <typename T>
57 template <template<class> class FUTURE, class DISPATCHER>
59 FutureJoiner<T>::join(ThreadContextTag, DISPATCHER& dispatcher, std::vector<typename FUTURE<T>::Ptr>&& futures)
60 {
61 #if (__cplusplus == 201103L)
62  std::shared_ptr<std::vector<typename FUTURE<T>::Ptr>> containerPtr(new std::vector<typename FUTURE<T>::Ptr>(std::move(futures)));
63  return dispatcher.template postAsyncIo<std::vector<T>>([containerPtr](ThreadPromisePtr<std::vector<T>> promise)
64  {
65  std::vector<T> result;
66  result.reserve(containerPtr->size());
67  for (auto&& f : *containerPtr)
68  {
69  result.emplace_back(f->get());
70  }
71  return promise->set(std::move(result));
72  });
73 #else
74  return dispatcher.template postAsyncIo<std::vector<T>>([container{std::move(futures)}](ThreadPromisePtr<std::vector<T>> promise)
75  {
76  std::vector<T> result;
77  result.reserve(container.size());
78  for (auto&& f : container)
79  {
80  result.emplace_back(f->get());
81  }
82  return promise->set(std::move(result));
83  });
84 #endif
85 }
86 
87 template <typename T>
88 template <template<class> class FUTURE, class DISPATCHER>
89 CoroContextPtr<std::vector<T>>
90 FutureJoiner<T>::join(CoroContextTag, DISPATCHER& dispatcher, std::vector<typename FUTURE<T>::Ptr>&& futures)
91 {
92 #if (__cplusplus == 201103L)
93  std::shared_ptr<std::vector<typename FUTURE<T>::Ptr>> containerPtr(new std::vector<typename FUTURE<T>::Ptr>(std::move(futures)));
94  return dispatcher.template post<std::vector<T>>([containerPtr](CoroContextPtr<std::vector<T>> ctx)
95  {
96  std::vector<T> result;
97  result.reserve(containerPtr->size());
98  for (auto&& f : *containerPtr)
99  {
100  result.emplace_back(f->get(ctx));
101  }
102  return ctx->set(std::move(result));
103  });
104 #else
105  return dispatcher.template post<std::vector<T>>([container{std::move(futures)}](CoroContextPtr<std::vector<T>> ctx)
106  {
107  std::vector<T> result;
108  result.reserve(container.size());
109  for (auto&& f : container)
110  {
111  result.emplace_back(f->get(ctx));
112  }
113  return ctx->set(std::move(result));
114  });
115 #endif
116 }
117 
118 }} //namespace
119 
typename IThreadPromise< Promise, T >::Ptr ThreadPromisePtr
Definition: quantum_ithread_promise.h:82
Definition: quantum_buffer_impl.h:22
typename ICoroFuture< T >::Ptr CoroFuturePtr
Definition: quantum_icoro_future.h:72
ThreadFuturePtr< std::vector< T > > operator()(DISPATCHER &dispatcher, std::vector< ThreadContextPtr< T >> &&futures)
Join N thread futures.
Definition: quantum_future_joiner_impl.h:27
typename ICoroContext< RET >::Ptr CoroContextPtr
Definition: quantum_icoro_context.h:479
Utility class that joins N futures into a single one.
Definition: quantum_future_joiner.h:39
Definition: quantum_icontext_base.h:26
typename IThreadFuture< T >::Ptr ThreadFuturePtr
Definition: quantum_ithread_future.h:69
Definition: quantum_icontext_base.h:25
typename IThreadContext< RET >::Ptr ThreadContextPtr
Definition: quantum_ithread_context.h:242