aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid <david@xagasoft.com>2007-04-10 00:26:26 +0000
committerDavid <david@xagasoft.com>2007-04-10 00:26:26 +0000
commit531761d77331915b3e6495d10e03191ba10c04d2 (patch)
tree4ed256755d1379a87efb1d85974ba484e8e98441
parent015b1ee8079175cace17917d375d24175311b2f8 (diff)
downloadlibbu++-531761d77331915b3e6495d10e03191ba10c04d2.tar.gz
libbu++-531761d77331915b3e6495d10e03191ba10c04d2.tar.bz2
libbu++-531761d77331915b3e6495d10e03191ba10c04d2.tar.xz
libbu++-531761d77331915b3e6495d10e03191ba10c04d2.zip
david - wow... that seems a little kludgy... see the constsptr test for details, but basically i had to make the members of sptr mutable to get this to work the way it seems it should... maybe i'm missing something...
Diffstat (limited to '')
-rw-r--r--src/sptr.h20
-rw-r--r--src/tests/constsptr.cpp94
2 files changed, 109 insertions, 5 deletions
diff --git a/src/sptr.h b/src/sptr.h
index 0ea8bca..9a6a2d6 100644
--- a/src/sptr.h
+++ b/src/sptr.h
@@ -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
83private: 93private:
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
104template< typename Tb, typename Ta > SPtr<Tb> SPtrCast( SPtr<Ta> src ) 114template< 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
4template <typename T>
5class DataBase
6{
7public:
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
48protected:
49 bool _bHas;
50 T _tVal;
51};
52
53
54class Test
55{
56public:
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
70private:
71 int _i;
72};
73
74int 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