diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/sptr.h | 20 | ||||
-rw-r--r-- | src/tests/constsptr.cpp | 94 |
2 files changed, 109 insertions, 5 deletions
@@ -40,7 +40,7 @@ public: | |||
40 | (*pRefCnt) = 1; | 40 | (*pRefCnt) = 1; |
41 | } | 41 | } |
42 | 42 | ||
43 | int32_t count() | 43 | int32_t count() const |
44 | { | 44 | { |
45 | return *pRefCnt; | 45 | return *pRefCnt; |
46 | } | 46 | } |
@@ -65,7 +65,17 @@ public: | |||
65 | return *this; | 65 | return *this; |
66 | } | 66 | } |
67 | 67 | ||
68 | bool operator==( const SPtr<T> &src ) | 68 | const SPtr<T> operator=( const SPtr<T> &src ) const |
69 | { | ||
70 | decCount(); | ||
71 | pRefCnt = src.pRefCnt; | ||
72 | pData = src.pData; | ||
73 | (*pRefCnt) += 1; | ||
74 | |||
75 | return *this; | ||
76 | } | ||
77 | |||
78 | bool operator==( const SPtr<T> &src ) const | ||
69 | { | 79 | { |
70 | return pData == src.pData; | 80 | return pData == src.pData; |
71 | } | 81 | } |
@@ -81,7 +91,7 @@ public: | |||
81 | } | 91 | } |
82 | 92 | ||
83 | private: | 93 | private: |
84 | void decCount() | 94 | void decCount() const |
85 | { | 95 | { |
86 | if( pRefCnt ) | 96 | if( pRefCnt ) |
87 | { | 97 | { |
@@ -97,8 +107,8 @@ private: | |||
97 | } | 107 | } |
98 | } | 108 | } |
99 | 109 | ||
100 | int32_t *pRefCnt; | 110 | mutable int32_t *pRefCnt; |
101 | T *pData; | 111 | mutable T *pData; |
102 | }; | 112 | }; |
103 | 113 | ||
104 | template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src ) | 114 | template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src ) |
diff --git a/src/tests/constsptr.cpp b/src/tests/constsptr.cpp new file mode 100644 index 0000000..e6f87c7 --- /dev/null +++ b/src/tests/constsptr.cpp | |||
@@ -0,0 +1,94 @@ | |||
1 | #include <stdio.h> | ||
2 | #include "sptr.h" | ||
3 | |||
4 | template <typename T> | ||
5 | class DataBase | ||
6 | { | ||
7 | public: | ||
8 | DataBase(): | ||
9 | _bHas(false) | ||
10 | { | ||
11 | } | ||
12 | |||
13 | virtual ~DataBase() | ||
14 | { | ||
15 | clr(); | ||
16 | } | ||
17 | |||
18 | virtual bool has() const | ||
19 | { | ||
20 | return _bHas; | ||
21 | } | ||
22 | |||
23 | virtual void clr() | ||
24 | { | ||
25 | _bHas = false; | ||
26 | } | ||
27 | |||
28 | virtual void set(T d) | ||
29 | { | ||
30 | _tVal = d; | ||
31 | _bHas = true; | ||
32 | } | ||
33 | |||
34 | virtual T const &get() const | ||
35 | { | ||
36 | if(!has()) | ||
37 | throw "no data"; | ||
38 | return _tVal; | ||
39 | } | ||
40 | |||
41 | virtual T &get() | ||
42 | { | ||
43 | if(!has()) | ||
44 | throw "no data"; | ||
45 | return _tVal; | ||
46 | } | ||
47 | |||
48 | protected: | ||
49 | bool _bHas; | ||
50 | T _tVal; | ||
51 | }; | ||
52 | |||
53 | |||
54 | class Test | ||
55 | { | ||
56 | public: | ||
57 | Test(){}; | ||
58 | virtual ~Test(){}; | ||
59 | |||
60 | void set(int i) | ||
61 | { | ||
62 | _i = i; | ||
63 | } | ||
64 | |||
65 | int get() const | ||
66 | { | ||
67 | return _i; | ||
68 | } | ||
69 | |||
70 | private: | ||
71 | int _i; | ||
72 | }; | ||
73 | |||
74 | int main() | ||
75 | { | ||
76 | typedef SPtr<Test> TestPtr; | ||
77 | |||
78 | TestPtr t1(new Test); | ||
79 | t1->set(42); | ||
80 | |||
81 | printf("t1: %d.\n", t1->get()); | ||
82 | |||
83 | const TestPtr t2 = t1; | ||
84 | |||
85 | printf("t2: %d.\n", t2->get()); | ||
86 | |||
87 | typedef DataBase<const TestPtr> DBTP; | ||
88 | |||
89 | DBTP db; | ||
90 | db.set(t1); | ||
91 | |||
92 | printf("dbt1: %d.\n", db.get()->get()); | ||
93 | } | ||
94 | |||