PEBL
2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
rc_ptrs.h
Go to the documentation of this file.
1
//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3
// Name: src/utility/CountedPounter.h
4
// Purpose: Contains definition for a slim counted pointer template class
5
// Author: Shane T. Mueller, Ph.D.
6
// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
7
// License: GPL 2
8
//
9
//
10
//
11
// This file is part of the PEBL project.
12
//
13
// PEBL is free software; you can redistribute it and/or modify
14
// it under the terms of the GNU General Public License as published by
15
// the Free Software Foundation; either version 2 of the License, or
16
// (at your option) any later version.
17
//
18
// PEBL is distributed in the hope that it will be useful,
19
// but WITHOUT ANY WARRANTY; without even the implied warranty of
20
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
// GNU General Public License for more details.
22
//
23
// You should have received a copy of the GNU General Public License
24
// along with PEBL; if not, write to the Free Software
25
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
#ifndef __RC_PTRS_H
28
#define __RC_PTRS_H
29
30
/*
31
32
This class is heavily based on a reference-counted pointer implementation by
33
Mark E. It included the below copyright statement, as well as a counted pointer
34
for use with arrays.
35
36
Copyright 1998, 1999
37
Mark E. (snowball3@usa.net)
38
http://snowball.digitalspace.net/cpp/
39
40
Permission to use, copy, modify, and distribute this software
41
and its documentation for any purpose is hereby granted without fee,
42
provided that the above copyright notice appear in all copies and
43
that both that copyright notice and this permission notice appear
44
in supporting documentation.
45
46
Mark E. makes no representations about the suitability of
47
this software for any purpose. It is provided "as is" without
48
express or implied warranty.
49
50
*/
51
52
53
54
55
/*
56
This header contains the classes counted_ptr and counted_array_ptr.
57
These classes implement reference-counted smart pointers that automatically
58
deletes the pointer it contains when no longer needed
59
i.e. (reference count drops to zero)
60
*/
61
#include <iostream>
62
#include <stddef.h>
63
64
template
<
class
X>
65
class
counted_ptr
66
{
67
//
68
// Public typedefs
69
//
70
public
:
71
typedef
X
element_type
;
72
typedef
X*
pointer_type
;
73
typedef
size_t
size_type
;
74
75
public
:
76
explicit
counted_ptr
(X* p=0) :
ptr
(p)
77
{
78
// Debug: std::cout <<"[RC_PTR] INITIATING counted_ptr for " << ptr << std::endl;
79
count
=
new
size_type
(1);
80
}
81
82
83
counted_ptr
(
const
counted_ptr<X>
&r)
84
{
85
// Debug: std::cout <<"[RC_PTR] COPY CONSTRUCTOR for ["<< r.ptr << "]"<<std::endl;
86
ptr
=r.
ptr
;
87
count
=r.
count
;
88
acquire
();
89
}
90
91
92
~counted_ptr
() {
93
release
();
94
}
95
96
97
counted_ptr
&
operator=
(
const
counted_ptr<X>
&r)
98
{
99
if
(
this
!= &r)
100
{
101
release
();
102
ptr
= r.
ptr
;
103
count
= r.
count
;
104
acquire
();
105
}
106
return
*
this
;
107
}
108
109
X&
operator*
()
const
{
return
*
ptr
; }
110
X*
get
()
const
{
return
ptr
; }
111
X*
operator->
()
const
{
return
ptr
; }
112
113
bool
unique
()
const
114
{
115
return
*
count
==1;
116
}
117
118
void
PrintCounts
()
119
{
120
// Debug: std::cout << "Object "<< ptr<<" has "<< *count << " copies" <<std::endl;
121
}
122
123
protected
:
124
X*
ptr
;
125
size_type
*
count
;
126
127
protected
:
128
129
void
acquire
()
130
{
131
// Debug: std::cout << "[RC_PTR] >>>>ACQUIRING " << ptr << "\n";
132
(*count) += 1;
133
// Debug: PrintCounts();
134
}
135
136
137
void
release
()
138
{
139
if
(
count
)
140
{
141
// Debug: std::cout << "[RC_PTR] <<<<<RELEASING\n";
142
// Debug: std::cout << "[RC_PTR] Object: " << ptr <<std::endl;
143
(*count)--;
144
// Debug: PrintCounts();
145
146
if
((*
count
)==0)
147
{
148
// Debug: std::cout << "[RC_PTR] *** COUNT IS ZERO; Deleting: "<< ptr<<std::endl;
149
delete
ptr
;
150
delete
count
;
151
ptr
=
NULL
;
152
count
=
NULL
;
153
}
154
}
155
}
156
};
157
158
#endif
159
NULL
#define NULL
Definition
BinReloc.cpp:317
counted_ptr
Definition
rc_ptrs.h:66
counted_ptr::pointer_type
X * pointer_type
Definition
rc_ptrs.h:72
counted_ptr::counted_ptr
counted_ptr(X *p=0)
Definition
rc_ptrs.h:76
counted_ptr::operator*
X & operator*() const
Definition
rc_ptrs.h:109
counted_ptr::acquire
void acquire()
Definition
rc_ptrs.h:129
counted_ptr::get
X * get() const
Definition
rc_ptrs.h:110
counted_ptr::ptr
X * ptr
Definition
rc_ptrs.h:124
counted_ptr::~counted_ptr
~counted_ptr()
Definition
rc_ptrs.h:92
counted_ptr::unique
bool unique() const
Definition
rc_ptrs.h:113
counted_ptr::PrintCounts
void PrintCounts()
Definition
rc_ptrs.h:118
counted_ptr::operator=
counted_ptr & operator=(const counted_ptr< X > &r)
Definition
rc_ptrs.h:97
counted_ptr::element_type
X element_type
Definition
rc_ptrs.h:71
counted_ptr::counted_ptr
counted_ptr(const counted_ptr< X > &r)
Definition
rc_ptrs.h:83
counted_ptr::size_type
size_t size_type
Definition
rc_ptrs.h:73
counted_ptr::count
size_type * count
Definition
rc_ptrs.h:125
counted_ptr::release
void release()
Definition
rc_ptrs.h:137
counted_ptr::operator->
X * operator->() const
Definition
rc_ptrs.h:111
src
utility
rc_ptrs.h
Generated by
1.9.8