-
Notifications
You must be signed in to change notification settings - Fork 15
/
test1.cpp
209 lines (163 loc) · 4.3 KB
/
test1.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
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <memory>
#include "smart_ptr.h"
using namespace smart_ptr;
class CBase
{
public:
CBase() : data0(0)
{
std::cout << "CBase()" << std::endl;
}
virtual ~CBase()
{
std::cout << "~CBase()" << std::endl;
}
int data0;
private:
};
class CDevide : public CBase
{
public:
CDevide() : data2(3)
{
std::cout << "CDevide()" << std::endl;
}
~CDevide()
{
std::cout << "~CDevide()" << std::endl;
}
int data2;
private:
};
#ifndef CDECL
#if defined(WIN32)
#define CDECL _cdecl
#else
#define CDECL
#endif // defined(WIN32)
#endif // !CDECL
int test1(void)
{
strong_ptr<CBase> spBase(new CDevide());
{
strong_ptr<CDevide> spDummy(spBase);
weak_ptr<CBase> spW1(spBase);
spW1.lock()->data0++;
std::cout << spDummy->data0 << std::endl;
weak_ptr<CDevide> spW2(spBase);
spW2.lock()->data0++;
std::cout << spDummy->data0 << std::endl;
weak_ptr<CBase> spW3(spW1);
spW3.lock()->data0++;
std::cout << spDummy->data0 << std::endl;
weak_ptr<CDevide> spW4(spW1);
spW4.lock()->data0++;
std::cout << spDummy->data0 << std::endl;
weak_ptr<CBase> spW5;
spW5 = spBase;
spW5.lock()->data0++;
std::cout << spDummy->data0 << std::endl;
weak_ptr<CDevide> spW6;
spW6 = spBase;
spW6.lock()->data0++;
std::cout << spDummy->data0 << std::endl;
weak_ptr<CBase> spW7;
spW7 = spW1;
spW7.lock()->data0++;
std::cout << spDummy->data0 << std::endl;
weak_ptr<CDevide> spW8;
spW8 = spW1;
spW8.lock()->data0++;
std::cout << spDummy->data0 << std::endl;
}
{
weak_ptr<CBase> wpDummy(spBase);
strong_ptr<CBase> sp1(spBase);
sp1->data0++;
std::cout << sp1->data0 << std::endl;
strong_ptr<CDevide> sp2(spBase);
sp2->data0++;
std::cout << sp1->data0 << std::endl;
strong_ptr<CBase> sp3(wpDummy);
sp3->data0++;
std::cout << sp1->data0 << std::endl;
strong_ptr<CDevide> sp4(wpDummy);
sp4->data0++;
std::cout << sp1->data0 << std::endl;
strong_ptr<CBase> sp5;
sp5 = spBase;
sp5->data0++;
std::cout << sp1->data0 << std::endl;
strong_ptr<CDevide> sp6;
sp6 = spBase;
sp6->data0++;
std::cout << sp1->data0 << std::endl;
strong_ptr<CBase> sp7;
sp7 = wpDummy;
sp7->data0++;
std::cout << sp1->data0 << std::endl;
strong_ptr<CDevide> sp8;
sp8 = wpDummy;
sp8->data0++;
std::cout << sp1->data0 << std::endl;
}
return 0;
}
#include <memory>
void test2(void)
{
CBase * pObj = new CDevide();
#if 0
std::shared_ptr<CBase> sp1;
sp1 = (pObj);
std::weak_ptr<CBase> wsp;
wsp = (sp1);
#else
smart_ptr::strong_ptr<CBase> sp1;
sp1.reset(pObj);
smart_ptr::weak_ptr<CDevide> wsp;
wsp.reset(sp1);
if (sp1 == wsp.lock()) {
std::cout << "Equal" <<std::endl;
}
#endif
}
#if (defined(_WIN32) || defined(WIN32)) //&& defined(_MSC_VER)
#include <objbase.h>
DEFINE_COM_STRONG_PTR(EMPTY_NAME_SPACE, IMalloc); // defining IMallocComPtr type
void test5(void)
{
CoInitialize(NULL);
{
IMallocComPtr spOuter;
IMalloc *rawPtr = NULL;
void * pMem = NULL;
::CoGetMalloc(1, &rawPtr);
if (rawPtr) {
// we must using make_com_strong_ptr calling to create a IMallocComPtr type pointer.
IMallocComPtr spInner = make_com_strong_ptr<IMalloc>(rawPtr);
// Release the raw pointer.
// The reference count of the COM object itself will changed from 2 to ONE(1).
rawPtr->Release();
rawPtr = NULL;
// using our pointer to call functions.
pMem = spInner->Alloc(32);
// NOTE: After this call, the reference count of the COM object itself is still ONE(1).
spOuter = spInner;
}
spOuter->Free(pMem);
//spOuter->Release(); // this line will cause compiling error.
}
CoUninitialize();
}
#else
void test5(void){}
#endif // _MSC_VER
int CDECL main(void)
{
test1();
test2();
test5();
return 0;
}