-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdispatch.cpp
262 lines (228 loc) · 6.09 KB
/
dispatch.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//
// Copyright (c) 2005, 2006 by CodeSourcery
// Copyright (c) 2013 Stefan Seefeld
// All rights reserved.
//
// This file is part of OpenVSIP. It is made available under the
// license contained in the accompanying LICENSE.GPL file.
#include <ovxx/dispatch.hpp>
#include <ovxx/dispatcher/diagnostics.hpp>
#include "test.hpp"
template <typename T> void test_dispatch();
// backends
struct A;
struct B;
struct C;
struct D;
///////////////////////// O1 tests /////////////////////////////////////
struct O1;
namespace ovxx
{
namespace dispatcher
{
template <>
struct List<O1>
{
typedef make_type_list<A, B, C, D>::type type;
};
template <>
struct Signature<O1>
{
typedef void(type)(int &, int);
};
template <>
struct Evaluator<O1, A>
{
static bool const ct_valid = true;
static char const *name() { return "A Evaluator";}
static bool rt_valid(int &, int) { return false;}
static void exec(int &o, int i) { o = i + 'A';}
};
template <>
struct Evaluator<O1, B>
{
static bool const ct_valid = true;
static char const *name() { return "B Evaluator";}
static bool rt_valid(int &, int) { return true;}
static void exec(int &o, int i) { o = i + 'B';}
};
} // namespace ovxx::dispatcher
} // namespace ovxx
template <>
void
test_dispatch<O1>()
{
// Test: Evaluator<O1, A> disqualifies at runtime, so 'B' should win.
int value = 0;
ovxx::dispatch<O1, void, int &, int>(value, 5);
test_assert(value == 5 + 'B');
}
///////////////////////// O2 tests /////////////////////////////////////
struct O2;
namespace ovxx
{
namespace dispatcher
{
template <>
struct List<O2>
{
typedef make_type_list<A, B, C, D>::type type;
};
template <>
struct Signature<O2>
{
typedef int(type)(int);
};
// The A backend gives correct results for even numbers.
template <>
struct Evaluator<O2, A>
{
static bool const ct_valid = true;
static bool rt_valid(int i) { return !(i % 2);}
static int exec(int i) { return 2 * (i / 2);}
};
// The B backend gives correct results for odd numbers.
template <>
struct Evaluator<O2, B>
{
static bool const ct_valid = true;
static bool rt_valid(int i) { return i % 2;}
static int exec(int i) { return 2 * (i / 2) + 1;}
};
} // namespace ovxx::dispatcher
} // namespace ovxx
template <>
void
test_dispatch<O2>()
{
// Test: Evaluator<O2, A> is valid for even input, Evaluator<O2, B> for odd.
test_assert((ovxx::dispatch<O2, int>(4) == 4));
test_assert((ovxx::dispatch<O2, int>(5) == 5));
}
///////////////////////// O3 tests /////////////////////////////////////
// Test compile-time dispatch
struct O3;
namespace ovxx
{
namespace dispatcher
{
template <>
struct List<O3>
{
typedef make_type_list<A, B, C, D>::type type;
};
template <>
struct Evaluator<O3, B>
{
static bool const ct_valid = true;
// Make the tag its own type, for this test.
typedef B backend_type;
};
template <>
struct Evaluator<O3, C, float>
{
static bool const ct_valid = true;
// Make the tag its own type, for this test.
typedef C backend_type;
};
template <>
struct Evaluator<O3, D, double>
{
static bool const ct_valid = true;
// Make the tag its own type, for this test.
typedef D backend_type;
};
} // namespace ovxx::dispatcher
} // namespace ovxx
using namespace vsip;
// Provide an alternative way to map type T to backend B,
// for testing.
template <typename B, typename T>
struct backend_available
{
static bool const value = false;
};
template <>
struct backend_available<C, float>
{
static bool const value = true;
};
template <>
struct backend_available<D, double>
{
static bool const value = true;
};
template <>
void
test_dispatch<O3>()
{
namespace dispatcher = ovxx::dispatcher;
// Test: Evaluator<O3, float> is ct-valid, while Evaluator<O3, double> is not.
test_assert((backend_available<dispatcher::Dispatcher<O3, float>::backend,
float>::value));
test_assert((!backend_available<dispatcher::Dispatcher<O3, float>::backend,
double>::value));
test_assert((!backend_available<dispatcher::Dispatcher<O3, double>::backend,
float>::value));
test_assert((backend_available<dispatcher::Dispatcher<O3, double>::backend,
double>::value));
test_assert((dispatcher::is_operation_supported<O3, float>::value == true));
test_assert((dispatcher::is_operation_supported<O3, int>::value == false));
}
///////////////////////// O4 tests /////////////////////////////////////
// Same as O3, but with additional template parameters.
template <typename A, typename B> struct O4;
namespace ovxx
{
namespace dispatcher
{
template <typename S, typename T>
struct List<O4<S, T> >
{
typedef make_type_list<A, B, C, D>::type type;
};
template <typename S, typename T>
struct Evaluator<O4<S, T>, B>
{
static bool const ct_valid = false;
// Make the tag its own type, for this test.
typedef B backend_type;
};
template <typename T>
struct Evaluator<O4<T, float>, C>
{
static bool const ct_valid = true;
// Make the tag its own type, for this test.
typedef C backend_type;
};
template <typename T>
struct Evaluator<O4<T, double>, D>
{
static bool const ct_valid = true;
// Make the tag its own type, for this test.
typedef D backend_type;
};
} // namespace ovxx::dispatcher
} // namespace ovxx
template <template <typename, typename> class O>
void
test_dispatch()
{
namespace dispatcher = ovxx::dispatcher;
// Test: Evaluator<O4<T, float> > is ct-valid, while Evaluator<O4<T, double> > is not.
test_assert((backend_available<typename dispatcher::Dispatcher<O<short, float> >::backend,
float>::value));
test_assert((!backend_available<typename dispatcher::Dispatcher<O<char, float> >::backend,
double>::value));
test_assert((!backend_available<typename dispatcher::Dispatcher<O<int, double> >::backend,
float>::value));
test_assert((backend_available<typename dispatcher::Dispatcher<O<float, double> >::backend,
double>::value));
}
int main(int, char **)
{
test_dispatch<O1>();
test_dispatch<O2>();
test_dispatch<O3>();
test_dispatch<O4>();
}