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