blob: e6f87c77e595668994cf60f0414c15776abf9baf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#include <stdio.h>
#include "sptr.h"
template <typename T>
class DataBase
{
public:
DataBase():
_bHas(false)
{
}
virtual ~DataBase()
{
clr();
}
virtual bool has() const
{
return _bHas;
}
virtual void clr()
{
_bHas = false;
}
virtual void set(T d)
{
_tVal = d;
_bHas = true;
}
virtual T const &get() const
{
if(!has())
throw "no data";
return _tVal;
}
virtual T &get()
{
if(!has())
throw "no data";
return _tVal;
}
protected:
bool _bHas;
T _tVal;
};
class Test
{
public:
Test(){};
virtual ~Test(){};
void set(int i)
{
_i = i;
}
int get() const
{
return _i;
}
private:
int _i;
};
int main()
{
typedef SPtr<Test> TestPtr;
TestPtr t1(new Test);
t1->set(42);
printf("t1: %d.\n", t1->get());
const TestPtr t2 = t1;
printf("t2: %d.\n", t2->get());
typedef DataBase<const TestPtr> DBTP;
DBTP db;
db.set(t1);
printf("dbt1: %d.\n", db.get()->get());
}
|