blob: 8299881424eff53dd8f869f656485104d59f207a (
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
95
96
97
98
99
100
101
102
103
104
|
// file : Test/ReferenceCounting/Interface/interface.cpp
// author : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2002-2003 Boris Kolpackov
// license : http://kolpackov.net/license.html
#include "Utility/ReferenceCounting/Interface.hpp"
using namespace Utility::ReferenceCounting;
struct Obj : public virtual Interface
{
Obj ()
: ref_count_ (1)
{
}
virtual
~Obj () throw ()
{
}
public:
virtual void
add_ref () const throw ()
{
add_ref_i ();
}
virtual void
remove_ref () const throw ()
{
if (remove_ref_i ()) delete this;
}
virtual count_t
refcount_value () const throw ()
{
return refcount_value_i ();
}
protected:
virtual void
add_ref_i () const throw ()
{
++ref_count_;
}
virtual bool
remove_ref_i () const throw ()
{
return --ref_count_ == 0;
}
virtual count_t
refcount_value_i () const throw ()
{
return ref_count_;
}
private:
mutable count_t ref_count_;
};
struct E {};
void postcondition (bool p)
{
if (!p) throw E ();
}
int main ()
{
try
{
// add_ref
//
{
Obj* a (new Obj);
Obj* b (add_ref (a));
postcondition (a == b && a->refcount_value () == 2);
a->remove_ref ();
b->remove_ref ();
}
{
Obj* a (0);
Obj* b (add_ref (a));
postcondition (b == 0);
}
}
catch (...)
{
return -1;
}
}
//$Id$
|