]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGWeakReferenced.hxx
Working 'noshadow' animation
[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 template<typename T>
25 class SGWeakPtr;
26
27 class SGWeakReferenced {
28 public:
29   /// The object backref and the reference count for this object need to be
30   /// there in any case. Also these are per object and shall not be copied nor
31   /// assigned.
32   /// The reference count for this object is stored in a secondary object that
33   /// is shared with all weak pointers to this current object. This way we
34   /// have an atomic decision using the reference count of this current object
35   /// if the backref is still valid. At the time where the atomic count is
36   /// equal to zero the object is considered dead.
37   SGWeakReferenced(void) :
38     mWeakData(new WeakData(this))
39   {}
40   SGWeakReferenced(const SGWeakReferenced& weakReferenced) :
41     mWeakData(new WeakData(this))
42   {}
43   ~SGWeakReferenced(void)
44   { mWeakData->mWeakReferenced = 0; }
45
46   /// Do not copy the weak backward references ...
47   SGWeakReferenced& operator=(const SGWeakReferenced&)
48   { return *this; }
49
50   /// The usual operations on weak pointers.
51   /// The interface should stay the same then what we have in Referenced.
52   static unsigned get(const SGWeakReferenced* ref)
53   { if (ref) return ++(ref->mWeakData->mRefcount); else return 0u; }
54   static unsigned put(const SGWeakReferenced* ref)
55   { if (ref) return --(ref->mWeakData->mRefcount); else return ~0u; }
56   static unsigned count(const SGWeakReferenced* ref)
57   { if (ref) return ref->mWeakData->mRefcount; else return 0u; }
58
59 private:
60   /// Support for weak references, not increasing the reference count
61   /// that is done through that small helper class which holds an uncounted
62   /// reference which is zeroed out on destruction of the current object
63   class WeakData : public SGReferenced {
64   public:
65     WeakData(SGWeakReferenced* weakReferenced) :
66       mRefcount(0u),
67       mWeakReferenced(weakReferenced)
68     { }
69
70     template<typename T>
71     T* getPointer()
72     {
73       // Try to increment the reference count if the count is greater
74       // then zero. Since it should only be incremented iff it is nonzero, we
75       // need to check that value and try to do an atomic test and set. If this
76       // fails, try again. The usual lockless algorithm ...
77       unsigned count;
78       do {
79         count = mRefcount;
80         if (count == 0)
81           return 0;
82       } while (!mRefcount.compareAndExchange(count, count + 1));
83       // We know that as long as the refcount is not zero, the pointer still
84       // points to valid data. So it is safe to work on it.
85       return static_cast<T*>(mWeakReferenced);
86     }
87
88     SGAtomic mRefcount;
89     SGWeakReferenced* mWeakReferenced;
90
91   private:
92     WeakData(void);
93     WeakData(const WeakData&);
94     WeakData& operator=(const WeakData&);
95   };
96
97   SGSharedPtr<WeakData> mWeakData;
98
99   template<typename T>
100   friend class SGWeakPtr;
101 };
102
103 #endif