| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2023 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 | // array-raii-cfa.cfa -- checks RAII on CFA arrays of initialized elements
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Mike Brooks
 | 
|---|
| 10 | // Created On       : Fri Sep 22 15:00:00 2023
 | 
|---|
| 11 | // Last Modified By :
 | 
|---|
| 12 | // Last Modified On :
 | 
|---|
| 13 | // Update Count     :
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | // CFA array means like `array(float, 17) x;`
 | 
|---|
| 17 | 
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include <fstream.hfa>
 | 
|---|
| 20 | #include <collections/array.hfa>
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #define ADECL1(X, El, N)    array(El, N) X;
 | 
|---|
| 23 | #define ADECL2(X, El, M, N) array(El, M, N) X;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "array-raii.hfa"
 | 
|---|
| 26 | 
 | 
|---|
| 27 | void test_uninit_alt() {
 | 
|---|
| 28 |     printf(" [1]\n");
 | 
|---|
| 29 |     {
 | 
|---|
| 30 |         array(thing, 5) a = { delay_init };
 | 
|---|
| 31 |         printf("before ctors\n");
 | 
|---|
| 32 |         for(i; 5) {
 | 
|---|
| 33 |             if (i == 1)
 | 
|---|
| 34 |                 (a[i]){};  // no need for `emplace` bc no-arg ctor call means elem's real ctor
 | 
|---|
| 35 |             else if (i == 2)
 | 
|---|
| 36 |                 (a[i]){888};
 | 
|---|
| 37 |             else
 | 
|---|
| 38 |                 (a[i]){i};
 | 
|---|
| 39 |         }
 | 
|---|
| 40 |         for(i; 5) printf("func %d\n", a[i].mem);
 | 
|---|
| 41 |     }
 | 
|---|
| 42 |     printf(" [2]\n");
 | 
|---|
| 43 |     {
 | 
|---|
| 44 |         array(thing, 2, 3) a = { delay_init };
 | 
|---|
| 45 |         printf("before ctors\n");
 | 
|---|
| 46 |         for(i; 2) for(j; 3) {
 | 
|---|
| 47 |             if (i == 1 && j == 1)
 | 
|---|
| 48 |                 (a[i][j]){};
 | 
|---|
| 49 |             else if (i == 1 && j == 2)
 | 
|---|
| 50 |                 (a[i][j]){888};
 | 
|---|
| 51 |             else
 | 
|---|
| 52 |                 (a[i][j]){100 + 10 * i + j};
 | 
|---|
| 53 |         }
 | 
|---|
| 54 |         for(i; 2) for(j; 3) {
 | 
|---|
| 55 |             printf("func %d at (%d, %d)\n", a[i][j].mem, i, j);
 | 
|---|
| 56 |         }
 | 
|---|
| 57 |     }
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | void test_uCxxCheatSheet() {
 | 
|---|
| 61 |     struct S {
 | 
|---|
| 62 |         int i;
 | 
|---|
| 63 |     };
 | 
|---|
| 64 |     void ?{}( S & s, int i ) { s.i = i; sout | "ctor" | s.i; }
 | 
|---|
| 65 |     void ^?{}( S & s ) { sout | "dtor" | s.i; }
 | 
|---|
| 66 | //  int main() {
 | 
|---|
| 67 |         enum { N = 5 };
 | 
|---|
| 68 |         array(S, N) s = { delay_init };   // no constructor calls
 | 
|---|
| 69 |         for ( i; N ) s[i]{ i };
 | 
|---|
| 70 |         for ( i; N ) sout | s[i].i;
 | 
|---|
| 71 | //  }
 | 
|---|
| 72 | }
 | 
|---|
| 73 | 
 | 
|---|
| 74 | void test_extras() {
 | 
|---|
| 75 | 
 | 
|---|
| 76 |     printf("=== uninit alt ( uArray )\n");
 | 
|---|
| 77 |     test_uninit_alt();
 | 
|---|
| 78 | 
 | 
|---|
| 79 |     printf("=== uC++ cheat sheet\n");
 | 
|---|
| 80 |     test_uCxxCheatSheet();
 | 
|---|
| 81 | }
 | 
|---|