Debian Codegraph @

Tests/PointerTest/pointertest.cpp

raw
1 #include <ClanLib/core.h>
2 #include <ClanLib/application.h>
3
4 #include <iostream>
5
6 using namespace std;
7
8 struct UCThing {
9 int x;
10 UCThing() : x(0) {cout << "--- UCThing default constructed\n";}
11 UCThing(const UCThing& other) : x(other.x) {cout << "--- UCThing copy\n";}
12 UCThing(int in) : x(in) {cout << "--- UCThing constructed\n";}
13 UCThing& operator=(const UCThing& other) {x = other.x; cout << "--- UCThing assignment\n"; return *this;}
14 ~UCThing() {cout << "--- UCThing destroyed\n";}
15 };
16
17 struct Thing : public CL_Clonable {
18 CL_Clonable* clone() const {return new Thing(*this);}
19
20 int x;
21 Thing() : x(0) {cout << "--- Thing default constructed\n";}
22 Thing(const Thing& other) : x(other.x) {cout << "--- Thing copy\n";}
23 Thing(int in) : x(in) {cout << "--- Thing constructed\n";}
24 Thing& operator=(const Thing& other) {x = other.x; cout << "--- Thing assignment\n"; return *this;}
25 ~Thing() {cout << "--- Thing destroyed\n";}
26 };
27
28 void shptr_test()
29 {
30 cout << "--- CL_SharedPtr Test ---\n";
31 cout << "Creating CL_SharedPtr<UCThing> a, given x value of 1\n";
32 CL_SharedPtr<UCThing> a(new UCThing(1));
33
34 cout << "Creating CL_SharedPtr<UCThing> b, given x value of 2\n";
35 CL_SharedPtr<UCThing> b(new UCThing(2));
36
37 cout << "a->x is: " << a->x << "\n";
38 cout << "b->x is: " << b->x << "\n";
39
40 cout << "Assigning 3 to a->x\n";
41 a->x = 3;
42
43 cout << "a->x is: " << a->x << "\n";
44 cout << "b->x is: " << b->x << "\n";
45
46 cout << "Assigning b to a\n";
47 a = b;
48
49 cout << "a->x is: " << a->x << "\n";
50 cout << "b->x is: " << b->x << "\n";
51
52 cout << "Assigning 18 to b->x\n";
53 b->x = 18;
54
55 cout << "a->x is: " << a->x << "\n";
56 cout << "b->x is: " << b->x << "\n";
57
58 cout << "Assigning 44 to a->x\n";
59 a->x = 44;
60
61 cout << "a->x is: " << a->x << "\n";
62 cout << "b->x is: " << b->x << "\n";
63
64 cout << "--- Done with CL_SharedPtr test ---\n";
65 }
66
67 void poptr_test()
68 {
69 cout << "\n--- CL_OwningPtr Test ---\n";
70
71 cout << "Creating CL_OwningPtr<Thing> a, given x value of 1\n";
72 CL_OwningPtr<Thing> a(new Thing(1));
73
74 cout << "Creating CL_OwningPtr<Thing> b, given x value of 2\n";
75 CL_OwningPtr<Thing> b(new Thing(2));
76
77 cout << "a->x is: " << a->x << "\n";
78 cout << "b->x is: " << b->x << "\n";
79
80 cout << "Assigning 3 to a->x\n";
81 a->x = 3;
82
83 cout << "a->x is: " << a->x << "\n";
84 cout << "b->x is: " << b->x << "\n";
85
86 cout << "Assigning b to a\n";
87 a = b;
88
89 cout << "a->x is: " << a->x << "\n";
90 cout << "b->x is: " << b->x << "\n";
91
92 cout << "Assigning 18 to b->x\n";
93 b->x = 18;
94
95 cout << "a->x is: " << a->x << "\n";
96 cout << "b->x is: " << b->x << "\n";
97
98 cout << "Assigning 44 to a->x\n";
99 a->x = 44;
100
101 cout << "a->x is: " << a->x << "\n";
102 cout << "b->x is: " << b->x << "\n";
103
104 cout << "--- Done with CL_OwningPtr test ---\n";
105 }
106
107 void cowptr_test()
108 {
109 cout << "\n--- CL_LazyCopyPtr Test ---\n";
110
111 cout << "Creating CL_LazyCopyPtr<Thing> a, given x value of 1\n";
112 CL_LazyCopyPtr<Thing> a(new Thing(1));
113
114 cout << "Creating CL_LazyCopyPtr<Thing> b, given x value of 2\n";
115 CL_LazyCopyPtr<Thing> b(new Thing(2));
116
117 cout << "a.cd().x is: " << a.cd().x << "\n";
118 cout << "b.cd().x is: " << b.cd().x << "\n";
119
120 cout << "Assigning 3 to a->x\n";
121 a->x = 3;
122
123 cout << "a.cd().x is: " << a.cd().x << "\n";
124 cout << "b.cd().x is: " << b.cd().x << "\n";
125
126 cout << "Assigning b to a\n";
127 a = b;
128
129 cout << "a.cd().x is: " << a.cd().x << "\n";
130 cout << "b.cd().x is: " << b.cd().x << "\n";
131
132 cout << "Assigning 18 to b->x\n";
133 b->x = 18;
134
135 cout << "a.cd().x is: " << a.cd().x << "\n";
136 cout << "b.cd().x is: " << b.cd().x << "\n";
137
138 cout << "Assigning 44 to a->x\n";
139 a->x = 44;
140
141 cout << "a.cd().x is: " << a.cd().x << "\n";
142 cout << "b.cd().x is: " << b.cd().x << "\n";
143
144 cout << "--- Done with CL_LazyCopyPtr test ---\n";
145 }
146
147 struct App : public CL_ClanApplication {
148 int main(int, char**) {
149 shptr_test();
150 poptr_test();
151 cowptr_test();
152
153 return 0;
154 }
155 } app;