ACT-CV - Machine Vision for Cognitive Modeling
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
general
garbagecoll.h
Go to the documentation of this file.
1
// -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2
3
4
/*
5
ACT-CV - Machine Vision for Cognitive Modeling
6
Copyright (c) 2008 Marc Halbruegge
7
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
*/
22
34
#ifndef __GARBAGECOLL_H
35
#define __GARBAGECOLL_H
36
37
39
class
ReferenceCount
{
40
int
numReferences
;
41
public
:
42
ReferenceCount
() :
numReferences
(0) {};
43
virtual
~ReferenceCount
(){};
44
void
IncRefCount
() {
45
++
numReferences
;
46
};
47
void
DecRefCount
() {
48
if
(--
numReferences
== 0)
delete
this
;
49
}
50
};
51
52
57
template
<
class
_t >
58
class
GCPointer
{
59
typedef
_t
reference_type
;
60
_t *
ref
;
61
public
:
62
GCPointer
(_t *newRef =
nullptr
) {
63
ref
=newRef;
64
if
(
ref
)
ref
->IncRefCount();
65
}
66
~GCPointer
() {
67
if
(
ref
)
ref
->DecRefCount();
68
}
69
GCPointer
(
const
GCPointer
&gcp) {
70
ref
=gcp.
ref
;
71
if
(
ref
)
ref
->IncRefCount();
72
}
73
GCPointer
&
operator=
(
const
GCPointer
&gcp) {
74
_t *old_ref =
ref
;
75
ref
=gcp.
ref
;
76
if
(
ref
)
ref
->IncRefCount();
77
if
(old_ref) old_ref->DecRefCount();
78
return
*
this
;
79
}
80
GCPointer
&
operator=
(_t *newRef) {
81
_t *old_ref =
ref
;
82
ref
=newRef;
83
if
(
ref
)
ref
->IncRefCount();
84
if
(old_ref) old_ref->DecRefCount();
85
return
*
this
;
86
}
87
_t&
operator *
()
const
{
88
return
*
ref
;
89
}
90
_t*
operator ->
()
const
{
91
return
ref
;
92
}
93
operator
bool()
const
{
94
return
ref
!=
nullptr
;
95
}
96
};
97
99
template
<
class
_t>
100
class
GCPLess
{
101
public
:
102
bool
operator ()
(
const
GCPointer<_t>
&x,
const
GCPointer<_t>
&y)
const
{
103
return
(*x) < (*y);
104
}
105
};
106
107
#endif
ACT-CV - Machine Vision for Cognitive Modeling
© 2015
Marc Halbruegge
(actcvlibrary@googlemail.com)