-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlatten.h
More file actions
65 lines (55 loc) · 1.68 KB
/
Flatten.h
File metadata and controls
65 lines (55 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef FLATTEN
#define FLATTEN
#include <thread>
#include "Promise.h"
#include "Future.h"
#include <memory>
#include <iostream>
template<typename T>
struct nested_type_promise;
template<typename T>
struct nested_type_promise<Future<T>> {
typedef T t_t;
};
template<typename T>
struct nested_type_promise<Future<Future<T>>> {
typedef typename nested_type_promise<Future<T>>::t_t t_t;
};
template<typename T>
auto flatten(const Future<T> &fut) {
std::shared_ptr<Promise<T>> p(new Promise<T>());
std::thread([&fut, p]() {
p->set(std::move(fut.get()));
}).detach();
return std::move(p->getFuture());
}
template<typename T>
auto flatten_impl(const Future<Future<T>> &fut) {
return std::move(flatten_impl(std::move(fut.get())));
}
template<typename T>
auto flatten(const Future<Future<T>> &fut) {
std::shared_ptr<Promise<typename nested_type_promise<Future<T>>::t_t>> p(
new Promise<typename nested_type_promise<Future<T>>::t_t>());
std::thread([&fut, p]() {
p->set(std::move(flatten_impl(fut)));
}).detach();
return std::move(p->getFuture());
}
template<typename T>
auto flatten_impl(const Future<T> &fut) {
return (std::move(fut.get()));
}
template<template<typename, typename...> typename C, typename T>
Future<C<T>> flatten(C<Future<T>> const &fut_col) {
std::shared_ptr<Promise<C<T>>> p(new Promise<C<T>>());
std::thread([&fut_col, p]() {
C<T> out = C<T>();
for (const Future<T> &fut : fut_col) {
out.insert(std::end(out), fut.get());
}
p->set(out);
}).detach();
return std::move(p->getFuture());
}
#endif //FLATTEN