1
// RefCount.hh for FbTk - Fluxbox Toolkit
2
// Copyright (c) 2002 Henrik Kinnunen (fluxgen at fluxbox dot org)
4
// Permission is hereby granted, free of charge, to any person obtaining a
5
// copy of this software and associated documentation files (the "Software"),
6
// to deal in the Software without restriction, including without limitation
7
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
// and/or sell copies of the Software, and to permit persons to whom the
9
// Software is furnished to do so, subject to the following conditions:
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
// DEALINGS IN THE SOFTWARE.
22
#ifndef FBTK_REFCOUNT_HH
23
#define FBTK_REFCOUNT_HH
27
/// holds a pointer with reference counting, similar to std:auto_ptr
28
template <typename Pointer>
32
explicit RefCount(Pointer *p);
33
explicit RefCount(RefCount<Pointer> ©);
34
RefCount(const RefCount<Pointer> ©);
36
RefCount<Pointer> &operator = (const RefCount<Pointer> ©);
37
RefCount<Pointer> &operator = (Pointer *p);
38
Pointer *operator * () const { return get(); }
39
Pointer *operator -> () const { return get(); }
40
Pointer *get() const { return m_data; }
41
/// @return number of referenses
42
unsigned int usedBy() const { return (m_refcount != 0 ? *m_refcount : 0); }
44
/// increase referense count
46
/// decrease referense count
48
Pointer *m_data; ///< data holder
49
mutable unsigned int *m_refcount; ///< holds reference counting
54
template <typename Pointer>
55
RefCount<Pointer>::RefCount():m_data(0), m_refcount(new unsigned int(0)) {
59
template <typename Pointer>
60
RefCount<Pointer>::RefCount(RefCount<Pointer> ©):
62
m_refcount(copy.m_refcount) {
66
template <typename Pointer>
67
RefCount<Pointer>::RefCount(Pointer *p):m_data(p), m_refcount(new unsigned int(0)) {
71
template <typename Pointer>
72
RefCount<Pointer>::RefCount(const RefCount<Pointer> ©):
74
m_refcount(copy.m_refcount) {
78
template <typename Pointer>
79
RefCount<Pointer>::~RefCount() {
83
template <typename Pointer>
84
RefCount<Pointer> &RefCount<Pointer>::operator = (const RefCount<Pointer> ©) {
85
decRefCount(); // dec current ref count
86
m_refcount = copy.m_refcount; // set new ref count
87
m_data = copy.m_data; // set new data pointer
88
incRefCount(); // inc new ref count
92
template <typename Pointer>
93
RefCount<Pointer> &RefCount<Pointer>::operator = (Pointer *p) {
95
m_data = p; // set data pointer
96
m_refcount = new unsigned int(0); // create new counter
101
template <typename Pointer>
102
void RefCount<Pointer>::decRefCount() {
105
if (*m_refcount == 0) { // already zero, then delete refcount
111
if (*m_refcount == 0) { // destroy m_data and m_refcount if nobody else is using this
120
template <typename Pointer>
121
void RefCount<Pointer>::incRefCount() {
127
} // end namespace FbTk
129
#endif // FBTK_REFCOUNT_HH