1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // tuplePolymorphism.c -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Fri Dec 16 10:25:35 2016 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Fri Dec 21 14:42:48 2016 |
---|
13 | // Update Count : 2 |
---|
14 | // |
---|
15 | |
---|
16 | forall(otype T) T * malloc(); |
---|
17 | |
---|
18 | forall(otype T, ttype Params | sized(T) | { void ?{}(T *, Params); }) |
---|
19 | T * new(Params p) { |
---|
20 | return ((T*)malloc()){ p }; |
---|
21 | } |
---|
22 | |
---|
23 | struct array { |
---|
24 | int * data; |
---|
25 | int size; |
---|
26 | }; |
---|
27 | |
---|
28 | // xxx - eventually this will be collapsed...x |
---|
29 | void ?{}(array * a) { |
---|
30 | a->size = 0; |
---|
31 | a->data = 0; |
---|
32 | printf("called ?{} with no a\n"); |
---|
33 | } |
---|
34 | |
---|
35 | void ?{}(array * a, int a0) { |
---|
36 | a->size = 1; |
---|
37 | a->data = (int*)malloc(sizeof(int)*a->size); |
---|
38 | a->data[0] = a0; |
---|
39 | printf("called ?{} with a: %d\n", a0); |
---|
40 | } |
---|
41 | |
---|
42 | void ?{}(array * a, int a0, int a1) { |
---|
43 | a->size = 2; |
---|
44 | a->data = (int*)malloc(sizeof(int)*a->size); |
---|
45 | a->data[0] = a0; |
---|
46 | a->data[1] = a1; |
---|
47 | printf("called ?{} with a: %d %d\n", a0, a1); |
---|
48 | } |
---|
49 | |
---|
50 | void ?{}(array * a, int a0, int a1, int a2) { |
---|
51 | a->size = 3; |
---|
52 | a->data = (int*)malloc(sizeof(int)*a->size); |
---|
53 | a->data[0] = a0; |
---|
54 | a->data[1] = a1; |
---|
55 | a->data[2] = a2; |
---|
56 | printf("called ?{} with a: %d %d %d\n", a0, a1, a2); |
---|
57 | } |
---|
58 | |
---|
59 | void ?{}(array * a, int a0, int a1, int a2, int a3) { |
---|
60 | a->size = 4; |
---|
61 | a->data = (int*)malloc(sizeof(int)*a->size); |
---|
62 | a->data[0] = a0; |
---|
63 | a->data[1] = a1; |
---|
64 | a->data[2] = a2; |
---|
65 | a->data[3] = a3; |
---|
66 | printf("called ?{} with a: %d %d %d %d\n", a0, a1, a2, a3); |
---|
67 | } |
---|
68 | |
---|
69 | void print(array * x) { |
---|
70 | printf("array = { "); |
---|
71 | for (int i = 0; i < x->size; ++i) { |
---|
72 | printf("%d, ", x->data[i]); |
---|
73 | } |
---|
74 | printf("}\n"); |
---|
75 | } |
---|
76 | |
---|
77 | int main() { |
---|
78 | // xxx - these two don't work just yet |
---|
79 | // array * x0 = new(); |
---|
80 | // print(x0); |
---|
81 | |
---|
82 | // array * x1 = new(999); |
---|
83 | // print(x1); |
---|
84 | |
---|
85 | array * x2 = new(123, 456); |
---|
86 | print(x2); |
---|
87 | |
---|
88 | array * x3 = new(100, 200, 300); |
---|
89 | print(x3); |
---|
90 | |
---|
91 | array * x4 = new(10, 2, 3, 4); |
---|
92 | print(x4); |
---|
93 | } |
---|
94 | |
---|
95 | // Local Variables: // |
---|
96 | // tab-width: 4 // |
---|
97 | // End: // |
---|
98 | |
---|