-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_smarter_pointer.cpp
48 lines (41 loc) · 1.01 KB
/
test_smarter_pointer.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
#include "smarterpointer.h"
#include <iostream>
class Font : public RefObject
{
public:
Font() { std::cout << "Font()" << std::endl; }
~Font() { std::cout << "~Font()" << std::endl; }
};
SMARTER_PTR(Font)
class FontWrapper
{
FontWPtr m_wptrFont;
public:
FontWrapper() { std::cout << "FontWrapper()" << std::endl; }
~FontWrapper() { std::cout << "~FontWrapper()" << std::endl; }
void setFont(FontPtr font) { m_wptrFont = font; }
void doSomething()
{
if (m_wptrFont.expired())
{
std::cout << "m_wptrFont is expired" << std::endl;
}
else
{
FontPtr ptrFont = m_wptrFont.lock();
if (ptrFont) { std::cout << "m_wptrFont is valid" << std::endl; }
else { std::cout << "m_wptrFont is invalid" << std::endl; }
}
}
};
int main()
{
FontWrapper wrapper;
{
FontPtr font = new Font();
wrapper.setFont(font);
wrapper.doSomething();
}
wrapper.doSomething();
return 0;
}