-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tuple.cpp
More file actions
149 lines (109 loc) · 3.64 KB
/
test_tuple.cpp
File metadata and controls
149 lines (109 loc) · 3.64 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <tuple>
#include <iostream>
//tbb related libraries
#include "tbb/tbb.h"
#include <array>
#include <type_traits>
class test{
public:
void operator()(int a, int b, int c, int* out){
out[0] = a +b +c;
}
};
//tuple related things...
template<typename F, typename Tuple, size_t ...S>
decltype(auto)apply_tuple_impl(F&& fn, Tuple&& t, std::index_sequence<S...>)
{
return std::forward<F>(fn)(std::get<S>(std::forward<Tuple>(t))...);
}
template<typename F, typename Tuple>
decltype(auto) apply_from_tuple(F&& fn, Tuple && t){
std::size_t constexpr tSize
= std::tuple_size<typename std::remove_reference<Tuple>::type>::value;
return apply_tuple_impl(std::forward<F>(fn),
std::forward<Tuple>(t),
std::make_index_sequence<tSize>());
}
template<class F, typename... ARGS>
class MyTask{
static_assert(sizeof... (ARGS)>=0,
"At least one argument needed ");
public:
MyTask(tbb::task_arena &arena, ARGS... args);
bool execute();
private:
std::tuple<ARGS...>m_tuple;
tbb::task_arena m_arena;
};
template<class F, typename... ARGS>
std::unique_ptr<MyTask<F,ARGS...>>make_Task(tbb::task_arena &arena, ARGS... args){
return std::make_unique<MyTask<F, ARGS...>>(arena,args...);
}
template<class F, typename... ARGS>
MyTask<F, ARGS...>::MyTask(tbb::task_arena &arena, ARGS... args):m_arena(std::move(arena)),m_tuple(std::make_tuple(args...)){}
template<class F, typename... ARGS>
bool MyTask<F, ARGS...>::execute(){
auto fun = F();
std::tuple temp_tuple(std::move(m_tuple));
m_arena.execute([&fun,&temp_tuple](){
tbb::task_group group;
group.run([&](){
apply_from_tuple(fun,temp_tuple);
});
group.wait();
});
}
//Test whether something is tuple or not....
template<typename T>
struct IsTupleImpl : std::false_type{};
template<typename... U>
struct IsTupleImpl<std::tuple<U...>>: std::true_type{};
template<typename T>
constexpr bool IsTuple(T* x){
return IsTupleImpl<std::decay_t<T>>::value;
}
template<typename T>
constexpr bool IsFundamental(T x){
return std::is_fundamental<T>::value;
}
//now the testing of the unpacking tuples...
template<typename T>
struct is_pointer { static const bool value = false; };
template<typename T>
struct is_pointer<T*> { static const bool value = true; };
//now for the arrays
template<typename T>
struct is_arrayImpl: std::false_type{};
template<typename T>
struct is_arrayImpl<T[]>: std::true_type{};
template<typename T, std::size_t N>
struct is_arrayImpl<T[N]>: std::true_type{};
template<typename T>
constexpr bool is_array(T* x){
std::cout<<"x is "<<x<<std::endl;
return is_arrayImpl<std::decay_t<T>>::value;
}
int main()
{
int a = 1;
int b = 2;
int c = 3;
int* out = new int[100];
int val[100];
bool _true = true;
int arrSize = *(&out + 1) - out;
std::cout<<out[0]<<" Empty array "<<std::endl;
std::cout<<"Size of Out is "<<sizeof(out)/sizeof(out[0])<<" "<<arrSize<<std::endl;
std::cout<<is_array(val)<<'\n'<<is_array(&a)<<std::endl;
std::cout<<"fundamental "<<std::is_fundamental<int>::value<<" "<<IsFundamental(out)<<std::endl;
//we just need one thread to dispatch....
tbb::task_arena arena(1);
auto task = make_Task<test>(arena,a,b,c,out);
task->execute();
std::cout<<"Output After the Execution "<<out[0]<<std::endl;
//test on make_index_sequence
auto x_ = std::make_index_sequence<10>();
auto test_tuple = std::make_tuple(1,2,3,4);
std::cout<<IsTuple(&test_tuple)<<std::endl;
return 1;
}