2014-09-26 10:53:47 -07: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: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
#ifndef mozilla_RefPtr_h
|
|
|
|
#define mozilla_RefPtr_h
|
2014-09-26 10:53:47 -07:00
|
|
|
|
2014-12-31 08:42:05 -08:00
|
|
|
#include "mozilla/AlreadyAddRefed.h"
|
2015-07-28 17:45:58 -07:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-12-25 12:18:38 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-09-26 10:53:47 -07:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
// template <class T> class RefPtrGetterAddRefs;
|
2014-09-26 10:53:47 -07:00
|
|
|
|
2014-11-07 08:16:54 -08:00
|
|
|
class nsCOMPtr_helper;
|
|
|
|
|
2015-04-19 05:28:50 -07:00
|
|
|
namespace mozilla {
|
|
|
|
template<class T> class OwningNonNull;
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2014-09-26 10:53:47 -07:00
|
|
|
template <class T>
|
2015-10-17 22:24:48 -07:00
|
|
|
class RefPtr
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
void
|
|
|
|
assign_with_AddRef(T* aRawPtr)
|
|
|
|
{
|
|
|
|
if (aRawPtr) {
|
2016-02-15 13:47:33 -08:00
|
|
|
ConstRemovingRefPtrTraits<T>::AddRef(aRawPtr);
|
2014-09-26 10:53:47 -07:00
|
|
|
}
|
|
|
|
assign_assuming_AddRef(aRawPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
assign_assuming_AddRef(T* aNewPtr)
|
|
|
|
{
|
|
|
|
T* oldPtr = mRawPtr;
|
|
|
|
mRawPtr = aNewPtr;
|
|
|
|
if (oldPtr) {
|
2016-02-15 13:47:33 -08:00
|
|
|
ConstRemovingRefPtrTraits<T>::Release(oldPtr);
|
2014-09-26 10:53:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-12-23 18:17:50 -08:00
|
|
|
T* MOZ_OWNING_REF mRawPtr;
|
2014-09-26 10:53:47 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
typedef T element_type;
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
~RefPtr()
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
if (mRawPtr) {
|
2016-02-15 13:47:33 -08:00
|
|
|
ConstRemovingRefPtrTraits<T>::Release(mRawPtr);
|
2014-09-26 10:53:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructors
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr()
|
2014-09-26 10:53:47 -07:00
|
|
|
: mRawPtr(0)
|
|
|
|
// default constructor
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr(const RefPtr<T>& aSmartPtr)
|
2014-09-26 10:53:47 -07:00
|
|
|
: mRawPtr(aSmartPtr.mRawPtr)
|
|
|
|
// copy-constructor
|
|
|
|
{
|
|
|
|
if (mRawPtr) {
|
2016-02-15 13:47:33 -08:00
|
|
|
ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
|
2014-09-26 10:53:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr(RefPtr<T>&& aRefPtr)
|
2014-09-26 10:53:47 -07:00
|
|
|
: mRawPtr(aRefPtr.mRawPtr)
|
|
|
|
{
|
|
|
|
aRefPtr.mRawPtr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct from a raw pointer (of the right type)
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
MOZ_IMPLICIT RefPtr(T* aRawPtr)
|
2014-09-26 10:53:47 -07:00
|
|
|
: mRawPtr(aRawPtr)
|
|
|
|
{
|
|
|
|
if (mRawPtr) {
|
2016-02-15 13:47:33 -08:00
|
|
|
ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
|
2014-09-26 10:53:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename I>
|
2015-10-17 22:24:48 -07:00
|
|
|
MOZ_IMPLICIT RefPtr(already_AddRefed<I>& aSmartPtr)
|
2014-09-26 10:53:47 -07:00
|
|
|
: mRawPtr(aSmartPtr.take())
|
|
|
|
// construct from |already_AddRefed|
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename I>
|
2015-10-17 22:24:48 -07:00
|
|
|
MOZ_IMPLICIT RefPtr(already_AddRefed<I>&& aSmartPtr)
|
2014-09-26 10:53:47 -07:00
|
|
|
: mRawPtr(aSmartPtr.take())
|
|
|
|
// construct from |otherRefPtr.forget()|
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-08-05 10:52:06 -07:00
|
|
|
template <typename I>
|
2015-10-17 22:24:48 -07:00
|
|
|
MOZ_IMPLICIT RefPtr(const RefPtr<I>& aSmartPtr)
|
2015-08-05 10:52:06 -07:00
|
|
|
: mRawPtr(aSmartPtr.get())
|
|
|
|
// copy-construct from a smart pointer with a related pointer type
|
|
|
|
{
|
|
|
|
if (mRawPtr) {
|
2016-02-15 13:47:33 -08:00
|
|
|
ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
|
2015-08-05 10:52:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 18:32:12 -08:00
|
|
|
template <typename I>
|
2015-10-17 22:24:48 -07:00
|
|
|
MOZ_IMPLICIT RefPtr(RefPtr<I>&& aSmartPtr)
|
2015-02-09 18:32:12 -08:00
|
|
|
: mRawPtr(aSmartPtr.forget().take())
|
2015-10-17 22:24:48 -07:00
|
|
|
// construct from |Move(RefPtr<SomeSubclassOfT>)|.
|
2015-02-09 18:32:12 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
MOZ_IMPLICIT RefPtr(const nsCOMPtr_helper& aHelper);
|
2014-09-26 10:53:47 -07:00
|
|
|
|
2015-04-19 05:28:50 -07:00
|
|
|
// Defined in OwningNonNull.h
|
|
|
|
template<class U>
|
2015-10-17 22:24:48 -07:00
|
|
|
MOZ_IMPLICIT RefPtr(const mozilla::OwningNonNull<U>& aOther);
|
2015-04-19 05:28:50 -07:00
|
|
|
|
2014-09-26 10:53:47 -07:00
|
|
|
// Assignment operators
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>&
|
|
|
|
operator=(const RefPtr<T>& aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
// copy assignment operator
|
|
|
|
{
|
|
|
|
assign_with_AddRef(aRhs.mRawPtr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-08-05 10:52:06 -07:00
|
|
|
template <typename I>
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>&
|
|
|
|
operator=(const RefPtr<I>& aRhs)
|
|
|
|
// assign from an RefPtr of a related pointer type
|
2015-08-05 10:52:06 -07:00
|
|
|
{
|
|
|
|
assign_with_AddRef(aRhs.get());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>&
|
2014-09-26 10:53:47 -07:00
|
|
|
operator=(T* aRhs)
|
|
|
|
// assign from a raw pointer (of the right type)
|
|
|
|
{
|
|
|
|
assign_with_AddRef(aRhs);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename I>
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>&
|
2014-09-26 10:53:47 -07:00
|
|
|
operator=(already_AddRefed<I>& aRhs)
|
|
|
|
// assign from |already_AddRefed|
|
|
|
|
{
|
|
|
|
assign_assuming_AddRef(aRhs.take());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename I>
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>&
|
2014-09-26 10:53:47 -07:00
|
|
|
operator=(already_AddRefed<I> && aRhs)
|
|
|
|
// assign from |otherRefPtr.forget()|
|
|
|
|
{
|
|
|
|
assign_assuming_AddRef(aRhs.take());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>& operator=(const nsCOMPtr_helper& aHelper);
|
2014-09-26 10:53:47 -07:00
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>&
|
|
|
|
operator=(RefPtr<T> && aRefPtr)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
assign_assuming_AddRef(aRefPtr.mRawPtr);
|
|
|
|
aRefPtr.mRawPtr = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-04-19 05:28:50 -07:00
|
|
|
// Defined in OwningNonNull.h
|
|
|
|
template<class U>
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>&
|
2015-08-05 05:28:27 -07:00
|
|
|
operator=(const mozilla::OwningNonNull<U>& aOther);
|
2015-04-19 05:28:50 -07:00
|
|
|
|
2014-09-26 10:53:47 -07:00
|
|
|
// Other pointer operators
|
|
|
|
|
|
|
|
void
|
2015-10-17 22:24:48 -07:00
|
|
|
swap(RefPtr<T>& aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
// ...exchange ownership with |aRhs|; can save a pair of refcount operations
|
|
|
|
{
|
|
|
|
T* temp = aRhs.mRawPtr;
|
|
|
|
aRhs.mRawPtr = mRawPtr;
|
|
|
|
mRawPtr = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
swap(T*& aRhs)
|
|
|
|
// ...exchange ownership with |aRhs|; can save a pair of refcount operations
|
|
|
|
{
|
|
|
|
T* temp = aRhs;
|
|
|
|
aRhs = mRawPtr;
|
|
|
|
mRawPtr = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<T>
|
|
|
|
forget()
|
|
|
|
// return the value of mRawPtr and null out mRawPtr. Useful for
|
|
|
|
// already_AddRefed return values.
|
|
|
|
{
|
|
|
|
T* temp = 0;
|
|
|
|
swap(temp);
|
|
|
|
return already_AddRefed<T>(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename I>
|
|
|
|
void
|
|
|
|
forget(I** aRhs)
|
|
|
|
// Set the target of aRhs to the value of mRawPtr and null out mRawPtr.
|
|
|
|
// Useful to avoid unnecessary AddRef/Release pairs with "out"
|
|
|
|
// parameters where aRhs bay be a T** or an I** where I is a base class
|
|
|
|
// of T.
|
|
|
|
{
|
2015-07-28 17:45:58 -07:00
|
|
|
MOZ_ASSERT(aRhs, "Null pointer passed to forget!");
|
2014-09-26 10:53:47 -07:00
|
|
|
*aRhs = mRawPtr;
|
|
|
|
mRawPtr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
T*
|
|
|
|
get() const
|
|
|
|
/*
|
|
|
|
Prefer the implicit conversion provided automatically by |operator T*() const|.
|
|
|
|
Use |get()| to resolve ambiguity or to get a castable pointer.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
return const_cast<T*>(mRawPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T*() const
|
2015-08-13 05:22:48 -07:00
|
|
|
#ifdef MOZ_HAVE_REF_QUALIFIERS
|
|
|
|
&
|
|
|
|
#endif
|
2014-09-26 10:53:47 -07:00
|
|
|
/*
|
2015-10-17 22:24:48 -07:00
|
|
|
...makes an |RefPtr| act like its underlying raw pointer type whenever it
|
2014-09-26 10:53:47 -07:00
|
|
|
is used in a context where a raw pointer is expected. It is this operator
|
2015-10-17 22:24:48 -07:00
|
|
|
that makes an |RefPtr| substitutable for a raw pointer.
|
2014-09-26 10:53:47 -07:00
|
|
|
|
|
|
|
Prefer the implicit use of this operator to calling |get()|, except where
|
|
|
|
necessary to resolve ambiguity.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
2015-08-13 05:22:48 -07:00
|
|
|
#ifdef MOZ_HAVE_REF_QUALIFIERS
|
2015-10-17 22:24:48 -07:00
|
|
|
// Don't allow implicit conversion of temporary RefPtr to raw pointer,
|
2015-08-13 05:22:48 -07:00
|
|
|
// because the refcount might be one and the pointer will immediately become
|
|
|
|
// invalid.
|
|
|
|
operator T*() const && = delete;
|
|
|
|
|
|
|
|
// These are needed to avoid the deleted operator above. XXX Why is operator!
|
|
|
|
// needed separately? Shouldn't the compiler prefer using the non-deleted
|
|
|
|
// operator bool instead of the deleted operator T*?
|
|
|
|
explicit operator bool() const { return !!mRawPtr; }
|
|
|
|
bool operator!() const { return !mRawPtr; }
|
|
|
|
#endif
|
|
|
|
|
2014-09-26 10:53:47 -07:00
|
|
|
T*
|
2014-12-25 12:18:38 -08:00
|
|
|
operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
2015-07-28 17:45:58 -07:00
|
|
|
MOZ_ASSERT(mRawPtr != 0,
|
2015-10-17 22:24:48 -07:00
|
|
|
"You can't dereference a NULL RefPtr with operator->().");
|
2014-09-26 10:53:47 -07:00
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
2015-07-29 10:47:00 -07:00
|
|
|
template <typename R, typename... Args>
|
|
|
|
class Proxy
|
|
|
|
{
|
|
|
|
typedef R (T::*member_function)(Args...);
|
|
|
|
T* mRawPtr;
|
|
|
|
member_function mFunction;
|
|
|
|
public:
|
|
|
|
Proxy(T* aRawPtr, member_function aFunction)
|
|
|
|
: mRawPtr(aRawPtr),
|
|
|
|
mFunction(aFunction)
|
|
|
|
{
|
|
|
|
}
|
2015-07-30 18:16:00 -07:00
|
|
|
template<typename... ActualArgs>
|
|
|
|
R operator()(ActualArgs&&... aArgs)
|
2015-07-29 10:47:00 -07:00
|
|
|
{
|
2015-07-30 18:16:00 -07:00
|
|
|
return ((*mRawPtr).*mFunction)(mozilla::Forward<ActualArgs>(aArgs)...);
|
2015-07-29 10:47:00 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename R, typename... Args>
|
|
|
|
Proxy<R, Args...> operator->*(R (T::*aFptr)(Args...)) const
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
2015-07-28 17:45:58 -07:00
|
|
|
MOZ_ASSERT(mRawPtr != 0,
|
2015-10-17 22:24:48 -07:00
|
|
|
"You can't dereference a NULL RefPtr with operator->*().");
|
2015-07-29 10:47:00 -07:00
|
|
|
return Proxy<R, Args...>(get(), aFptr);
|
2014-09-26 10:53:47 -07:00
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>*
|
2014-09-26 10:53:47 -07:00
|
|
|
get_address()
|
|
|
|
// This is not intended to be used by clients. See |address_of|
|
|
|
|
// below.
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
const RefPtr<T>*
|
2014-09-26 10:53:47 -07:00
|
|
|
get_address() const
|
|
|
|
// This is not intended to be used by clients. See |address_of|
|
|
|
|
// below.
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
T&
|
|
|
|
operator*() const
|
|
|
|
{
|
2015-07-28 17:45:58 -07:00
|
|
|
MOZ_ASSERT(mRawPtr != 0,
|
2015-10-17 22:24:48 -07:00
|
|
|
"You can't dereference a NULL RefPtr with operator*().");
|
2014-09-26 10:53:47 -07:00
|
|
|
return *get();
|
|
|
|
}
|
|
|
|
|
|
|
|
T**
|
|
|
|
StartAssignment()
|
|
|
|
{
|
|
|
|
assign_assuming_AddRef(0);
|
|
|
|
return reinterpret_cast<T**>(&mRawPtr);
|
|
|
|
}
|
2015-08-05 10:52:06 -07:00
|
|
|
private:
|
2015-10-12 19:29:51 -07:00
|
|
|
// Because some classes make their AddRef/Release implementations private
|
2015-10-17 22:24:48 -07:00
|
|
|
// and then friend RefPtr to make them visible, we redirect AddRefTraits's
|
|
|
|
// calls to static helper functions in RefPtr so we don't have to figure
|
2015-10-12 19:29:51 -07:00
|
|
|
// out how to make AddRefTraits visible to *those* classes.
|
|
|
|
static MOZ_ALWAYS_INLINE void
|
|
|
|
AddRefTraitsAddRefHelper(typename mozilla::RemoveConst<T>::Type* aPtr)
|
|
|
|
{
|
|
|
|
aPtr->AddRef();
|
|
|
|
}
|
|
|
|
static MOZ_ALWAYS_INLINE void
|
|
|
|
AddRefTraitsReleaseHelper(typename mozilla::RemoveConst<T>::Type* aPtr)
|
|
|
|
{
|
|
|
|
aPtr->Release();
|
|
|
|
}
|
|
|
|
|
2016-02-15 13:47:33 -08:00
|
|
|
// This helper class makes |RefPtr<const T>| possible by casting away
|
|
|
|
// the constness from the pointer when calling AddRef() and Release().
|
|
|
|
//
|
|
|
|
// This is necessary because AddRef() and Release() implementations can't
|
|
|
|
// generally expected to be const themselves (without heavy use of |mutable|
|
|
|
|
// and |const_cast| in their own implementations).
|
|
|
|
//
|
|
|
|
// This should be sound because while |RefPtr<const T>| provides a
|
|
|
|
// const view of an object, the object itself should not be const (it
|
|
|
|
// would have to be allocated as |new const T| or similar to be const).
|
2015-08-05 10:52:06 -07:00
|
|
|
template<class U>
|
2016-02-15 13:47:33 -08:00
|
|
|
struct ConstRemovingRefPtrTraits
|
2015-08-05 10:52:06 -07:00
|
|
|
{
|
|
|
|
static void AddRef(U* aPtr) {
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>::AddRefTraitsAddRefHelper(aPtr);
|
2015-08-05 10:52:06 -07:00
|
|
|
}
|
|
|
|
static void Release(U* aPtr) {
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>::AddRefTraitsReleaseHelper(aPtr);
|
2015-08-05 10:52:06 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template<class U>
|
2016-02-15 13:47:33 -08:00
|
|
|
struct ConstRemovingRefPtrTraits<const U>
|
2015-08-05 10:52:06 -07:00
|
|
|
{
|
|
|
|
static void AddRef(const U* aPtr) {
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>::AddRefTraitsAddRefHelper(const_cast<U*>(aPtr));
|
2015-08-05 10:52:06 -07:00
|
|
|
}
|
|
|
|
static void Release(const U* aPtr) {
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>::AddRefTraitsReleaseHelper(const_cast<U*>(aPtr));
|
2015-08-05 10:52:06 -07:00
|
|
|
}
|
|
|
|
};
|
2014-09-26 10:53:47 -07:00
|
|
|
};
|
|
|
|
|
2014-11-07 06:13:18 -08:00
|
|
|
class nsCycleCollectionTraversalCallback;
|
|
|
|
template <typename T>
|
|
|
|
void
|
|
|
|
CycleCollectionNoteChild(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
T* aChild, const char* aName, uint32_t aFlags);
|
|
|
|
|
2014-09-26 10:53:47 -07:00
|
|
|
template <typename T>
|
|
|
|
inline void
|
2015-10-17 22:24:48 -07:00
|
|
|
ImplCycleCollectionUnlink(RefPtr<T>& aField)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
aField = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>& aField,
|
2014-09-26 10:53:47 -07:00
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
CycleCollectionNoteChild(aCallback, aField.get(), aName, aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2015-10-17 22:24:48 -07:00
|
|
|
inline RefPtr<T>*
|
|
|
|
address_of(RefPtr<T>& aPtr)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
return aPtr.get_address();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2015-10-17 22:24:48 -07:00
|
|
|
inline const RefPtr<T>*
|
|
|
|
address_of(const RefPtr<T>& aPtr)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
return aPtr.get_address();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2015-10-17 22:24:48 -07:00
|
|
|
class RefPtrGetterAddRefs
|
2014-09-26 10:53:47 -07:00
|
|
|
/*
|
|
|
|
...
|
|
|
|
|
|
|
|
This class is designed to be used for anonymous temporary objects in the
|
|
|
|
argument list of calls that return COM interface pointers, e.g.,
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<IFoo> fooP;
|
2014-09-26 10:53:47 -07:00
|
|
|
...->GetAddRefedPointer(getter_AddRefs(fooP))
|
|
|
|
|
|
|
|
DO NOT USE THIS TYPE DIRECTLY IN YOUR CODE. Use |getter_AddRefs()| instead.
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
When initialized with a |RefPtr|, as in the example above, it returns
|
2014-09-26 10:53:47 -07:00
|
|
|
a |void**|, a |T**|, or an |nsISupports**| as needed, that the
|
|
|
|
outer call (|GetAddRefedPointer| in this case) can fill in.
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
This type should be a nested class inside |RefPtr<T>|.
|
2014-09-26 10:53:47 -07:00
|
|
|
*/
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtrGetterAddRefs(RefPtr<T>& aSmartPtr)
|
2014-09-26 10:53:47 -07:00
|
|
|
: mTargetSmartPtr(aSmartPtr)
|
|
|
|
{
|
|
|
|
// nothing else to do
|
|
|
|
}
|
|
|
|
|
|
|
|
operator void**()
|
|
|
|
{
|
|
|
|
return reinterpret_cast<void**>(mTargetSmartPtr.StartAssignment());
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T**()
|
|
|
|
{
|
|
|
|
return mTargetSmartPtr.StartAssignment();
|
|
|
|
}
|
|
|
|
|
|
|
|
T*&
|
|
|
|
operator*()
|
|
|
|
{
|
|
|
|
return *(mTargetSmartPtr.StartAssignment());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T>& mTargetSmartPtr;
|
2014-09-26 10:53:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2015-10-17 22:24:48 -07:00
|
|
|
inline RefPtrGetterAddRefs<T>
|
|
|
|
getter_AddRefs(RefPtr<T>& aSmartPtr)
|
2014-09-26 10:53:47 -07:00
|
|
|
/*
|
2015-10-17 22:24:48 -07:00
|
|
|
Used around a |RefPtr| when
|
|
|
|
...makes the class |RefPtrGetterAddRefs<T>| invisible.
|
2014-09-26 10:53:47 -07:00
|
|
|
*/
|
|
|
|
{
|
2015-10-17 22:24:48 -07:00
|
|
|
return RefPtrGetterAddRefs<T>(aSmartPtr);
|
2014-09-26 10:53:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
// Comparing two |RefPtr|s
|
2014-09-26 10:53:47 -07:00
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator==(const RefPtr<T>& aLhs, const RefPtr<U>& aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(aLhs.get()) == static_cast<const U*>(aRhs.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator!=(const RefPtr<T>& aLhs, const RefPtr<U>& aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(aLhs.get()) != static_cast<const U*>(aRhs.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
// Comparing an |RefPtr| to a raw pointer
|
2014-09-26 10:53:47 -07:00
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator==(const RefPtr<T>& aLhs, const U* aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(aLhs.get()) == static_cast<const U*>(aRhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator==(const U* aLhs, const RefPtr<T>& aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
return static_cast<const U*>(aLhs) == static_cast<const T*>(aRhs.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator!=(const RefPtr<T>& aLhs, const U* aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(aLhs.get()) != static_cast<const U*>(aRhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator!=(const U* aLhs, const RefPtr<T>& aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
return static_cast<const U*>(aLhs) != static_cast<const T*>(aRhs.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator==(const RefPtr<T>& aLhs, U* aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(aLhs.get()) == const_cast<const U*>(aRhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator==(U* aLhs, const RefPtr<T>& aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
return const_cast<const U*>(aLhs) == static_cast<const T*>(aRhs.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator!=(const RefPtr<T>& aLhs, U* aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(aLhs.get()) != const_cast<const U*>(aRhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator!=(U* aLhs, const RefPtr<T>& aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
|
|
|
return const_cast<const U*>(aLhs) != static_cast<const T*>(aRhs.get());
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
// Comparing an |RefPtr| to |nullptr|
|
2014-09-26 10:53:47 -07:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator==(const RefPtr<T>& aLhs, decltype(nullptr))
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
2015-08-19 11:06:05 -07:00
|
|
|
return aLhs.get() == nullptr;
|
2014-09-26 10:53:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator==(decltype(nullptr), const RefPtr<T>& aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
2015-08-19 11:06:05 -07:00
|
|
|
return nullptr == aRhs.get();
|
2014-09-26 10:53:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator!=(const RefPtr<T>& aLhs, decltype(nullptr))
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
2015-08-19 11:06:05 -07:00
|
|
|
return aLhs.get() != nullptr;
|
2014-09-26 10:53:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline bool
|
2015-10-17 22:24:48 -07:00
|
|
|
operator!=(decltype(nullptr), const RefPtr<T>& aRhs)
|
2014-09-26 10:53:47 -07:00
|
|
|
{
|
2015-08-19 11:06:05 -07:00
|
|
|
return nullptr != aRhs.get();
|
2014-09-26 10:53:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2015-07-09 20:21:46 -07:00
|
|
|
template <class T>
|
|
|
|
inline already_AddRefed<T>
|
|
|
|
do_AddRef(T*&& aObj)
|
|
|
|
{
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T> ref(aObj);
|
2015-07-09 20:21:46 -07:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:23:10 -07:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to be able to conveniently write things like:
|
|
|
|
*
|
|
|
|
* already_AddRefed<T>
|
|
|
|
* f(...)
|
|
|
|
* {
|
|
|
|
* return MakeAndAddRef<T>(...);
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
template<typename T, typename... Args>
|
|
|
|
already_AddRefed<T>
|
|
|
|
MakeAndAddRef(Args&&... aArgs)
|
|
|
|
{
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<T> p(new T(Forward<Args>(aArgs)...));
|
2015-09-22 19:23:10 -07:00
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
#endif /* mozilla_RefPtr_h */
|