/usr/include/telepathy-qt5/TelepathyQt/shared-ptr.h is in libtelepathy-qt5-dev 0.9.3-0ubuntu13.
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | /**
* This file is part of TelepathyQt
*
* @copyright Copyright (C) 2009-2011 Collabora Ltd. <http://www.collabora.co.uk/>
* @copyright Copyright (C) 2009-2011 Nokia Corporation
* @license LGPL 2.1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _TelepathyQt_shared_ptr_h_HEADER_GUARD_
#define _TelepathyQt_shared_ptr_h_HEADER_GUARD_
#ifndef IN_TP_QT_HEADER
#error IN_TP_QT_HEADER
#endif
#include <TelepathyQt/Global>
#include <QHash>
#include <QObject>
namespace Tp
{
class RefCounted;
template <class T> class SharedPtr;
template <class T> class WeakPtr;
class TP_QT_EXPORT RefCounted
{
Q_DISABLE_COPY(RefCounted)
class SharedCount
{
Q_DISABLE_COPY(SharedCount)
public:
SharedCount(RefCounted *d)
: d(d), strongref(0), weakref(0)
{
}
private:
template <class T> friend class SharedPtr;
template <class T> friend class WeakPtr;
friend class RefCounted;
RefCounted *d;
mutable QAtomicInt strongref;
mutable QAtomicInt weakref;
};
public:
inline RefCounted() : sc(new SharedCount(this))
{
sc->weakref.ref();
}
inline virtual ~RefCounted()
{
sc->d = 0;
if (!sc->weakref.deref()) {
delete sc;
}
}
private:
template <class T> friend class SharedPtr;
template <class T> friend class WeakPtr;
// TODO: Remove when Conn.I.ContactList, etc becomes mandatory. This is required to circumvent
// a reference cycle when using contact list channels, due to the fact that Channels hold
// strong references to their parent Connection, but not needed when using
// Conn.I.ContactList and friends.
friend class ContactManager;
inline void ref() const { sc->strongref.ref(); }
inline bool deref() const { return sc->strongref.deref(); }
SharedCount *sc;
};
template <class T>
class SharedPtr
{
typedef bool (SharedPtr<T>::*UnspecifiedBoolType)() const;
public:
inline SharedPtr() : d(0) { }
explicit inline SharedPtr(T *d) : d(d) { if (d) { d->ref(); } }
template <typename Subclass>
inline SharedPtr(const SharedPtr<Subclass> &o) : d(o.data()) { if (d) { d->ref(); } }
inline SharedPtr(const SharedPtr<T> &o) : d(o.d) { if (d) { d->ref(); } }
explicit inline SharedPtr(const WeakPtr<T> &o)
{
RefCounted::SharedCount *sc = o.sc;
if (sc) {
// increase the strongref, but never up from zero
// or less (negative is used on untracked objects)
register int tmp = sc->strongref.fetchAndAddOrdered(0);
while (tmp > 0) {
// try to increment from "tmp" to "tmp + 1"
if (sc->strongref.testAndSetRelaxed(tmp, tmp + 1)) {
// succeeded
break;
}
// failed, try again
tmp = sc->strongref.fetchAndAddOrdered(0);
}
if (tmp > 0) {
d = dynamic_cast<T*>(sc->d);
Q_ASSERT(d != NULL);
} else {
d = 0;
}
} else {
d = 0;
}
}
inline ~SharedPtr()
{
if (d && !d->deref()) {
T *saved = d;
d = 0;
delete saved;
}
}
inline void reset()
{
SharedPtr<T>().swap(*this);
}
inline T *data() const { return d; }
inline const T *constData() const { return d; }
inline T *operator->() { return d; }
inline T *operator->() const { return d; }
inline bool isNull() const { return !d; }
inline bool operator!() const { return isNull(); }
operator UnspecifiedBoolType() const { return !isNull() ? &SharedPtr<T>::operator! : 0; }
inline bool operator==(const SharedPtr<T> &o) const { return d == o.d; }
inline bool operator!=(const SharedPtr<T> &o) const { return d != o.d; }
inline bool operator==(const T *ptr) const { return d == ptr; }
inline bool operator!=(const T *ptr) const { return d != ptr; }
inline SharedPtr<T> &operator=(const SharedPtr<T> &o)
{
SharedPtr<T>(o).swap(*this);
return *this;
}
inline void swap(SharedPtr<T> &o)
{
T *tmp = d;
d = o.d;
o.d = tmp;
}
template <class X>
static inline SharedPtr<T> staticCast(const SharedPtr<X> &src)
{
return SharedPtr<T>(static_cast<T*>(src.data()));
}
template <class X>
static inline SharedPtr<T> dynamicCast(const SharedPtr<X> &src)
{
return SharedPtr<T>(dynamic_cast<T*>(src.data()));
}
template <class X>
static inline SharedPtr<T> constCast(const SharedPtr<X> &src)
{
return SharedPtr<T>(const_cast<T*>(src.data()));
}
template <class X>
static inline SharedPtr<T> qObjectCast(const SharedPtr<X> &src)
{
return SharedPtr<T>(qobject_cast<T*>(src.data()));
}
private:
friend class WeakPtr<T>;
T *d;
};
template<typename T>
inline uint qHash(const SharedPtr<T> &ptr)
{
return QT_PREPEND_NAMESPACE(qHash<T>(ptr.data()));
}
template<typename T> inline uint qHash(const WeakPtr<T> &ptr);
template <class T>
class WeakPtr
{
typedef bool (WeakPtr<T>::*UnspecifiedBoolType)() const;
public:
inline WeakPtr() : sc(0) { }
explicit inline WeakPtr(T *d)
{
if (d) {
sc = d->sc;
sc->weakref.ref();
} else {
sc = 0;
}
}
inline WeakPtr(const WeakPtr<T> &o) : sc(o.sc) { if (sc) { sc->weakref.ref(); } }
inline WeakPtr(const SharedPtr<T> &o)
{
if (o.d) {
sc = o.d->sc;
sc->weakref.ref();
} else {
sc = 0;
}
}
inline ~WeakPtr()
{
if (sc && !sc->weakref.deref()) {
delete sc;
}
}
inline bool isNull() const { return !sc || sc->strongref.fetchAndAddOrdered(0) <= 0; }
inline bool operator!() const { return isNull(); }
operator UnspecifiedBoolType() const { return !isNull() ? &WeakPtr<T>::operator! : 0; }
inline WeakPtr<T> &operator=(const WeakPtr<T> &o)
{
WeakPtr<T>(o).swap(*this);
return *this;
}
inline WeakPtr<T> &operator=(const SharedPtr<T> &o)
{
WeakPtr<T>(o).swap(*this);
return *this;
}
inline void swap(WeakPtr<T> &o)
{
RefCounted::SharedCount *tmp = sc;
sc = o.sc;
o.sc = tmp;
}
SharedPtr<T> toStrongRef() const { return SharedPtr<T>(*this); }
private:
friend class SharedPtr<T>;
friend uint qHash<T>(const WeakPtr<T> &ptr);
RefCounted::SharedCount *sc;
};
template<typename T>
inline uint qHash(const WeakPtr<T> &ptr)
{
T *actualPtr = ptr.sc ? ptr.sc.d : 0;
return QT_PREPEND_NAMESPACE(qHash<T>(actualPtr));
}
} // Tp
#endif
|