blob: e0c5201396c4f01475b30b7267760e6459c40487 (
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#ifndef ICE_UTIL_SHARED_H
#define ICE_UTIL_SHARED_H
#include <IceUtil/Config.h>
//
// Base classes for reference counted types. The IceUtil::Handle
// template can be used for smart pointers to types derived from these
// bases.
//
// IceUtil::SimpleShared
// =====================
//
// A non thread-safe base class for reference-counted types.
//
// IceUtil::Shared
// ===============
//
// A thread-safe base class for reference-counted types.
//
namespace IceUtil
{
class SimpleShared : public noncopyable
{
public:
SimpleShared() :
_ref(0),
_noDelete(false)
{
}
virtual ~SimpleShared()
{
}
void __incRef()
{
assert(_ref >= 0);
++_ref;
}
void __decRef()
{
assert(_ref > 0);
if (--_ref == 0)
{
if(!_noDelete)
{
delete this;
}
}
}
int __getRef()
{
return _ref;
}
void __setNoDelete(bool b)
{
_noDelete = b;
}
private:
int _ref;
bool _noDelete;
};
class Shared : public noncopyable
{
public:
Shared() :
_ref(0),
_noDelete(false)
{
}
virtual ~Shared()
{
}
void __incRef()
{
_mutex.lock();
assert(_ref >= 0);
++_ref;
_mutex.unlock();
}
void __decRef()
{
_mutex.lock();
bool doDelete = false;
assert(_ref > 0);
if (--_ref == 0)
{
doDelete = !_noDelete;
}
_mutex.unlock();
if (doDelete)
{
delete this;
}
}
int __getRef()
{
_mutex.lock();
int ref = _ref;
_mutex.unlock();
return ref;
}
void __setNoDelete(bool b)
{
_mutex.lock();
_noDelete = b;
_mutex.unlock();
}
private:
int _ref;
bool _noDelete;
JTCMutex _mutex;
};
}
#endif
|