2013-07-24 07:41:39 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-10-04 19:45:07 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-24 07:41:39 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-10-04 19:45:07 +00:00
|
|
|
|
|
|
|
/* Weak pointer functionality, implemented as a mixin for use with any class. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SupportsWeakPtr lets you have a pointer to an object 'Foo' without affecting
|
|
|
|
* its lifetime. It works by creating a single shared reference counted object
|
|
|
|
* (WeakReference) that each WeakPtr will access 'Foo' through. This lets 'Foo'
|
|
|
|
* clear the pointer in the WeakReference without having to know about all of
|
|
|
|
* the WeakPtrs to it and allows the WeakReference to live beyond the lifetime
|
|
|
|
* of 'Foo'.
|
|
|
|
*
|
2014-03-28 04:12:30 +00:00
|
|
|
* PLEASE NOTE: This weak pointer implementation is not thread-safe.
|
|
|
|
*
|
2014-02-21 02:33:49 +00:00
|
|
|
* Note that when deriving from SupportsWeakPtr you should add
|
|
|
|
* MOZ_DECLARE_REFCOUNTED_TYPENAME(ClassName) to the public section of your
|
|
|
|
* class, where ClassName is the name of your class.
|
|
|
|
*
|
2012-10-04 19:45:07 +00:00
|
|
|
* The overhead of WeakPtr is that accesses to 'Foo' becomes an additional
|
|
|
|
* dereference, and an additional heap allocated pointer sized object shared
|
|
|
|
* between all of the WeakPtrs.
|
|
|
|
*
|
|
|
|
* Example of usage:
|
|
|
|
*
|
2014-07-11 02:10:17 +00:00
|
|
|
* // To have a class C support weak pointers, inherit from
|
|
|
|
* // SupportsWeakPtr<C>.
|
2012-10-04 19:45:07 +00:00
|
|
|
* class C : public SupportsWeakPtr<C>
|
|
|
|
* {
|
2014-07-11 02:10:17 +00:00
|
|
|
* public:
|
|
|
|
* MOZ_DECLARE_REFCOUNTED_TYPENAME(C)
|
|
|
|
* int mNum;
|
|
|
|
* void act();
|
2012-10-04 19:45:07 +00:00
|
|
|
* };
|
|
|
|
*
|
2014-07-11 02:10:17 +00:00
|
|
|
* C* ptr = new C();
|
2012-10-04 19:45:07 +00:00
|
|
|
*
|
2014-07-30 19:52:05 +00:00
|
|
|
* // Get weak pointers to ptr. The first time a weak pointer
|
|
|
|
* // is obtained, a reference counted WeakReference object is created that
|
2012-10-04 19:45:07 +00:00
|
|
|
* // can live beyond the lifetime of 'ptr'. The WeakReference
|
|
|
|
* // object will be notified of 'ptr's destruction.
|
2014-07-30 19:52:05 +00:00
|
|
|
* WeakPtr<C> weak = ptr;
|
|
|
|
* WeakPtr<C> other = ptr;
|
2012-10-04 19:45:07 +00:00
|
|
|
*
|
|
|
|
* // Test a weak pointer for validity before using it.
|
|
|
|
* if (weak) {
|
2014-07-11 02:10:17 +00:00
|
|
|
* weak->mNum = 17;
|
2012-10-04 19:45:07 +00:00
|
|
|
* weak->act();
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // Destroying the underlying object clears weak pointers to it.
|
|
|
|
* delete ptr;
|
|
|
|
*
|
|
|
|
* MOZ_ASSERT(!weak, "Deleting |ptr| clears weak pointers to it.");
|
|
|
|
* MOZ_ASSERT(!other, "Deleting |ptr| clears all weak pointers to it.");
|
|
|
|
*
|
|
|
|
* WeakPtr is typesafe and may be used with any class. It is not required that
|
|
|
|
* the class be reference-counted or allocated in any particular way.
|
|
|
|
*
|
|
|
|
* The API was loosely inspired by Chromium's weak_ptr.h:
|
|
|
|
* http://src.chromium.org/svn/trunk/src/base/memory/weak_ptr.h
|
|
|
|
*/
|
|
|
|
|
2013-07-24 07:41:39 +00:00
|
|
|
#ifndef mozilla_WeakPtr_h
|
|
|
|
#define mozilla_WeakPtr_h
|
2012-10-04 19:45:07 +00:00
|
|
|
|
2014-03-09 18:36:36 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2012-10-04 19:45:07 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/NullPtr.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "mozilla/TypeTraits.h"
|
|
|
|
|
2014-03-09 18:36:36 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-10-04 19:45:07 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-07-30 19:52:02 +00:00
|
|
|
template <typename T> class WeakPtr;
|
|
|
|
template <typename T> class SupportsWeakPtr;
|
2012-10-04 19:45:07 +00:00
|
|
|
|
2013-04-19 21:59:01 +00:00
|
|
|
namespace detail {
|
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
// This can live beyond the lifetime of the class derived from
|
2014-07-30 19:52:02 +00:00
|
|
|
// SupportsWeakPtr.
|
2013-10-16 17:48:34 +00:00
|
|
|
template<class T>
|
|
|
|
class WeakReference : public ::mozilla::RefCounted<WeakReference<T> >
|
2013-04-19 21:59:01 +00:00
|
|
|
{
|
2014-07-11 02:10:17 +00:00
|
|
|
public:
|
|
|
|
explicit WeakReference(T* p) : mPtr(p) {}
|
|
|
|
|
|
|
|
T* get() const { return mPtr; }
|
2013-04-19 21:59:01 +00:00
|
|
|
|
2014-03-09 18:36:36 +00:00
|
|
|
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
2014-07-11 02:10:17 +00:00
|
|
|
const char* typeName() const
|
|
|
|
{
|
|
|
|
static char nameBuffer[1024];
|
|
|
|
const char* innerType = mPtr->typeName();
|
|
|
|
// We could do fancier length checks at runtime, but innerType is
|
|
|
|
// controlled by us so we can ensure that this never causes a buffer
|
|
|
|
// overflow by this assertion.
|
|
|
|
MOZ_ASSERT(strlen(innerType) + sizeof("WeakReference<>") <
|
|
|
|
ArrayLength(nameBuffer),
|
|
|
|
"Exceedingly large type name");
|
|
|
|
snprintf(nameBuffer, ArrayLength(nameBuffer), "WeakReference<%s>",
|
|
|
|
innerType);
|
|
|
|
// This is usually not OK, but here we are returning a pointer to a static
|
|
|
|
// buffer which will immediately be used by the caller.
|
|
|
|
return nameBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t typeSize() const { return sizeof(*this); }
|
2014-03-09 18:36:36 +00:00
|
|
|
#undef snprintf
|
|
|
|
#endif
|
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
private:
|
2014-07-30 19:52:04 +00:00
|
|
|
friend class mozilla::SupportsWeakPtr<T>;
|
2014-07-11 02:10:17 +00:00
|
|
|
|
|
|
|
void detach() { mPtr = nullptr; }
|
|
|
|
|
|
|
|
T* mPtr;
|
2013-04-19 21:59:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2014-07-30 19:52:01 +00:00
|
|
|
template <typename T>
|
2014-07-30 19:52:02 +00:00
|
|
|
class SupportsWeakPtr
|
2012-10-04 19:45:07 +00:00
|
|
|
{
|
2014-07-11 02:10:17 +00:00
|
|
|
protected:
|
2014-07-30 19:52:02 +00:00
|
|
|
~SupportsWeakPtr()
|
2014-07-11 02:10:17 +00:00
|
|
|
{
|
2014-07-30 19:52:02 +00:00
|
|
|
static_assert(IsBaseOf<SupportsWeakPtr<T>, T>::value,
|
|
|
|
"T must derive from SupportsWeakPtr<T>");
|
2014-07-30 19:52:04 +00:00
|
|
|
if (mSelfReferencingWeakPtr) {
|
|
|
|
mSelfReferencingWeakPtr.mRef->detach();
|
2012-10-04 19:45:07 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
}
|
2012-10-04 19:45:07 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
private:
|
2014-07-30 19:52:04 +00:00
|
|
|
const WeakPtr<T>& SelfReferencingWeakPtr()
|
|
|
|
{
|
|
|
|
if (!mSelfReferencingWeakPtr) {
|
|
|
|
mSelfReferencingWeakPtr.mRef = new detail::WeakReference<T>(static_cast<T*>(this));
|
|
|
|
}
|
|
|
|
return mSelfReferencingWeakPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WeakPtr<const T>& SelfReferencingWeakPtr() const
|
|
|
|
{
|
|
|
|
const WeakPtr<T>& p = const_cast<SupportsWeakPtr*>(this)->SelfReferencingWeakPtr();
|
|
|
|
return reinterpret_cast<const WeakPtr<const T>&>(p);
|
|
|
|
}
|
|
|
|
|
2014-07-30 19:52:02 +00:00
|
|
|
friend class WeakPtr<T>;
|
2014-07-30 19:52:04 +00:00
|
|
|
friend class WeakPtr<const T>;
|
2012-10-04 19:45:07 +00:00
|
|
|
|
2014-07-30 19:52:04 +00:00
|
|
|
WeakPtr<T> mSelfReferencingWeakPtr;
|
2012-10-04 19:45:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2014-07-30 19:52:02 +00:00
|
|
|
class WeakPtr
|
2012-10-04 19:45:07 +00:00
|
|
|
{
|
2014-07-30 19:52:04 +00:00
|
|
|
typedef detail::WeakReference<T> WeakReference;
|
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
public:
|
2014-07-30 19:52:04 +00:00
|
|
|
WeakPtr& operator=(const WeakPtr& aOther)
|
|
|
|
{
|
|
|
|
mRef = aOther.mRef;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-07-30 19:52:03 +00:00
|
|
|
WeakPtr(const WeakPtr& aOther)
|
2014-07-30 19:52:04 +00:00
|
|
|
{
|
|
|
|
*this = aOther;
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakPtr& operator=(T* aOther)
|
|
|
|
{
|
|
|
|
return *this = aOther->SelfReferencingWeakPtr();
|
|
|
|
}
|
|
|
|
|
2014-08-05 13:21:12 +00:00
|
|
|
MOZ_IMPLICIT WeakPtr(T* aOther)
|
2014-07-30 19:52:04 +00:00
|
|
|
{
|
|
|
|
*this = aOther;
|
|
|
|
}
|
2012-10-04 19:45:07 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
// Ensure that mRef is dereferenceable in the uninitialized state.
|
2014-07-30 19:52:04 +00:00
|
|
|
WeakPtr() : mRef(new WeakReference(nullptr)) {}
|
2012-10-04 19:45:07 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
operator T*() const { return mRef->get(); }
|
|
|
|
T& operator*() const { return *mRef->get(); }
|
2012-10-04 19:45:07 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
T* operator->() const { return mRef->get(); }
|
|
|
|
|
|
|
|
T* get() const { return mRef->get(); }
|
2012-10-12 22:17:58 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
private:
|
2014-07-30 19:52:02 +00:00
|
|
|
friend class SupportsWeakPtr<T>;
|
2012-10-04 19:45:07 +00:00
|
|
|
|
2014-07-30 19:52:04 +00:00
|
|
|
explicit WeakPtr(const RefPtr<WeakReference>& aOther) : mRef(aOther) {}
|
2012-10-04 19:45:07 +00:00
|
|
|
|
2014-07-30 19:52:04 +00:00
|
|
|
RefPtr<WeakReference> mRef;
|
2013-04-19 21:59:01 +00:00
|
|
|
};
|
|
|
|
|
2012-10-04 19:45:07 +00:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-24 07:41:39 +00:00
|
|
|
#endif /* mozilla_WeakPtr_h */
|