Changeset 1665ee5
- Timestamp:
- Sep 11, 2024, 4:49:07 PM (2 months ago)
- Branches:
- master
- Children:
- 05f8761
- Parents:
- 6325bd4
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/collections/array.hfa
r6325bd4 r1665ee5 156 156 // other two. This solution offers ?{}() that needs only ?{}(), and similar for ^?{}. 157 157 158 // skip initializing elements 159 // array(float, 5) x = { delay_init }; 160 struct delay_init_t {} delay_init; 161 forall( [N], S & | sized(S), Timmed &, Tbase & ) 162 static inline void ?{}( arpk( N, S, Timmed, Tbase ) & this, delay_init_t ) { 163 void ?{}( S (&)[N] ) {} 164 ?{}(this.strides); 165 } 166 167 // call default ctor on elements 168 // array(float, 5) x; 158 169 forall( [N], S & | sized(S), Timmed &, Tbase & | { void ?{}( Timmed & ); } ) 159 170 static inline void ?{}( arpk( N, S, Timmed, Tbase ) & this ) { 160 void ?{}( S (&)[N] ) {} 161 ?{}(this.strides); 162 171 ?{}( this, delay_init ); 163 172 for (i; N) ?{}( (Timmed &)this.strides[i] ); 164 173 } … … 173 182 } 174 183 } 184 175 185 176 186 // -
tests/array-collections/.expect/array-raii-c.txt
r6325bd4 r1665ee5 70 70 dtor 1 71 71 dtor 0 72 === uninit 72 === uninit ( uNoCtor[] ) 73 73 [1] 74 74 before ctors -
tests/array-collections/.expect/array-raii-cfa.txt
r6325bd4 r1665ee5 70 70 dtor 1 71 71 dtor 0 72 === uninit 72 === uninit ( uNoCtor[] ) 73 73 [1] 74 74 before ctors … … 108 108 dtor 101 109 109 dtor 100 110 === uninit alt ( uArray ) 111 [1] 112 before ctors 113 ctor 0 114 ctor 999 115 ctor 888 116 ctor 3 117 ctor 4 118 func 0 119 func 999 120 func 888 121 func 3 122 func 4 123 dtor 4 124 dtor 3 125 dtor 888 126 dtor 999 127 dtor 0 128 [2] 129 before ctors 130 ctor 100 131 ctor 101 132 ctor 102 133 ctor 110 134 ctor 999 135 ctor 888 136 func 100 at (0, 0) 137 func 101 at (0, 1) 138 func 102 at (0, 2) 139 func 110 at (1, 0) 140 func 999 at (1, 1) 141 func 888 at (1, 2) 142 dtor 888 143 dtor 999 144 dtor 110 145 dtor 102 146 dtor 101 147 dtor 100 148 === uC++ cheat sheet 149 ctor 0 150 ctor 1 151 ctor 2 152 ctor 3 153 ctor 4 154 0 155 1 156 2 157 3 158 4 159 dtor 4 160 dtor 3 161 dtor 2 162 dtor 1 163 dtor 0 -
tests/array-collections/array-raii-c.cfa
r6325bd4 r1665ee5 20 20 21 21 #include "array-raii.hfa" 22 23 void test_extras() {} -
tests/array-collections/array-raii-cfa.cfa
r6325bd4 r1665ee5 16 16 // CFA array means like `array(float, 17) x;` 17 17 18 19 #include <fstream.hfa> 18 20 #include <collections/array.hfa> 19 21 … … 22 24 23 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 } -
tests/array-collections/array-raii.hfa
r6325bd4 r1665ee5 118 118 } 119 119 120 struct thing { int mem; }; 121 void ?{}( thing & this, int i ) { 122 (this.mem){ i }; 123 printf("ctor %d\n", this.mem); 124 } 125 void ?{}( thing & this ) { 126 (this){ 999 }; 127 } 128 void ^?{}( thing & this ) { 129 printf("dtor %d\n", this.mem); 130 } 131 120 132 // Array of uninits sees explicit ctor calls (only), and implied dtor calls 121 133 void test_uninit() { 122 struct thing { int mem; };123 void ?{}( thing & this, int i ) {124 (this.mem){ i };125 printf("ctor %d\n", this.mem);126 }127 void ?{}( thing & this ) {128 (this){ 999 };129 }130 void ^?{}( thing & this ) {131 printf("dtor %d\n", this.mem);132 }133 134 printf(" [1]\n"); 134 135 { … … 163 164 } 164 165 166 void test_extras(); 167 165 168 int main() { 166 169 printf("=== builtins\n"); … … 170 173 test_custom(); 171 174 172 printf("=== uninit \n");175 printf("=== uninit ( uNoCtor[] )\n"); 173 176 test_uninit(); 177 178 test_extras(); 174 179 }
Note: See TracChangeset
for help on using the changeset viewer.