aboutsummaryrefslogtreecommitdiff
path: root/src/sptr.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/sptr.h85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/sptr.h b/src/sptr.h
new file mode 100644
index 0000000..c711a89
--- /dev/null
+++ b/src/sptr.h
@@ -0,0 +1,85 @@
1#ifndef SPTR_H
2#define SPTR_H
3
4#include <stdint.h>
5#include <stdio.h>
6
7template<typename T>
8class SPtr
9{
10public:
11 SPtr() :
12 pRefCnt( NULL ),
13 pData( NULL )
14 {
15 }
16
17 ~SPtr()
18 {
19 decCount();
20 }
21
22 SPtr( SPtr<T> &src ) :
23 pRefCnt( src.pRefCnt ),
24 pData( src.pData )
25 {
26 (*pRefCnt) += 1;
27 }
28
29 SPtr( T *pSrc ) :
30 pRefCnt( NULL ),
31 pData( pSrc )
32 {
33 pRefCnt = new uint32_t;
34 (*pRefCnt) = 1;
35 }
36
37 uint32_t count()
38 {
39 return *pRefCnt;
40 }
41
42 T *operator->() const
43 {
44 return pData;
45 }
46
47 T *operator*() const
48 {
49 return pData;
50 }
51
52 SPtr<T> operator=( const SPtr<T> &src )
53 {
54 decCount();
55 pRefCnt = src.pRefCnt;
56 pData = src.pData;
57 (*pRefCnt) += 1;
58 }
59
60 bool operator==( const SPtr<T> &src )
61 {
62 return pData == src.pData;
63 }
64
65private:
66 void decCount()
67 {
68 if( pRefCnt )
69 {
70 (*pRefCnt) -= 1;
71 if( (*pRefCnt) == 0 )
72 {
73 delete pRefCnt;
74 delete pData;
75 pRefCnt = NULL;
76 pData = NULL;
77 }
78 }
79 }
80
81 uint32_t *pRefCnt;
82 T *pData;
83};
84
85#endif