source: tests/raii/memberCtors.cfa @ 221c542e

Last change on this file since 221c542e was 6c7fe7b, checked in by Peter A. Buhr <pabuhr@…>, 16 months ago

change from printf to sout

  • Property mode set to 100644
File size: 2.6 KB
Line 
1#include <fstream.hfa>
2
3struct WrappedInt {
4        int x;
5        int id;
6};
7int intID = 0;
8
9void ?{}( WrappedInt & this ) {
10        this.id = intID++;
11        sout | "constructing int id: " | this.id;
12        this.x = 0;
13}
14
15void ?{}( WrappedInt & this, WrappedInt other ) {
16        this.id = intID++;
17        sout | "copy constructing int: " | other.x | "id: " | this.id;
18        this.x = other.x;
19}
20
21void ?{}( WrappedInt & this, int x ) {
22        this.id = intID++;
23        sout | "constructing int: " | x | "id: " | this.id;
24        this.x = x;
25}
26
27void ^?{}( WrappedInt & this ) {
28        sout | "destructing int: " | this.x | "id: " | this.id;
29}
30
31/* WrappedInt */ void ?=?( WrappedInt & this, int x ) {
32        sout | "assigning int: " | this.x | x | "id: " | this.id;
33        this.x = x;
34        // return this;
35}
36
37// WrappedInt ?=?( WrappedInt & this, WrappedInt other ) {
38//   sout | "assigning int: " | this.x | other.x;
39//   this.x = other.x;
40//   return this;
41// }
42
43struct A {
44        WrappedInt x, y, z;
45        int id;
46};
47int AID = 0;
48
49void ?{}( A & a ) {
50        // currently must define default ctor, since there's no "= default" syntax
51        a.id = AID++;
52        sout | "default construct A" | a.id;
53}
54
55void ?{}( A & a, int x ) {
56        a.id = AID++;
57        sout | "begin construct A id: " | a.id;
58        sout | "construct a.x";
59        (a.x){ x+999 };
60        sout | "assign a.y";
61        a.y = 0; // not a constructor - default constructor will be inserted
62        sout | "end construct A";
63} // z never constructed - will be automatically default constructed
64
65void ?{}( A & this, A other ) {
66        this.id = AID++;
67        sout | "begin copy construct A id: " | this.id;
68        sout | "copy construct this.x";
69        (this.x){ other.x };
70        sout | "assign this.y";
71        this.y = other.y; // not a constructor - copy constructor will be inserted
72        sout | "end copy construct A";
73} // z never constructed - will be automatically copy constructed
74
75A ?=?( A & this, A other ) {
76        sout | "begin ?=? A id: " | this.id;
77        this.x = other.x;
78        this.y = other.y;
79        this.z = other.z;
80        sout | "end ?=? A";
81        return this;
82}
83
84struct B {
85        A a1, a2, a3;
86        int id;
87};
88int BID = 0;
89
90void ?{}( B & b ) {
91        b.id = BID++;
92        sout | "begin construct B id: " | b.id;
93        sout | "assign b.a2";
94        b.a2 = (A){ 2 };
95        sout | "construct b.a1";
96        (b.a1){ 1 };
97#ifdef ERR1
98        (b.a2){ b.a3 }; // error, b->a2 was used previously but is explicitly constructed
99#endif
100        sout | "end construct B";
101} // a2, a3 never constructed - will be automatically default constructed
102
103void ^?{}( B & b ) {
104        b.id = BID++;
105        sout | "begin destruct B id: " | b.id;
106        b.a2 = (A) { 0 };
107        ^(b.a1){};
108        sout | "end destruct B";
109} // a2, a3 never destructed - will be automatically destructed
110
111int main() {
112        sout | "Before declaration of b1";
113        B b1;  // b1 = { { 1000, 0, 0 }, { 1001, 0, 0 }, { 0, 0, 0 } }
114        sout | "Before declaration of b2";
115        B b2 = b1;
116        sout | "End of main";
117}
Note: See TracBrowser for help on using the repository browser.