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"
25 # pragma warning(push)
26 // C4355: 'this' : used in base member initializer list
27 // Tell MSVC we know what we do and really want to do it this way.
28 # pragma warning(disable: 4355)
34 class SGWeakReferenced {
36 /// The object backref and the reference count for this object need to be
37 /// there in any case. Also these are per object and shall not be copied nor
39 /// The reference count for this object is stored in a secondary object that
40 /// is shared with all weak pointers to this current object. This way we
41 /// have an atomic decision using the reference count of this current object
42 /// if the backref is still valid. At the time where the atomic count is
43 /// equal to zero the object is considered dead.
44 SGWeakReferenced(void) :
45 mWeakData(new WeakData(this))
47 SGWeakReferenced(const SGWeakReferenced& weakReferenced) :
48 mWeakData(new WeakData(this))
50 ~SGWeakReferenced(void)
51 { mWeakData->mWeakReferenced = 0; }
53 /// Do not copy the weak backward references ...
54 SGWeakReferenced& operator=(const SGWeakReferenced&)
57 /// The usual operations on weak pointers.
58 /// The interface should stay the same then what we have in Referenced.
59 static unsigned get(const SGWeakReferenced* ref)
60 { if (ref) return ++(ref->mWeakData->mRefcount); else return 0u; }
61 static unsigned put(const SGWeakReferenced* ref)
62 { if (ref) return --(ref->mWeakData->mRefcount); else return ~0u; }
63 static unsigned count(const SGWeakReferenced* ref)
64 { if (ref) return ref->mWeakData->mRefcount; else return 0u; }
67 /// Support for weak references, not increasing the reference count
68 /// that is done through that small helper class which holds an uncounted
69 /// reference which is zeroed out on destruction of the current object
70 class WeakData : public SGReferenced {
72 WeakData(SGWeakReferenced* weakReferenced) :
74 mWeakReferenced(weakReferenced)
80 // Try to increment the reference count if the count is greater
81 // then zero. Since it should only be incremented iff it is nonzero, we
82 // need to check that value and try to do an atomic test and set. If this
83 // fails, try again. The usual lockless algorithm ...
89 } while (!mRefcount.compareAndExchange(count, count + 1));
90 // We know that as long as the refcount is not zero, the pointer still
91 // points to valid data. So it is safe to work on it.
92 return static_cast<T*>(mWeakReferenced);
96 SGWeakReferenced* mWeakReferenced;
100 WeakData(const WeakData&);
101 WeakData& operator=(const WeakData&);
104 SGSharedPtr<WeakData> mWeakData;
107 friend class SGWeakPtr;
111 # pragma warning(pop)