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"
24 #include <boost/type_traits/is_base_of.hpp>
25 #include <boost/utility/enable_if.hpp>
28 # pragma warning(push)
29 // C4355: 'this' : used in base member initializer list
30 // Tell MSVC we know what we do and really want to do it this way.
31 # pragma warning(disable: 4355)
36 class SGVirtualWeakReferenced;
39 * Base class for all reference counted SimGear objects supporting weak
40 * references, not incrementing the reference count.
42 * Classes derived from this one are meant to be managed with the SGSharedPtr
43 * and SGWeakPtr classes.
45 * If the class hierarchy contains virtual base classes use
46 * SGVirtualWeakReferenced instead.
48 class SGWeakReferenced {
50 /// The object backref and the reference count for this object need to be
51 /// there in any case. Also these are per object and shall not be copied nor
53 /// The reference count for this object is stored in a secondary object that
54 /// is shared with all weak pointers to this current object. This way we
55 /// have an atomic decision using the reference count of this current object
56 /// if the backref is still valid. At the time where the atomic count is
57 /// equal to zero the object is considered dead.
58 SGWeakReferenced(void) :
59 mWeakData(new WeakData(this))
61 SGWeakReferenced(const SGWeakReferenced& weakReferenced) :
62 mWeakData(new WeakData(this))
64 ~SGWeakReferenced(void)
65 { mWeakData->mWeakReferenced = 0; }
67 /// Do not copy the weak backward references ...
68 SGWeakReferenced& operator=(const SGWeakReferenced&)
71 /// The usual operations on weak pointers.
72 /// The interface should stay the same then what we have in Referenced.
73 static unsigned get(const SGWeakReferenced* ref)
74 { if (ref) return ++(ref->mWeakData->mRefcount); else return 0; }
75 static unsigned put(const SGWeakReferenced* ref)
76 { if (ref) return --(ref->mWeakData->mRefcount); else return 0; }
77 static unsigned count(const SGWeakReferenced* ref)
78 { if (ref) return ref->mWeakData->mRefcount; else return 0; }
81 /// Support for weak references, not increasing the reference count
82 /// that is done through that small helper class which holds an uncounted
83 /// reference which is zeroed out on destruction of the current object
84 class WeakData : public SGReferenced {
86 WeakData(SGWeakReferenced* weakReferenced) :
88 mWeakReferenced(weakReferenced)
94 // Try to increment the reference count if the count is greater
95 // then zero. Since it should only be incremented iff it is nonzero, we
96 // need to check that value and try to do an atomic test and set. If this
97 // fails, try again. The usual lockless algorithm ...
103 } while (!mRefcount.compareAndExchange(count, count + 1));
104 // We know that as long as the refcount is not zero, the pointer still
105 // points to valid data. So it is safe to work on it.
106 return up_cast<T>(mWeakReferenced);
110 SGWeakReferenced* mWeakReferenced;
114 WeakData(const WeakData&);
115 WeakData& operator=(const WeakData&);
117 /// Upcast in a class hierarchy with a virtual base class
120 typename boost::enable_if<
121 boost::is_base_of<SGVirtualWeakReferenced, T>,
124 up_cast(SGWeakReferenced* ptr);
126 /// Upcast in a non-virtual class hierarchy
129 typename boost::disable_if<
130 boost::is_base_of<SGVirtualWeakReferenced, T>,
133 up_cast(SGWeakReferenced* ptr)
135 return static_cast<T*>(ptr);
139 SGSharedPtr<WeakData> mWeakData;
142 friend class SGWeakPtr;
146 * Base class for all reference counted SimGear objects with virtual base
147 * classes, supporting weak references.
149 * Classes derived from this one are meant to be managed with the SGSharedPtr
150 * and SGWeakPtr classes.
155 * public virtual SGVirtualWeakReferenced
159 * public virtual SGVirtualWeakReferenced
167 * SGSharedPtr<Derived> ptr( new Derived() );
168 * SGWeakPtr<Derived> weak_ptr( ptr );
169 * SGSharedPtr<Base1> ptr1( weak_ptr.lock() );
170 * SGSharedPtr<Base2> ptr2( weak_ptr.lock() );
174 class SGVirtualWeakReferenced:
175 public SGWeakReferenced
178 virtual ~SGVirtualWeakReferenced() {}
181 /// Upcast in a class hierarchy with a virtual base class
182 // We (clang) need the definition of SGVirtualWeakReferenced for the static_cast
184 typename boost::enable_if<
185 boost::is_base_of<SGVirtualWeakReferenced, T>,
188 SGWeakReferenced::WeakData::up_cast(SGWeakReferenced* ptr)
190 // First get the virtual base class, which then can be used to further
192 return dynamic_cast<T*>(static_cast<SGVirtualWeakReferenced*>(ptr));
196 # pragma warning(pop)