1 // Copyright (C) 2004-2009 Mathias Froehlich - Mathias.Froehlich@web.de
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library 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 GNU
11 // Library General Public License for more details.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #ifndef SGWeakReferenced_HXX
19 #define SGWeakReferenced_HXX
21 #include "SGReferenced.hxx"
22 #include "SGSharedPtr.hxx"
27 class SGWeakReferenced {
29 /// The object backref and the reference count for this object need to be
30 /// there in any case. Also these are per object and shall not be copied nor
32 /// The reference count for this object is stored in a secondary object that
33 /// is shared with all weak pointers to this current object. This way we
34 /// have an atomic decision using the reference count of this current object
35 /// if the backref is still valid. At the time where the atomic count is
36 /// equal to zero the object is considered dead.
37 SGWeakReferenced(void) :
38 mWeakData(new WeakData(this))
40 SGWeakReferenced(const SGWeakReferenced& weakReferenced) :
41 mWeakData(new WeakData(this))
43 ~SGWeakReferenced(void)
44 { mWeakData->mWeakReferenced = 0; }
46 /// Do not copy the weak backward references ...
47 SGWeakReferenced& operator=(const SGWeakReferenced&)
50 /// The usual operations on weak pointers.
51 /// The interface should stay the same then what we have in Referenced.
52 static unsigned get(const SGWeakReferenced* ref)
53 { if (ref) return ++(ref->mWeakData->mRefcount); else return 0u; }
54 static unsigned put(const SGWeakReferenced* ref)
55 { if (ref) return --(ref->mWeakData->mRefcount); else return ~0u; }
56 static unsigned count(const SGWeakReferenced* ref)
57 { if (ref) return ref->mWeakData->mRefcount; else return 0u; }
60 /// Support for weak references, not increasing the reference count
61 /// that is done through that small helper class which holds an uncounted
62 /// reference which is zeroed out on destruction of the current object
63 class WeakData : public SGReferenced {
65 WeakData(SGWeakReferenced* weakReferenced) :
67 mWeakReferenced(weakReferenced)
73 // Try to increment the reference count if the count is greater
74 // then zero. Since it should only be incremented iff it is nonzero, we
75 // need to check that value and try to do an atomic test and set. If this
76 // fails, try again. The usual lockless algorithm ...
82 } while (!mRefcount.compareAndExchange(count, count + 1));
83 // We know that as long as the refcount is not zero, the pointer still
84 // points to valid data. So it is safe to work on it.
85 return static_cast<T*>(mWeakReferenced);
89 SGWeakReferenced* mWeakReferenced;
93 WeakData(const WeakData&);
94 WeakData& operator=(const WeakData&);
97 SGSharedPtr<WeakData> mWeakData;
100 friend class SGWeakPtr;