aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/sptr.h15
-rw-r--r--src/tests/sptr.cpp5
2 files changed, 18 insertions, 2 deletions
diff --git a/src/sptr.h b/src/sptr.h
index 23c21dc..0ea8bca 100644
--- a/src/sptr.h
+++ b/src/sptr.h
@@ -4,9 +4,14 @@
4#include <stdint.h> 4#include <stdint.h>
5#include <stdio.h> 5#include <stdio.h>
6 6
7template<typename T> class SPtr;
8template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src );
9
7template<typename T> 10template<typename T>
8class SPtr 11class SPtr
9{ 12{
13 template<typename Tb, typename Ta>
14 friend SPtr<Tb> SPtrCast( SPtr<Ta> pt );
10public: 15public:
11 SPtr() : 16 SPtr() :
12 pRefCnt( NULL ), 17 pRefCnt( NULL ),
@@ -96,4 +101,14 @@ private:
96 T *pData; 101 T *pData;
97}; 102};
98 103
104template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src )
105{
106 SPtr<Tb> ret;
107 ret.pRefCnt = src.pRefCnt;
108 ret.pData = dynamic_cast<Tb *>(src.pData);
109 if( ret.pRefCnt )
110 (*(ret.pRefCnt)) += 1;
111 return ret;
112}
113
99#endif 114#endif
diff --git a/src/tests/sptr.cpp b/src/tests/sptr.cpp
index bc00f78..252463b 100644
--- a/src/tests/sptr.cpp
+++ b/src/tests/sptr.cpp
@@ -65,11 +65,12 @@ int main()
65 beAnnoying( pt3 ); 65 beAnnoying( pt3 );
66 66
67 { 67 {
68 SPtr<Annoy2> pt4( dynamic_cast<SPtr<Annoy2> >(pt3) ); 68 SPtr<Annoy2> pt4( SPtrCast<Annoy2>( pt3 ) );
69 printf("Count: %d\n", pt4.count() ); 69 printf("Count: %d\n", pt4.count() );
70 70
71 pt4.go2(); 71 pt4->go2();
72 } 72 }
73 printf("Count: %d\n", pt.count() );
73 } 74 }
74 printf("Count: %d\n", pt.count() ); 75 printf("Count: %d\n", pt.count() );
75 } 76 }