Last change
on this file since 51574 was 50213, checked in by vboxsync, 11 years ago |
spaces
|
-
Property svn:eol-style
set to
native
-
Property svn:keywords
set to
Author Date Id Revision
|
File size:
1.1 KB
|
Line | |
---|
1 | #ifndef __SHARED_PTR_H__
|
---|
2 | #define __SHARED_PTR_H__
|
---|
3 |
|
---|
4 | #ifdef __cplusplus
|
---|
5 | template<typename T>
|
---|
6 | class SharedPtr
|
---|
7 | {
|
---|
8 | struct imp
|
---|
9 | {
|
---|
10 | imp(T *pTrg = NULL, int cnt = 1): ptr(pTrg),refcnt(cnt){}
|
---|
11 | ~imp() { if (ptr) delete ptr;}
|
---|
12 |
|
---|
13 | T *ptr;
|
---|
14 | int refcnt;
|
---|
15 | };
|
---|
16 |
|
---|
17 |
|
---|
18 | public:
|
---|
19 | SharedPtr(T *t = NULL):p(NULL)
|
---|
20 | {
|
---|
21 | p = new imp(t);
|
---|
22 | }
|
---|
23 |
|
---|
24 | ~SharedPtr()
|
---|
25 | {
|
---|
26 | p->refcnt--;
|
---|
27 |
|
---|
28 | if (p->refcnt == 0)
|
---|
29 | delete p;
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | SharedPtr(const SharedPtr& rhs)
|
---|
34 | {
|
---|
35 | p = rhs.p;
|
---|
36 | p->refcnt++;
|
---|
37 | }
|
---|
38 |
|
---|
39 | const SharedPtr& operator= (const SharedPtr& rhs)
|
---|
40 | {
|
---|
41 | if (p == rhs.p) return *this;
|
---|
42 |
|
---|
43 | p->refcnt--;
|
---|
44 | if (p->refcnt == 0)
|
---|
45 | delete p;
|
---|
46 |
|
---|
47 | p = rhs.p;
|
---|
48 | p->refcnt++;
|
---|
49 |
|
---|
50 | return *this;
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | T *get() const
|
---|
55 | {
|
---|
56 | return p->ptr;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | T *operator->()
|
---|
61 | {
|
---|
62 | return p->ptr;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | const T*operator->() const
|
---|
67 | {
|
---|
68 | return p->ptr;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | int use_count()
|
---|
73 | {
|
---|
74 | return p->refcnt;
|
---|
75 | }
|
---|
76 |
|
---|
77 | private:
|
---|
78 | imp *p;
|
---|
79 | };
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | #endif
|
---|
Note:
See
TracBrowser
for help on using the repository browser.