GNU CommonC++
Main Page
Related Pages
Namespaces
Classes
Files
Examples
File List
File Members
inc
cc++
pointer.h
Go to the documentation of this file.
1
// Copyright (C) 2001 Open Source Telecom Corporation.
2
//
3
// This program is free software; you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation; either version 2 of the License, or
6
// (at your option) any later version.
7
//
8
// This program is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with this program; if not, write to the Free Software
15
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
//
17
// As a special exception to the GNU General Public License, permission is
18
// granted for additional uses of the text contained in its release
19
// of Common C++.
20
//
21
// The exception is that, if you link the Common C++ library with other
22
// files to produce an executable, this does not by itself cause the
23
// resulting executable to be covered by the GNU General Public License.
24
// Your use of that executable is in no way restricted on account of
25
// linking the Common C++ library code into it.
26
//
27
// This exception does not however invalidate any other reasons why
28
// the executable file might be covered by the GNU General Public License.
29
//
30
// This exception applies only to the code released under the
31
// name Common C++. If you copy code from other releases into a copy of
32
// Common C++, as the General Public License permits, the exception does
33
// not apply to the code that you add in this way. To avoid misleading
34
// anyone as to the status of such modified files, you must delete
35
// this exception notice from them.
36
//
37
// If you write modifications of your own for Common C++, it is your choice
38
// whether to permit this exception to apply to your modifications.
39
// If you do not wish that, delete this exception notice.
40
46
#ifndef CCXX_MISSING_H_
47
#include <
cc++/missing.h
>
48
#endif
49
50
namespace
ost {
51
58
template
<
class
T>
59
class
Pointer
60
{
61
protected
:
62
unsigned
*
ptrCount
;
63
T *
ptrObject
;
64
65
void
ptrDetach
(
void
)
66
{
67
if
(
ptrCount
&& --(*
ptrCount
)==0) {
68
delete
ptrObject
;
69
delete
ptrCount
;
70
}
71
ptrObject
= NULL;
72
ptrCount
= NULL;
73
}
74
75
public
:
76
explicit
Pointer
(T* ptr = NULL) :
ptrObject
(ptr)
77
{
78
ptrCount
=
new
unsigned;
79
*
ptrCount
= 1;
80
}
81
82
Pointer
(
const
Pointer<T>
&ref)
83
{
84
ptrObject
= ref.
ptrObject
;
85
ptrCount
= ref.
ptrCount
;
86
++(*ptrCount);
87
}
88
89
inline
virtual
~Pointer
()
90
{
ptrDetach
();}
91
92
93
Pointer
&
operator=
(
const
Pointer<T>
&ref)
94
{
95
if
(
this
!= &ref) {
96
ptrDetach
();
97
ptrObject
= ref.
ptrObject
;
98
ptrCount
= ref.
ptrCount
;
99
++(*ptrCount);
100
}
101
return
*
this
;
102
}
103
104
inline
T&
operator*
()
const
105
{
return
*
ptrObject
;};
106
107
inline
T*
getObject
()
const
108
{
return
ptrObject
;};
109
110
inline
T*
operator->
()
const
111
{
return
ptrObject
;};
112
113
inline
bool
operator!
()
const
114
{
return
(*
ptrCount
== 1);};
115
116
inline
int
operator++
()
const
117
{
return
++(*ptrCount);};
118
119
int
operator--
()
const
120
{
121
if
(*
ptrCount
== 1) {
122
delete
this
;
123
return
0;
124
}
125
return
--(*ptrCount);
126
}
127
};
128
129
}
Generated by
1.8.1