-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJTypes.hpp
More file actions
362 lines (312 loc) · 11.2 KB
/
JTypes.hpp
File metadata and controls
362 lines (312 loc) · 11.2 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#ifndef JTYPES_HPP
#define JTYPES_HPP
#include "JGrammar.hpp"
#include "JExceptions.hpp"
#include "JNoun.hpp"
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/optional.hpp>
#include <vector>
#include <utility>
namespace J {
using std::multimap;
using std::vector;
using boost::shared_ptr;
using boost::optional;
using std::pair;
template <template <typename> class Op, typename Ret>
struct JTypeDispatcher {
Ret operator()(j_value_type t) const {
switch (t) {
case j_value_type_int:
return Op<JInt>()();
case j_value_type_float:
return Op<JFloat>()();
case j_value_type_box:
return Op<JBox>()();
case j_value_type_complex:
return Op<JComplex>()();
default:
throw JIllegalValueTypeException();
}
}
template <typename Arg1>
Ret operator()(j_value_type t, const Arg1& arg) const {
switch (t) {
case j_value_type_int:
return Op<JInt>()(arg);
case j_value_type_float:
return Op<JFloat>()(arg);
case j_value_type_box:
return Op<JBox>()(arg);
case j_value_type_complex:
return Op<JComplex>()(arg);
default:
throw JIllegalValueTypeException();
}
}
template <typename Arg1, typename Arg2>
Ret operator()(j_value_type t, const Arg1& arg1, const Arg2& arg2) const {
switch (t) {
case j_value_type_int:
return Op<JInt>()(arg1, arg2);
case j_value_type_float:
return Op<JFloat>()(arg1, arg2);
case j_value_type_box:
return Op<JBox>()(arg1, arg2);
case j_value_type_complex:
return Op<JComplex>()(arg1, arg2);
default:
throw JIllegalValueTypeException();
}
}
template <typename Arg1, typename Arg2, typename Arg3>
Ret operator()(j_value_type t, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) const {
switch (t) {
case j_value_type_int:
return Op<JInt>()(arg1, arg2, arg3);
case j_value_type_float:
return Op<JFloat>()(arg1, arg2, arg3);
case j_value_type_box:
return Op<JBox>()(arg1, arg2, arg3);
case j_value_type_complex:
return Op<JComplex>()(arg1, arg2, arg3);
default:
throw JIllegalValueTypeException();
}
}
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
Ret operator()(j_value_type t, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4) const {
switch (t) {
case j_value_type_int:
return Op<JInt>()(arg1, arg2, arg3, arg4);
case j_value_type_float:
return Op<JFloat>()(arg1, arg2, arg3, arg4);
case j_value_type_box:
return Op<JBox>()(arg1, arg2, arg3, arg4);
case j_value_type_complex:
return Op<JComplex>()(arg1, arg2, arg3, arg4);
default:
throw JIllegalValueTypeException();
}
}
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
Ret operator()(j_value_type t, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4,
const Arg5& arg5) const {
switch (t) {
case j_value_type_int:
return Op<JInt>()(arg1, arg2, arg3, arg4, arg5);
case j_value_type_float:
return Op<JFloat>()(arg1, arg2, arg3, arg4, arg5);
case j_value_type_box:
return Op<JBox>()(arg1, arg2, arg3, arg4, arg5);
case j_value_type_complex:
return Op<JComplex>()(arg1, arg2, arg3, arg4, arg5);
default:
throw JIllegalValueTypeException();
}
}
};
template <template <typename> class Op, typename Ret>
struct JArrayCaller {
template <typename T>
struct Converter {
Ret operator()(const JNoun& noun) const {
return Op<T>()(static_cast<const JArray<T>&>(noun));
}
template <typename Arg1>
Ret operator()(const JNoun& noun, Arg1& arg1) const {
return Op<T>()(static_cast<const JArray<T>&>(noun), arg1);
}
template <typename Arg1, typename Arg2>
Ret operator()(const JNoun& noun, Arg1& arg1, Arg2& arg2) const {
return Op<T>()(static_cast<const JArray<T>&>(noun), arg1, arg2);
}
template <typename Arg1, typename Arg2, typename Arg3>
Ret operator()(const JNoun& noun, Arg1& arg1, Arg2& arg2, Arg3& arg3) const {
return Op<T>()(static_cast<const JArray<T>&>(noun), arg1, arg2, arg3);
}
};
Ret operator()(const JNoun& noun) const {
return JTypeDispatcher<Converter, Ret>()(noun.get_value_type(), noun);
};
template <typename Arg1>
Ret operator()(const JNoun& noun, Arg1& arg1) const {
return JTypeDispatcher<Converter, Ret>()(noun.get_value_type(), noun, arg1);
}
template <typename Arg1, typename Arg2>
Ret operator()(const JNoun& noun, Arg1& arg1, Arg2& arg2) const {
return JTypeDispatcher<Converter, Ret>()(noun.get_value_type(), noun, arg1, arg2);
}
template <typename Arg1, typename Arg2, typename Arg3>
Ret operator()(const JNoun& noun, Arg1& arg1, Arg2& arg2, Arg3& arg3) const {
return JTypeDispatcher<Converter, Ret>()(noun.get_value_type(), noun, arg1, arg2, arg3);
}
};
template <typename From, typename To>
struct ConvertType {
To operator()(From) const {
throw JIllegalTypeCastException();
}
};
template <typename Arg>
struct ConvertType<Arg, Arg> {
Arg operator()(Arg arg) const {
return arg;
}
};
template <>
struct ConvertType<JInt, JComplex> {
JComplex operator()(JInt arg) const {
return JComplex(static_cast<JFloat>(arg));
}
};
template <>
struct ConvertType<JFloat, JComplex> {
JComplex operator()(JFloat arg) const {
return JComplex(arg);
}
};
template <>
struct ConvertType<JInt, JFloat> {
JFloat operator()(JInt from) {
return static_cast<JFloat>(from);
}
};
template <typename From>
struct CurriedConvertType {
template <typename To>
struct Impl {
To operator()(From from) const {
return ConvertType<From, To>()(from);
}
};
};
template <typename From, typename To>
struct ConvertJArray {
shared_ptr<JArray<To> > operator()(const JArray<From>& from) const {
shared_ptr<vector<To> > to(new vector<To>(from.get_dims().number_of_elems(),
JTypeTrait<To>::base_elem()));
transform(from.begin(), from.end(), to->begin(), ConvertType<From, To>());
return shared_ptr<JArray<To> >(new JArray<To>(from.get_dims(), to));
}
};
template <typename Arg>
struct ConvertJArray<Arg, Arg> {
shared_ptr<JArray<Arg> > operator()(const JArray<Arg>& arr) const {
return boost::static_pointer_cast<JArray<Arg> >(arr.clone());
}
};
class TypeConversions {
typedef multimap<j_value_type, j_value_type> our_map;
our_map type_conversions;
TypeConversions();
public:
typedef shared_ptr<TypeConversions> Ptr;
static Ptr get_instance();
bool is_convertible_to(j_value_type from, j_value_type to) const;
optional<j_value_type> find_best_type_conversion(j_value_type t1, j_value_type t2) const;
};
template <typename From>
struct ConversionOpPerformer {
template <typename To>
struct Impl {
JNoun::Ptr operator()(const JArray<From>& from) const {
return boost::static_pointer_cast<JNoun>(ConvertJArray<From, To>()(from));
}
};
};
template <typename T>
struct ConversionOp {
JNoun::Ptr operator()(const JArray<T>& from, j_value_type to_type) const {
return JTypeDispatcher<ConversionOpPerformer<T>::template Impl, JNoun::Ptr>()(to_type, from);
}
};
struct GetNounAsJArrayOfType {
JNoun::Ptr operator()(const JNoun& arg, j_value_type to_type) const;
};
template <template <typename> class Op, typename Res>
struct DualArrayConverter {
template <typename T>
struct Impl {
Res operator()(const JNoun& larg, const JNoun& rarg) const {
return Op<T>()(static_cast<const JArray<T>&>(larg),
static_cast<const JArray<T>&>(rarg));
}
template <typename Arg1>
Res operator()(const JNoun& larg, const JNoun& rarg, const Arg1& arg1) const {
return Op<T>()(static_cast<const JArray<T>&>(larg),
static_cast<const JArray<T>&>(rarg),
arg1);
}
template <typename Arg1, typename Arg2>
Res operator()(const JNoun& larg, const JNoun& rarg, const Arg1& arg1, const Arg2& arg2) const {
return Op<T>()(static_cast<const JArray<T>&>(larg),
static_cast<const JArray<T>&>(rarg),
arg1, arg2);
}
template <typename Arg1, typename Arg2, typename Arg3>
Res operator()(const JNoun& larg, const JNoun& rarg,
const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) const {
return Op<T>()(static_cast<const JArray<T>&>(larg),
static_cast<const JArray<T>&>(rarg),
arg1, arg2, arg3);
}
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
Res operator()(const JNoun& larg, const JNoun& rarg,
const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, Arg4& arg4) const {
return Op<T>()(static_cast<const JArray<T>&>(larg), static_cast<const JArray<T>&>(rarg),
arg1, arg2, arg3, arg4);
}
};
};
template <template <typename> class Op, typename Res>
struct CallWithCommonType {
typedef pair<JNoun::Ptr, JNoun::Ptr> JNounCouple;
JNounCouple do_conversion(const JNoun& larg, const JNoun& rarg) const {
optional<j_value_type> type(TypeConversions::get_instance()
->find_best_type_conversion(larg.get_value_type(),
rarg.get_value_type()));
if (!type) throw JIllegalValueTypeException();
JNoun::Ptr larg_right_type(GetNounAsJArrayOfType()(larg, *type));
JNoun::Ptr rarg_right_type(GetNounAsJArrayOfType()(rarg, *type));
return JNounCouple(larg_right_type, rarg_right_type);
}
Res operator()(const JNoun& larg, const JNoun& rarg) const {
JNounCouple convertee(do_conversion(larg, rarg));
return JTypeDispatcher<DualArrayConverter<Op, Res>::template Impl, Res>()(convertee.first->get_value_type(),
*convertee.first, *convertee.second);
}
template <typename Arg1>
Res operator()(const JNoun& larg, const JNoun& rarg, const Arg1& arg1) {
JNounCouple convertee(do_conversion(larg, rarg));
return JTypeDispatcher<DualArrayConverter<Op, Res>::template Impl, Res>()(convertee.first->get_value_type(),
*convertee.first, *convertee.second, arg1);
}
template <typename Arg1, typename Arg2>
Res operator()(const JNoun& larg, const JNoun& rarg, const Arg1& arg1, const Arg2& arg2) {
JNounCouple convertee(do_conversion(larg, rarg));
return JTypeDispatcher<DualArrayConverter<Op, Res>::template Impl, Res>()(convertee.first->get_value_type(),
*convertee.first, *convertee.second, arg1, arg2);
}
template <typename Arg1, typename Arg2, typename Arg3>
Res operator()(const JNoun& larg, const JNoun& rarg, const Arg1& arg1,
const Arg2& arg2, const Arg3& arg3) {
JNounCouple convertee(do_conversion(larg, rarg));
return JTypeDispatcher<DualArrayConverter<Op, Res>::template Impl, Res>()
(convertee.first->get_value_type(),
*convertee.first, *convertee.second, arg1, arg2, arg3);
}
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
Res operator()(const JNoun& larg, const JNoun& rarg, const Arg1& arg1,
const Arg2& arg2, const Arg3& arg3, const Arg4& arg4) {
JNounCouple convertee(do_conversion(larg, rarg));
return JTypeDispatcher<DualArrayConverter<Op, Res>::template Impl, Res>()
(convertee.first->get_value_type(),
*convertee.first, *convertee.second, arg1, arg2, arg3, arg4);
}
};
template <typename T>
JArray<T> require_type(const JNoun& noun);
}
#endif