]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGWeakReferenced.hxx
Fix PagedLOD for random objects.
[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     /// Upcast in a non-virtual class hierarchy
127     template<class T>
128     static
129     typename boost::disable_if<
130       boost::is_base_of<SGVirtualWeakReferenced, T>,
131       T*
132     >::type
133     up_cast(SGWeakReferenced* ptr)
134     {
135       return static_cast<T*>(ptr);
136     }
137   };
138
139   SGSharedPtr<WeakData> mWeakData;
140
141   template<typename T>
142   friend class SGWeakPtr;
143 };
144
145 /**
146  * Base class for all reference counted SimGear objects with virtual base
147  * classes, supporting weak references.
148  *
149  * Classes derived from this one are meant to be managed with the SGSharedPtr
150  * and SGWeakPtr classes.
151  *
152  * @code{cpp}
153  *
154  * class Base1:
155  *   public virtual SGVirtualWeakReferenced
156  * {};
157  *
158  * class Base2:
159  *   public virtual SGVirtualWeakReferenced
160  * {};
161  *
162  * class Derived:
163  *   public Base1,
164  *   public Base2
165  * {};
166  *
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() );
171  *
172  * @endcode
173  */
174 class SGVirtualWeakReferenced:
175   public SGWeakReferenced
176 {
177   public:
178     virtual ~SGVirtualWeakReferenced() {}
179 };
180
181 /// Upcast in a class hierarchy with a virtual base class
182 // We (clang) need the definition of SGVirtualWeakReferenced for the static_cast
183 template<class T>
184 typename boost::enable_if<
185   boost::is_base_of<SGVirtualWeakReferenced, T>,
186   T*
187 >::type
188 SGWeakReferenced::WeakData::up_cast(SGWeakReferenced* ptr)
189 {
190   // First get the virtual base class, which then can be used to further
191   // upcast.
192   return dynamic_cast<T*>(static_cast<SGVirtualWeakReferenced*>(ptr));
193 }
194
195 #ifdef _MSC_VER
196 # pragma warning(pop)
197 #endif
198
199 #endif