Main Page
Namespaces
Classes
Files
Examples
File List
File Members
include
xqilla
framework
ReferenceCounted.hpp
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2001, 2008,
3
* DecisionSoft Limited. All rights reserved.
4
* Copyright (c) 2004, 2011,
5
* Oracle and/or its affiliates. All rights reserved.
6
*
7
* Licensed under the Apache License, Version 2.0 (the "License");
8
* you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at
10
*
11
* http://www.apache.org/licenses/LICENSE-2.0
12
*
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
18
*/
19
20
#ifndef _REFERENCECOUNTED_HPP
21
#define _REFERENCECOUNTED_HPP
22
23
#include <xqilla/framework/XQillaExport.hpp>
24
#include <
xercesc/framework/MemoryManager.hpp
>
25
26
// for null RefCountPointer instances
27
#define NULLRCP ((void *)0)
28
30
class
XQILLA_API
ReferenceCounted
31
{
32
public
:
33
ReferenceCounted
()
34
: _ref_count(0) {}
35
virtual
~ReferenceCounted
() {}
36
38
void
incrementRefCount
()
const
39
{
40
++
const_cast<
unsigned
int
&
>
(_ref_count);
41
}
42
44
virtual
void
decrementRefCount
()
const
45
{
46
if
(--const_cast<unsigned int&>(_ref_count) == 0) {
47
delete
this
;
48
}
49
}
50
51
unsigned
int
getRefCount
()
const
52
{
53
return
_ref_count;
54
}
55
56
protected
:
57
unsigned
int
_ref_count
;
// mutable
58
};
59
61
template
<
class
T>
class
RefCountPointer
62
{
63
public
:
64
RefCountPointer
(T *p = 0) :
_p
(p)
65
{
66
if
(
_p
!= 0)
_p
->incrementRefCount();
67
}
68
69
template
<
class
T2>
RefCountPointer
(
const
RefCountPointer<T2>
&o) :
_p
((T*)(T2*)o)
70
{
71
if
(
_p
!= 0)
_p
->incrementRefCount();
72
}
73
74
RefCountPointer
(
const
RefCountPointer<T>
&o) :
_p
(o.
_p
)
75
{
76
if
(
_p
!= 0)
_p
->incrementRefCount();
77
}
78
79
RefCountPointer
&
operator=
(
const
RefCountPointer<T>
&o)
80
{
81
if
(
_p
!= o.
_p
) {
82
if
(
_p
!= 0)
_p
->decrementRefCount();
83
_p
= o.
_p
;
84
if
(
_p
!= 0)
_p
->incrementRefCount();
85
}
86
return
*
this
;
87
}
88
89
~RefCountPointer
()
90
{
91
if
(
_p
!= 0)
_p
->decrementRefCount();
92
}
93
94
T *
operator->
()
const
95
{
96
return
_p
;
97
}
98
99
operator
T*()
const
100
{
101
return
_p
;
102
}
103
104
T *
get
()
const
105
{
106
return
_p
;
107
}
108
109
bool
isNull
()
const
110
{
111
return
(
_p
== 0);
112
}
113
114
bool
notNull
()
const
115
{
116
return
(
_p
!= 0);
117
}
118
119
protected
:
120
T *
_p
;
121
};
122
123
template
<
class
T1,
class
T2>
124
inline
bool
operator==
(
const
RefCountPointer<T1>
&a,
const
RefCountPointer<T2>
&b)
125
{
126
return
(
void
*)(T1*)a == (
void
*)(T2*)b;
127
}
128
129
template
<
class
T1,
class
T2>
130
inline
bool
operator!=
(
const
RefCountPointer<T1>
&a,
const
RefCountPointer<T2>
&b)
131
{
132
return
(
void
*)(T1*)a != (
void
*)(T2*)b;
133
}
134
135
template
<
class
T>
136
inline
bool
operator==
(
const
RefCountPointer<T>
&a,
void
*b)
137
{
138
return
(T*)a == (T*)b;
139
}
140
141
template
<
class
T>
142
inline
bool
operator!=
(
const
RefCountPointer<T>
&a,
void
*b)
143
{
144
return
(T*)a != (T*)b;
145
}
146
147
#endif
Generated by
1.8.4