]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGWeakReferenced.hxx
Silence MSVC
[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 #ifdef _MSC_VER
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)
29 #endif
30
31 template<typename T>
32 class SGWeakPtr;
33
34 class SGWeakReferenced {
35 public:
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
38   /// assigned.
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))
46   {}
47   SGWeakReferenced(const SGWeakReferenced& weakReferenced) :
48     mWeakData(new WeakData(this))
49   {}
50   ~SGWeakReferenced(void)
51   { mWeakData->mWeakReferenced = 0; }
52
53   /// Do not copy the weak backward references ...
54   SGWeakReferenced& operator=(const SGWeakReferenced&)
55   { return *this; }
56
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; }
65
66 private:
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 {
71   public:
72     WeakData(SGWeakReferenced* weakReferenced) :
73       mRefcount(0u),
74       mWeakReferenced(weakReferenced)
75     { }
76
77     template<typename T>
78     T* getPointer()
79     {
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 ...
84       unsigned count;
85       do {
86         count = mRefcount;
87         if (count == 0)
88           return 0;
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);
93     }
94
95     SGAtomic mRefcount;
96     SGWeakReferenced* mWeakReferenced;
97
98   private:
99     WeakData(void);
100     WeakData(const WeakData&);
101     WeakData& operator=(const WeakData&);
102   };
103
104   SGSharedPtr<WeakData> mWeakData;
105
106   template<typename T>
107   friend class SGWeakPtr;
108 };
109
110 #ifdef _MSC_VER
111 # pragma warning(pop)
112 #endif
113
114 #endif