1 | #include <fstream.hfa>
|
---|
2 | #include <stdlib.hfa>
|
---|
3 | #include <array.hfa>
|
---|
4 |
|
---|
5 |
|
---|
6 |
|
---|
7 |
|
---|
8 |
|
---|
9 |
|
---|
10 |
|
---|
11 | forall( T, [Nclients], [Ncosts] )
|
---|
12 | struct request {
|
---|
13 | unsigned int requestor_id;
|
---|
14 | array( T, Nclients ) impacted_client_ids; // nested VLA
|
---|
15 | array( float, Ncosts ) cost_contribs; // nested VLA
|
---|
16 | float total_cost;
|
---|
17 | };
|
---|
18 |
|
---|
19 |
|
---|
20 | // TODO: understand (fix?) why these are needed (autogen seems to be failing ... is typeof as struct member nayok?)
|
---|
21 |
|
---|
22 | forall( T, [Nclients], [Ncosts] )
|
---|
23 | void ?{}( T &, request( T, Nclients, Ncosts ) & this ) {}
|
---|
24 |
|
---|
25 | forall( T &, [Nclients], [Ncosts] )
|
---|
26 | void ^?{}( request( T, Nclients, Ncosts ) & this ) {}
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 | forall( T, [Nclients], [Ncosts] )
|
---|
42 | void summarize( request( T, Nclients, Ncosts ) & r ) {
|
---|
43 | r.total_cost = 0;
|
---|
44 | for( i; Ncosts )
|
---|
45 | r.total_cost += r.cost_contribs[i];
|
---|
46 | // say the cost is per-client, to make output vary
|
---|
47 | r.total_cost *= Nclients;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 | int main( int argc, char * argv[] ) {
|
---|
62 | const int ncl = ato( argv[1] );
|
---|
63 | const int nco = 2;
|
---|
64 |
|
---|
65 | request( int, ncl, nco ) r;
|
---|
66 | r.cost_contribs[0] = 100;
|
---|
67 | r.cost_contribs[1] = 0.1;
|
---|
68 |
|
---|
69 | summarize(r);
|
---|
70 | sout | "Total cost:" | r.total_cost;
|
---|
71 | }
|
---|
72 | /*
|
---|
73 | $\$$ ./a.out 5
|
---|
74 | Total cost: 500.5
|
---|
75 | $\$$ ./a.out 6
|
---|
76 | Total cost: 600.6
|
---|
77 | */
|
---|