-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
200 lines (182 loc) · 4.2 KB
/
client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include "gc.h"
using namespace std;
class X
{
int a;
public:
X() { a = 10; }
X(int n) : a(n){};
void f()
{
cout << "This is X(" << a << ")\n";
}
friend ostream &operator<<(ostream &o, const X &x)
{
return o << x.a;
}
};
class Y
{
int b;
GCBase<X> x;
public:
Y(int n) : b(n), x(n + 1){};
void f()
{
cout << "This is Y(" << b << ")\n";
x->f();
}
};
class Z
{
int c;
string d;
GCBase<X> x;
GCBase<Y> y;
public:
Z(int n, string a) : c(n), d(a), x(n + 1), y(n + 2){};
~Z() { cout << "Dtor Z\n"; }
void f()
{
cout << "This is Z(" << c << "," << d << ")\n";
x->f();
y->f();
}
};
class A
{
int a;
public:
A(int n) : a(n){};
virtual ~A(){};
virtual void f() { cout << "A\n"; };
};
class B : public A
{
int b;
public:
B(int n) : A(n), b(n){};
virtual void f() { cout << "B\n"; };
};
int main()
{
// Simple types
cout << "Simple Integers a; b; c\n";
{
GCBase<int> a = 0;
GCBase<int> b = 1;
GCBase<int> c = 1;
++a;
b = 1729;
c = b * a;
cout << "a: " << a << '\n';
cout << "b: " << b << '\n';
cout << "c: " << c << '\n';
memSingleton::get().print_info();
}
cout << "Destructing a; b; c\n";
memSingleton::get().print_info();
// Basic class
cout << "Basic Class x\n";
{
GCBase<X> x(2);
x->f();
cout << "x:" << x << '\n';
memSingleton::get().print_info();
}
cout << "Destructing x\n";
memSingleton::get().print_info();
memSingleton::get().force_collect();
// Nested class
cout << "Nested Class y\n";
{
GCBase<Y> y(3);
y->f();
memSingleton::get().print_info();
}
cout << "Destructing class y\n";
memSingleton::get().print_info();
// Nested class with Multiple references
cout << "Nested Class with multiple references\n";
{
GCBase<Z> z(5, "Malleshwaram");
z->f();
memSingleton::get().print_info();
}
cout << "Destructing class z\n";
memSingleton::get().print_info();
// Copy Ctors
cout << "Nested Class with multiple references and copy ctor\n";
{
GCBase<Z> z1(9, "Halli mane");
GCBase<Z> z2(z1);
z1->f();
z2->f();
memSingleton::get().print_info();
}
cout << "Destructing class z1 and z2\n";
memSingleton::get().print_info();
// Assignment operator
cout << "Nested Class with multiple references, assignment operator\n";
{
GCBase<Y> y1(20);
GCBase<Y> y2(30);
y1->f();
cout << "------\n";
y2->f();
y1 = y2;
cout << "-----------\n";
y1->f();
cout << "------\n";
y2->f();
cout << "-----------\n";
memSingleton::get().print_info();
}
cout << "Destructing class y1 and y2\n";
memSingleton::get().print_info();
// Memory expansion
cout << "Memory expansion\n";
{
GCBase<X> x1(1);
GCBase<double> x2(8.0);
GCBase<X> x3(3);
cout << x1 << '\n';
cout << x2 << '\n';
cout << x3 << '\n';
GCBase<X> x4(4);
GCBase<int> x5(5);
GCBase<X> x6(6);
cout << x1 << '\n';
cout << x2 << '\n';
cout << x3 << '\n';
cout << x4 << '\n';
cout << x5 << '\n';
cout << x6 << '\n';
GCBase<string> x7("Hello, World!\n");
GCBase<X> x8(8);
GCBase<X> x9(9);
cout << x1 << '\n';
cout << x2 << '\n';
cout << x3 << '\n';
cout << x4 << '\n';
cout << x5 << '\n';
cout << x6 << '\n';
cout << (string)(x7);
cout << x8 << '\n';
cout << x9 << '\n';
memSingleton::get().print_info();
}
cout << "Destructing class x{1..9}\n";
memSingleton::get().print_info();
//Inheritance
cout << "Inheritance(Experimental Interface)\n";
{
GCBase<B> y(1);
GCBase<A> *a = (GCBase<A> *)&y;
(*a)->f();
memSingleton::get().print_info();
}
cout << "Destructing Inheritance classes\n";
memSingleton::get().print_info();
}