/usr/include/simgear/structure/SGWeakReferenced.hxx is in libsimgear-dev 3.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | // Copyright (C) 2004-2009 Mathias Froehlich - Mathias.Froehlich@web.de
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#ifndef SGWeakReferenced_HXX
#define SGWeakReferenced_HXX
#include "SGReferenced.hxx"
#include "SGSharedPtr.hxx"
#ifdef _MSC_VER
# pragma warning(push)
// C4355: 'this' : used in base member initializer list
// Tell MSVC we know what we do and really want to do it this way.
# pragma warning(disable: 4355)
#endif
template<typename T>
class SGWeakPtr;
class SGWeakReferenced {
public:
/// The object backref and the reference count for this object need to be
/// there in any case. Also these are per object and shall not be copied nor
/// assigned.
/// The reference count for this object is stored in a secondary object that
/// is shared with all weak pointers to this current object. This way we
/// have an atomic decision using the reference count of this current object
/// if the backref is still valid. At the time where the atomic count is
/// equal to zero the object is considered dead.
SGWeakReferenced(void) :
mWeakData(new WeakData(this))
{}
SGWeakReferenced(const SGWeakReferenced& weakReferenced) :
mWeakData(new WeakData(this))
{}
~SGWeakReferenced(void)
{ mWeakData->mWeakReferenced = 0; }
/// Do not copy the weak backward references ...
SGWeakReferenced& operator=(const SGWeakReferenced&)
{ return *this; }
/// The usual operations on weak pointers.
/// The interface should stay the same then what we have in Referenced.
static unsigned get(const SGWeakReferenced* ref)
{ if (ref) return ++(ref->mWeakData->mRefcount); else return 0u; }
static unsigned put(const SGWeakReferenced* ref)
{ if (ref) return --(ref->mWeakData->mRefcount); else return ~0u; }
static unsigned count(const SGWeakReferenced* ref)
{ if (ref) return ref->mWeakData->mRefcount; else return 0u; }
private:
/// Support for weak references, not increasing the reference count
/// that is done through that small helper class which holds an uncounted
/// reference which is zeroed out on destruction of the current object
class WeakData : public SGReferenced {
public:
WeakData(SGWeakReferenced* weakReferenced) :
mRefcount(0u),
mWeakReferenced(weakReferenced)
{ }
template<typename T>
T* getPointer()
{
// Try to increment the reference count if the count is greater
// then zero. Since it should only be incremented iff it is nonzero, we
// need to check that value and try to do an atomic test and set. If this
// fails, try again. The usual lockless algorithm ...
unsigned count;
do {
count = mRefcount;
if (count == 0)
return 0;
} while (!mRefcount.compareAndExchange(count, count + 1));
// We know that as long as the refcount is not zero, the pointer still
// points to valid data. So it is safe to work on it.
return static_cast<T*>(mWeakReferenced);
}
SGAtomic mRefcount;
SGWeakReferenced* mWeakReferenced;
private:
WeakData(void);
WeakData(const WeakData&);
WeakData& operator=(const WeakData&);
};
SGSharedPtr<WeakData> mWeakData;
template<typename T>
friend class SGWeakPtr;
};
#ifdef _MSC_VER
# pragma warning(pop)
#endif
#endif
|