]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGWeakReferenced.hxx
7a2bcaf5fae97961181da0a7a826ca516ccf7672
[simgear.git] / simgear / structure / SGWeakReferenced.hxx
1 // Copyright (C) 2004-2009  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
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.
7 //
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.
12 //
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.
16 //
17
18 #ifndef SGWeakReferenced_HXX
19 #define SGWeakReferenced_HXX
20
21 #include "SGReferenced.hxx"
22 #include "SGSharedPtr.hxx"
23
24 #include <boost/type_traits/is_base_of.hpp>
25 #include <boost/utility/enable_if.hpp>
26
27 #ifdef _MSC_VER
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)
32 #endif
33
34 template<typename T>
35 class SGWeakPtr;
36 class SGVirtualWeakReferenced;
37
38 /**
39  * Base class for all reference counted SimGear objects supporting weak
40  * references, not incrementing the reference count.
41  *
42  * Classes derived from this one are meant to be managed with the SGSharedPtr
43  * and SGWeakPtr classes.
44  *
45  * If the class hierarchy contains virtual base classes use
46  * SGVirtualWeakReferenced instead.
47  */
48 class SGWeakReferenced {
49 public:
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
52   /// assigned.
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))
60   {}
61   SGWeakReferenced(const SGWeakReferenced& weakReferenced) :
62     mWeakData(new WeakData(this))
63   {}
64   ~SGWeakReferenced(void)
65   { mWeakData->mWeakReferenced = 0; }
66
67   /// Do not copy the weak backward references ...
68   SGWeakReferenced& operator=(const SGWeakReferenced&)
69   { return *this; }
70
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; }
79
80 private:
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 {
85   public:
86     WeakData(SGWeakReferenced* weakReferenced) :
87       mRefcount(0u),
88       mWeakReferenced(weakReferenced)
89     { }
90
91     template<typename T>
92     T* getPointer()
93     {
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 ...
98       unsigned count;
99       do {
100         count = mRefcount;
101         if (count == 0)
102           return 0;
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);
107     }
108
109     SGAtomic mRefcount;
110     SGWeakReferenced* mWeakReferenced;
111
112   private:
113     WeakData(void);
114     WeakData(const WeakData&);
115     WeakData& operator=(const WeakData&);
116
117     /// Upcast in a class hierarchy with a virtual base class
118     template<class T>
119     static
120     typename boost::enable_if<
121       boost::is_base_of<SGVirtualWeakReferenced, T>,
122       T*
123     >::type
124     up_cast(SGWeakReferenced* ptr)
125     {
126       // First get the virtual base class, which then can be used to further
127       // upcast.
128       return dynamic_cast<T*>(static_cast<SGVirtualWeakReferenced*>(ptr));
129     }
130
131     /// Upcast in a non-virtual class hierarchy
132     template<class T>
133     static
134     typename boost::disable_if<
135       boost::is_base_of<SGVirtualWeakReferenced, T>,
136       T*
137     >::type
138     up_cast(SGWeakReferenced* ptr)
139     {
140       return static_cast<T*>(ptr);
141     }
142   };
143
144   SGSharedPtr<WeakData> mWeakData;
145
146   template<typename T>
147   friend class SGWeakPtr;
148 };
149
150 /**
151  * Base class for all reference counted SimGear objects with virtual base
152  * classes, supporting weak references.
153  *
154  * Classes derived from this one are meant to be managed with the SGSharedPtr
155  * and SGWeakPtr classes.
156  *
157  * @code{cpp}
158  *
159  * class Base1:
160  *   public virtual SGVirtualWeakReferenced
161  * {};
162  *
163  * class Base2:
164  *   public virtual SGVirtualWeakReferenced
165  * {};
166  *
167  * class Derived:
168  *   public Base1,
169  *   public Base2
170  * {};
171  *
172  * SGSharedPtr<Derived> ptr( new Derived() );
173  * SGWeakPtr<Derived> weak_ptr( ptr );
174  * SGSharedPtr<Base1> ptr1( weak_ptr.lock() );
175  * SGSharedPtr<Base2> ptr2( weak_ptr.lock() );
176  *
177  * @endcode
178  */
179 class SGVirtualWeakReferenced:
180   public SGWeakReferenced
181 {
182   public:
183     virtual ~SGVirtualWeakReferenced() {}
184 };
185
186 #ifdef _MSC_VER
187 # pragma warning(pop)
188 #endif
189
190 #endif