diff options
author | David <david@xagasoft.com> | 2007-04-10 00:26:26 +0000 |
---|---|---|
committer | David <david@xagasoft.com> | 2007-04-10 00:26:26 +0000 |
commit | 531761d77331915b3e6495d10e03191ba10c04d2 (patch) | |
tree | 4ed256755d1379a87efb1d85974ba484e8e98441 /src/tests | |
parent | 015b1ee8079175cace17917d375d24175311b2f8 (diff) | |
download | libbu++-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 'src/tests')
-rw-r--r-- | src/tests/constsptr.cpp | 94 |
1 files changed, 94 insertions, 0 deletions
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 | |||