Debian Codegraph @

pypy/module/_cppyy/test/templates.h

raw symbols SCIP
1 #ifndef CPPYY_TEST_TEMPLATES_H
2 #define CPPYY_TEST_TEMPLATES_H
3
4 #include <stdexcept>
5 #include <string>
6 #include <sstream>
7 #include <vector>
8
9 #if defined(_MSC_VER)
10 #define INLINE __inline
11 #elif defined(__GNUC__)
12 #if defined(__STRICT_ANSI__)
13 #define INLINE __inline__
14 #else
15 #define INLINE inline
16 #endif
17 #else
18 #define INLINE
19 #endif
20
21 #ifndef __MSC_VER
22 #include <cxxabi.h>
23 INLINE std::string demangle_it(const char* name, const char* errmsg) {
24 int status;
25 std::string res = abi::__cxa_demangle(name, 0, 0, &status);
26 if (status != 0) throw std::runtime_error(errmsg);
27 return res;
28 }
29 #else
30 INLINE std::string demangle_it(const char* name, const char*) {
31 return name; // typeinfo's name() is already demangled
32 }
33 #endif
34
35
36 //===========================================================================
37 class MyTemplatedMethodClass { // template methods
38 public:
39 template<class A> long get_size(A&);
40 template<class A> long get_size(const A&);
41
42 long get_size();
43 template<class B> long get_size();
44
45 long get_char_size();
46 long get_int_size();
47 long get_long_size();
48 long get_float_size();
49 long get_double_size();
50
51 long get_self_size();
52
53 private:
54 double m_data[3];
55 };
56
57 template<class A>
58 long MyTemplatedMethodClass::get_size(A&) {
59 return sizeof(A);
60 }
61
62 template<class A>
63 long MyTemplatedMethodClass::get_size(const A&) {
64 return sizeof(A)+1;
65 }
66
67 template<class B>
68 INLINE long MyTemplatedMethodClass::get_size() {
69 return sizeof(B);
70 }
71
72 //
73 typedef MyTemplatedMethodClass MyTMCTypedef_t;
74
75 // explicit instantiation
76 template long MyTemplatedMethodClass::get_size<char>();
77 template long MyTemplatedMethodClass::get_size<int>();
78
79 // "lying" specialization
80 template<>
81 INLINE long MyTemplatedMethodClass::get_size<long>() {
82 return 42;
83 }
84
85
86 //===========================================================================
87 // global templated functions
88 template<typename T>
89 long global_get_size() {
90 return sizeof(T);
91 }
92
93 template <typename T>
94 int global_some_foo(T) {
95 return 42;
96 }
97
98 template <typename T>
99 int global_some_bar(T) {
100 return 13;
101 }
102
103 template <typename F>
104 struct SomeResult {
105 F m_retval;
106 };
107
108 template <class I, typename O = float>
109 SomeResult<O> global_get_some_result(const I& carrier) {
110 SomeResult<O> r{};
111 r.m_retval = O(carrier[0]);
112 return r;
113 }
114
115
116 //===========================================================================
117 // variadic functions
118 INLINE bool isSomeInt(int) { return true; }
119 INLINE bool isSomeInt(double) { return false; }
120 template <typename ...Args>
121 INLINE bool isSomeInt(Args...) { return false; }
122
123 namespace AttrTesting {
124
125 struct Obj1 { int var1; };
126 struct Obj2 { int var2; };
127
128 template <typename T>
129 constexpr auto has_var1(T t) -> decltype(t.var1, true) { return true; }
130
131 template <typename ...Args>
132 constexpr bool has_var1(Args...) { return false; }
133
134 template <typename T>
135 constexpr bool call_has_var1(T&& t) { return AttrTesting::has_var1(std::forward<T>(t)); }
136
137 template <int N, typename... T>
138 struct select_template_arg {};
139
140 template <typename T0, typename... T>
141 struct select_template_arg<0, T0, T...> {
142 typedef T0 type;
143 };
144
145 template <int N, typename T0, typename... T>
146 struct select_template_arg<N, T0, T...> {
147 typedef typename select_template_arg<N-1, T...>::type argument;
148 };
149
150 } // AttrTesting
151
152
153 namespace SomeNS {
154
155 template <typename T>
156 int some_foo(T) {
157 return 42;
158 }
159
160 template <int T>
161 int some_bar() {
162 return T;
163 }
164
165 INLINE std::string tuplify(std::ostringstream& out) {
166 out << "NULL)";
167 return out.str();
168 }
169
170 template<typename T, typename... Args>
171 std::string tuplify(std::ostringstream& out, T value, Args... args)
172 {
173 out << value << ", ";
174 return tuplify(out, args...);
175 }
176
177 } // namespace SomeNS
178
179
180 //===========================================================================
181 // using of static data
182 // TODO: this should live here instead of in test_templates.test08
183 /*
184 template <typename T> struct BaseClassWithStatic {
185 static T const ref_value;
186 };
187
188 template <typename T>
189 T const BaseClassWithStatic<T>::ref_value = 42;
190
191 template <typename T>
192 struct DerivedClassUsingStatic : public BaseClassWithStatic<T> {
193 using BaseClassWithStatic<T>::ref_value;
194
195 explicit DerivedClassUsingStatic(T x) : BaseClassWithStatic<T>() {
196 m_value = x > ref_value ? ref_value : x;
197 }
198
199 T m_value;
200 };
201 */
202
203
204 //===========================================================================
205 // templated callable
206 class TemplatedCallable {
207 public:
208 template <class I , class O = double>
209 O operator() (const I& in) const { return O(in); }
210 };
211
212
213 //===========================================================================
214 // templated typedefs
215 namespace TemplatedTypedefs {
216
217 template<typename TYPE_IN, typename TYPE_OUT, size_t _vsize = 4>
218 struct BaseWithEnumAndTypedefs {
219 enum { vsize = _vsize };
220 typedef TYPE_IN in_type;
221 typedef TYPE_OUT out_type;
222 };
223
224 template <typename TYPE_IN, typename TYPE_OUT, size_t _vsize = 4>
225 struct DerivedWithUsing : public BaseWithEnumAndTypedefs<TYPE_IN, TYPE_OUT, _vsize>
226 {
227 typedef BaseWithEnumAndTypedefs<TYPE_IN, TYPE_OUT, _vsize> base_type;
228 using base_type::vsize;
229 using typename base_type::in_type;
230 typedef typename base_type::in_type in_type_tt;
231 using typename base_type::out_type;
232 };
233
234 struct SomeDummy {};
235
236 } // namespace TemplatedTypedefs
237
238
239 //===========================================================================
240 // hiding templated methods
241 namespace TemplateHiding {
242
243 struct Base {
244 template<class T>
245 int callme(T t = T(1)) { return 2*t; }
246 };
247
248 struct Derived : public Base {
249 int callme(int t = 2) { return t; }
250 };
251
252 } // namespace TemplateHiding
253
254
255 //===========================================================================
256 // 'using' of templates
257 template<typename T> using DA_vector = std::vector<T>;
258
259 #if __cplusplus > 201402L
260 namespace using_problem {
261
262 template <typename T, size_t SZ>
263 struct vector {
264 vector() : m_val(SZ) {}
265 T m_val;
266 };
267
268 template <typename T, size_t ... sizes>
269 struct matryoshka {
270 typedef T type;
271 };
272
273 template <typename T, size_t SZ, size_t ... sizes>
274 struct matryoshka<T, SZ, sizes ... > {
275 typedef vector<typename matryoshka<T, sizes ...>::type, SZ> type;
276 };
277
278 template <typename T, size_t ... sizes>
279 using make_vector = typename matryoshka<T, sizes ...>::type;
280 typedef make_vector<int, 2, 3> iiv_t;
281 };
282 #endif
283
284 namespace using_problem {
285
286 template<typename T>
287 class Base {
288 public:
289 template<typename R>
290 R get1(T t) { return t + R{5}; }
291 T get2() { return T{5}; }
292 template<typename R>
293 R get3(T t) { return t + R{5}; }
294 T get3() { return T{5}; }
295 };
296
297 template<typename T>
298 class Derived : public Base<T> {
299 public:
300 typedef Base<T> _Mybase;
301 using _Mybase::get1;
302 using _Mybase::get2;
303 using _Mybase::get3;
304 };
305
306 } // namespace using_problem
307
308
309 //===========================================================================
310 // template with r-value
311 namespace T_WithRValue {
312
313 template<typename T>
314 bool is_valid(T&& new_value) {
315 return new_value != T{};
316 }
317
318 } // namespace T_WithRValue
319
320
321 //===========================================================================
322 // variadic templates
323 namespace some_variadic {
324
325 #ifdef _WIN32
326 #ifdef __CLING__
327 extern __declspec(dllimport) std::string gTypeName;
328 #else
329 extern __declspec(dllexport) std::string gTypeName;
330 #endif
331 #else
332 extern std::string gTypeName;
333 #endif
334
335 template <typename ... Args>
336 class A {
337 public:
338 A() {
339 gTypeName = demangle_it(typeid(A<Args...>).name(), "A::A");
340 }
341 A(const A&) = default;
342 A(A&&) = default;
343 A& operator=(const A&) = default;
344 A& operator=(A&&) = default;
345
346 template <typename ... FArgs>
347 void a(FArgs&&... args) {
348 gTypeName = demangle_it(typeid(&A<Args...>::a<FArgs...>).name(), "A::a-2");
349 }
350
351 template <typename T, typename ... FArgs>
352 T a_T(FArgs&&... args) {
353 gTypeName = demangle_it(typeid(&A<Args...>::a_T<T, FArgs...>).name(), "A::a_T-2");
354 return T{};
355 }
356
357 template <typename ... FArgs>
358 static void sa(FArgs&&... args) {
359 gTypeName = demangle_it(typeid(A<Args...>).name(), "A::sa-1");
360 gTypeName += "::";
361 gTypeName += demangle_it(typeid(A<Args...>::sa<FArgs...>).name(), "A::sa-2");
362 }
363
364 template <typename T, typename ... FArgs>
365 static T sa_T(FArgs&&... args) {
366 gTypeName = demangle_it(typeid(A<Args...>).name(), "A::sa_T-1");
367 gTypeName += "::";
368 gTypeName += demangle_it(typeid(A<Args...>::sa_T<T, FArgs...>).name(), "A::sa_T-2");
369 return T{};
370 }
371 };
372
373 class B {
374 public:
375 B() {
376 gTypeName = demangle_it(typeid(B).name(), "B::B");
377 }
378 B(const B&) = default;
379 B(B&&) = default;
380 B& operator=(const B&) = default;
381 B& operator=(B&&) = default;
382
383 template <typename ... FArgs>
384 void b(FArgs&&... args) {
385 gTypeName = demangle_it(typeid(&B::b<FArgs...>).name(), "B::b-2");
386 }
387
388 template <typename T, typename ... FArgs>
389 T b_T(FArgs&&... args) {
390 gTypeName = demangle_it(typeid(&B::b_T<T, FArgs...>).name(), "B::b_T-2");
391 return T{};
392 }
393
394 template <typename ... FArgs>
395 static void sb(FArgs&&... args) {
396 gTypeName = demangle_it(typeid(B).name(), "B::sb-1");
397 gTypeName += "::";
398 gTypeName += demangle_it(typeid(B::sb<FArgs...>).name(), "B::sb-2");
399 }
400
401 template <typename T, typename ... FArgs>
402 static T sb_T(FArgs&&... args) {
403 gTypeName = demangle_it(typeid(B).name(), "B::sb_T-1");
404 gTypeName += "::";
405 gTypeName += demangle_it(typeid(B::sb_T<T, FArgs...>).name(), "B::sb_T-2");
406 return T{};
407 }
408 };
409
410 template <typename ... Args>
411 void fn(Args&&... args) {
412 gTypeName = demangle_it(typeid(fn<Args...>).name(), "fn");
413 }
414
415 template <typename T, typename ... Args>
416 T fn_T(Args&&... args) {
417 gTypeName = demangle_it(typeid(fn<Args...>).name(), "fn_T");
418 return T{};
419 }
420
421 } // namespace some_variadic
422
423
424 //===========================================================================
425 // template with empty body
426 namespace T_WithEmptyBody {
427
428 #ifdef _WIN32
429 #ifdef __CLING__
430 extern __declspec(dllimport) std::string side_effect;
431 #else
432 extern __declspec(dllexport) std::string side_effect;
433 #endif
434 #else
435 extern std::string side_effect;
436 #endif
437
438 template<typename T>
439 void some_empty();
440
441 } // namespace T_WithEmptyBody
442
443
444 //===========================================================================
445 // template with catch-all (void*, void**)overloads
446 namespace T_WithGreedyOverloads {
447
448 class SomeClass {
449 double fD;
450 };
451
452 class WithGreedy1 {
453 public:
454 template<class T>
455 int get_size(T*) { return (int)sizeof(T); }
456 int get_size(void*, bool force=false) { return -1; }
457 };
458
459 class WithGreedy2 {
460 public:
461 template<class T>
462 int get_size(T*) { return (int)sizeof(T); }
463 int get_size(void**, bool force=false) { return -1; }
464 };
465
466 class DoesNotExist;
467
468 class WithGreedy3 {
469 public:
470 template<class T>
471 int get_size(T*) { return (int)sizeof(T); }
472 int get_size(DoesNotExist*, bool force=false) { return -1; }
473 };
474
475 } // namespace T_WithGreedyOverloads
476
477
478 //===========================================================================
479 // template with overloaded non-templated and templated setitem
480 namespace TemplateWithSetItem {
481
482 template <typename T>
483 class MyVec {
484 private:
485 std::vector<T> fData;
486
487 public:
488 using size_type = typename std::vector<T>::size_type;
489
490 MyVec(size_type count) : fData(count) {}
491
492 T & operator[](size_type index) { return fData[index]; }
493
494 // The definition of this templated operator causes the issue
495 template <typename V>
496 MyVec operator[](const MyVec<V> &conds) const { return MyVec(2); }
497 };
498
499 } // namespace TemplateWithSetItem
500
501
502 //===========================================================================
503 // type reduction examples on gmpxx-like template expressions
504 namespace TypeReduction {
505
506 template <typename T>
507 struct BinaryExpr;
508
509 template <typename T>
510 struct Expr {
511 Expr() {}
512 Expr(const BinaryExpr<T>&) {}
513 };
514
515 template <typename T>
516 struct BinaryExpr {
517 BinaryExpr(const Expr<T>&, const Expr<T>&) {}
518 };
519
520 template<typename T>
521 BinaryExpr<T> operator+(const Expr<T>& e1, const Expr<T>& e2) {
522 return BinaryExpr<T>(e1, e2);
523 }
524
525 } // namespace TypeReduction
526
527
528 //===========================================================================
529 // type deduction examples
530 namespace FailedTypeDeducer {
531
532 template<class T>
533 class B {
534 public:
535 auto result() { return 5.; }
536 };
537
538 extern template class B<int>;
539
540 }
541
542 #endif // !CPPYY_TEST_TEMPLATES_H