2014-09-26 21:53:47 +04: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-18 08:24:48 +03:00
|
|
|
#ifndef mozilla_RefPtr_h
|
|
|
|
#define mozilla_RefPtr_h
|
2014-09-26 21:53:47 +04:00
|
|
|
|
2014-12-31 19:42:05 +03:00
|
|
|
#include "mozilla/AlreadyAddRefed.h"
|
2015-07-29 03:45:58 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-12-25 23:18:38 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-09-26 21:53:47 +04:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
// template <class T> class RefPtrGetterAddRefs;
|
2014-09-26 21:53:47 +04:00
|
|
|
|
2014-11-07 19:16:54 +03:00
|
|
|
class nsCOMPtr_helper;
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
namespace mozilla {
|
|
|
|
template<class T> class OwningNonNull;
|
2016-07-21 19:54:17 +03:00
|
|
|
template<class T> class StaticRefPtr;
|
2016-02-16 02:13:00 +03:00
|
|
|
|
|
|
|
// Traditionally, RefPtr supports automatic refcounting of any pointer type
|
|
|
|
// with AddRef() and Release() methods that follow the traditional semantics.
|
|
|
|
//
|
|
|
|
// This traits class can be specialized to operate on other pointer types. For
|
|
|
|
// example, we specialize this trait for opaque FFI types that represent
|
|
|
|
// refcounted objects in Rust.
|
2016-02-17 21:16:47 +03:00
|
|
|
//
|
|
|
|
// Given the use of ConstRemovingRefPtrTraits below, U should not be a const-
|
|
|
|
// qualified type.
|
2016-02-16 02:13:00 +03:00
|
|
|
template<class U>
|
|
|
|
struct RefPtrTraits
|
|
|
|
{
|
|
|
|
static void AddRef(U* aPtr) {
|
|
|
|
aPtr->AddRef();
|
|
|
|
}
|
|
|
|
static void Release(U* aPtr) {
|
|
|
|
aPtr->Release();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2014-09-26 21:53:47 +04:00
|
|
|
template <class T>
|
2015-10-18 08:24:48 +03:00
|
|
|
class RefPtr
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
void
|
|
|
|
assign_with_AddRef(T* aRawPtr)
|
|
|
|
{
|
|
|
|
if (aRawPtr) {
|
2016-02-16 00:47:33 +03:00
|
|
|
ConstRemovingRefPtrTraits<T>::AddRef(aRawPtr);
|
2014-09-26 21:53:47 +04:00
|
|
|
}
|
|
|
|
assign_assuming_AddRef(aRawPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
assign_assuming_AddRef(T* aNewPtr)
|
|
|
|
{
|
|
|
|
T* oldPtr = mRawPtr;
|
|
|
|
mRawPtr = aNewPtr;
|
|
|
|
if (oldPtr) {
|
2016-02-16 00:47:33 +03:00
|
|
|
ConstRemovingRefPtrTraits<T>::Release(oldPtr);
|
2014-09-26 21:53:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-12-24 05:17:50 +03:00
|
|
|
T* MOZ_OWNING_REF mRawPtr;
|
2014-09-26 21:53:47 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
typedef T element_type;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
~RefPtr()
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
if (mRawPtr) {
|
2016-02-16 00:47:33 +03:00
|
|
|
ConstRemovingRefPtrTraits<T>::Release(mRawPtr);
|
2014-09-26 21:53:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructors
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr()
|
2014-09-26 21:53:47 +04:00
|
|
|
: mRawPtr(0)
|
|
|
|
// default constructor
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr(const RefPtr<T>& aSmartPtr)
|
2014-09-26 21:53:47 +04:00
|
|
|
: mRawPtr(aSmartPtr.mRawPtr)
|
|
|
|
// copy-constructor
|
|
|
|
{
|
|
|
|
if (mRawPtr) {
|
2016-02-16 00:47:33 +03:00
|
|
|
ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
|
2014-09-26 21:53:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr(RefPtr<T>&& aRefPtr)
|
2014-09-26 21:53:47 +04:00
|
|
|
: mRawPtr(aRefPtr.mRawPtr)
|
|
|
|
{
|
|
|
|
aRefPtr.mRawPtr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct from a raw pointer (of the right type)
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
MOZ_IMPLICIT RefPtr(T* aRawPtr)
|
2014-09-26 21:53:47 +04:00
|
|
|
: mRawPtr(aRawPtr)
|
|
|
|
{
|
|
|
|
if (mRawPtr) {
|
2016-02-16 00:47:33 +03:00
|
|
|
ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
|
2014-09-26 21:53:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename I>
|
2015-10-18 08:24:48 +03:00
|
|
|
MOZ_IMPLICIT RefPtr(already_AddRefed<I>& aSmartPtr)
|
2014-09-26 21:53:47 +04:00
|
|
|
: mRawPtr(aSmartPtr.take())
|
|
|
|
// construct from |already_AddRefed|
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename I>
|
2015-10-18 08:24:48 +03:00
|
|
|
MOZ_IMPLICIT RefPtr(already_AddRefed<I>&& aSmartPtr)
|
2014-09-26 21:53:47 +04:00
|
|
|
: mRawPtr(aSmartPtr.take())
|
|
|
|
// construct from |otherRefPtr.forget()|
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-08-05 20:52:06 +03:00
|
|
|
template <typename I>
|
2015-10-18 08:24:48 +03:00
|
|
|
MOZ_IMPLICIT RefPtr(const RefPtr<I>& aSmartPtr)
|
2015-08-05 20:52:06 +03:00
|
|
|
: mRawPtr(aSmartPtr.get())
|
|
|
|
// copy-construct from a smart pointer with a related pointer type
|
|
|
|
{
|
|
|
|
if (mRawPtr) {
|
2016-02-16 00:47:33 +03:00
|
|
|
ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
|
2015-08-05 20:52:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 05:32:12 +03:00
|
|
|
template <typename I>
|
2015-10-18 08:24:48 +03:00
|
|
|
MOZ_IMPLICIT RefPtr(RefPtr<I>&& aSmartPtr)
|
2015-02-10 05:32:12 +03:00
|
|
|
: mRawPtr(aSmartPtr.forget().take())
|
2015-10-18 08:24:48 +03:00
|
|
|
// construct from |Move(RefPtr<SomeSubclassOfT>)|.
|
2015-02-10 05:32:12 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
MOZ_IMPLICIT RefPtr(const nsCOMPtr_helper& aHelper);
|
2014-09-26 21:53:47 +04:00
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
// Defined in OwningNonNull.h
|
|
|
|
template<class U>
|
2015-10-18 08:24:48 +03:00
|
|
|
MOZ_IMPLICIT RefPtr(const mozilla::OwningNonNull<U>& aOther);
|
2015-04-19 15:28:50 +03:00
|
|
|
|
2016-07-21 19:54:17 +03:00
|
|
|
// Defined in StaticPtr.h
|
|
|
|
template<class U>
|
|
|
|
MOZ_IMPLICIT RefPtr(const mozilla::StaticRefPtr<U>& aOther);
|
|
|
|
|
2014-09-26 21:53:47 +04:00
|
|
|
// Assignment operators
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T>&
|
|
|
|
operator=(const RefPtr<T>& aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
// copy assignment operator
|
|
|
|
{
|
|
|
|
assign_with_AddRef(aRhs.mRawPtr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-08-05 20:52:06 +03:00
|
|
|
template <typename I>
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T>&
|
|
|
|
operator=(const RefPtr<I>& aRhs)
|
|
|
|
// assign from an RefPtr of a related pointer type
|
2015-08-05 20:52:06 +03:00
|
|
|
{
|
|
|
|
assign_with_AddRef(aRhs.get());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T>&
|
2014-09-26 21:53:47 +04:00
|
|
|
operator=(T* aRhs)
|
|
|
|
// assign from a raw pointer (of the right type)
|
|
|
|
{
|
|
|
|
assign_with_AddRef(aRhs);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename I>
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T>&
|
2014-09-26 21:53:47 +04:00
|
|
|
operator=(already_AddRefed<I>& aRhs)
|
|
|
|
// assign from |already_AddRefed|
|
|
|
|
{
|
|
|
|
assign_assuming_AddRef(aRhs.take());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename I>
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T>&
|
2014-09-26 21:53:47 +04:00
|
|
|
operator=(already_AddRefed<I> && aRhs)
|
|
|
|
// assign from |otherRefPtr.forget()|
|
|
|
|
{
|
|
|
|
assign_assuming_AddRef(aRhs.take());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T>& operator=(const nsCOMPtr_helper& aHelper);
|
2014-09-26 21:53:47 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T>&
|
|
|
|
operator=(RefPtr<T> && aRefPtr)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
assign_assuming_AddRef(aRefPtr.mRawPtr);
|
|
|
|
aRefPtr.mRawPtr = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
// Defined in OwningNonNull.h
|
|
|
|
template<class U>
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T>&
|
2015-08-05 15:28:27 +03:00
|
|
|
operator=(const mozilla::OwningNonNull<U>& aOther);
|
2015-04-19 15:28:50 +03:00
|
|
|
|
2016-07-21 19:54:17 +03:00
|
|
|
// Defined in StaticPtr.h
|
|
|
|
template<class U>
|
|
|
|
RefPtr<T>&
|
|
|
|
operator=(const mozilla::StaticRefPtr<U>& aOther);
|
|
|
|
|
2014-09-26 21:53:47 +04:00
|
|
|
// Other pointer operators
|
|
|
|
|
|
|
|
void
|
2015-10-18 08:24:48 +03:00
|
|
|
swap(RefPtr<T>& aRhs)
|
2014-09-26 21:53:47 +04: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-29 03:45:58 +03:00
|
|
|
MOZ_ASSERT(aRhs, "Null pointer passed to forget!");
|
2014-09-26 21:53:47 +04: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 15:22:48 +03:00
|
|
|
#ifdef MOZ_HAVE_REF_QUALIFIERS
|
|
|
|
&
|
|
|
|
#endif
|
2014-09-26 21:53:47 +04:00
|
|
|
/*
|
2015-10-18 08:24:48 +03:00
|
|
|
...makes an |RefPtr| act like its underlying raw pointer type whenever it
|
2014-09-26 21:53:47 +04:00
|
|
|
is used in a context where a raw pointer is expected. It is this operator
|
2015-10-18 08:24:48 +03:00
|
|
|
that makes an |RefPtr| substitutable for a raw pointer.
|
2014-09-26 21:53:47 +04:00
|
|
|
|
|
|
|
Prefer the implicit use of this operator to calling |get()|, except where
|
|
|
|
necessary to resolve ambiguity.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
2015-08-13 15:22:48 +03:00
|
|
|
#ifdef MOZ_HAVE_REF_QUALIFIERS
|
2015-10-18 08:24:48 +03:00
|
|
|
// Don't allow implicit conversion of temporary RefPtr to raw pointer,
|
2015-08-13 15:22:48 +03: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 21:53:47 +04:00
|
|
|
T*
|
2014-12-25 23:18:38 +03:00
|
|
|
operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
2015-07-29 03:45:58 +03:00
|
|
|
MOZ_ASSERT(mRawPtr != 0,
|
2015-10-18 08:24:48 +03:00
|
|
|
"You can't dereference a NULL RefPtr with operator->().");
|
2014-09-26 21:53:47 +04:00
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
2015-07-29 20:47:00 +03: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-31 04:16:00 +03:00
|
|
|
template<typename... ActualArgs>
|
|
|
|
R operator()(ActualArgs&&... aArgs)
|
2015-07-29 20:47:00 +03:00
|
|
|
{
|
2015-07-31 04:16:00 +03:00
|
|
|
return ((*mRawPtr).*mFunction)(mozilla::Forward<ActualArgs>(aArgs)...);
|
2015-07-29 20:47:00 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename R, typename... Args>
|
|
|
|
Proxy<R, Args...> operator->*(R (T::*aFptr)(Args...)) const
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
2015-07-29 03:45:58 +03:00
|
|
|
MOZ_ASSERT(mRawPtr != 0,
|
2015-10-18 08:24:48 +03:00
|
|
|
"You can't dereference a NULL RefPtr with operator->*().");
|
2015-07-29 20:47:00 +03:00
|
|
|
return Proxy<R, Args...>(get(), aFptr);
|
2014-09-26 21:53:47 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T>*
|
2014-09-26 21:53:47 +04:00
|
|
|
get_address()
|
|
|
|
// This is not intended to be used by clients. See |address_of|
|
|
|
|
// below.
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
const RefPtr<T>*
|
2014-09-26 21:53:47 +04: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-29 03:45:58 +03:00
|
|
|
MOZ_ASSERT(mRawPtr != 0,
|
2015-10-18 08:24:48 +03:00
|
|
|
"You can't dereference a NULL RefPtr with operator*().");
|
2014-09-26 21:53:47 +04:00
|
|
|
return *get();
|
|
|
|
}
|
|
|
|
|
|
|
|
T**
|
|
|
|
StartAssignment()
|
|
|
|
{
|
|
|
|
assign_assuming_AddRef(0);
|
|
|
|
return reinterpret_cast<T**>(&mRawPtr);
|
|
|
|
}
|
2015-08-05 20:52:06 +03:00
|
|
|
private:
|
2016-02-16 00:47:33 +03: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 20:52:06 +03:00
|
|
|
template<class U>
|
2016-02-16 00:47:33 +03:00
|
|
|
struct ConstRemovingRefPtrTraits
|
2015-08-05 20:52:06 +03:00
|
|
|
{
|
|
|
|
static void AddRef(U* aPtr) {
|
2016-02-16 02:13:00 +03:00
|
|
|
mozilla::RefPtrTraits<U>::AddRef(aPtr);
|
2015-08-05 20:52:06 +03:00
|
|
|
}
|
|
|
|
static void Release(U* aPtr) {
|
2016-02-16 02:13:00 +03:00
|
|
|
mozilla::RefPtrTraits<U>::Release(aPtr);
|
2015-08-05 20:52:06 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template<class U>
|
2016-02-16 00:47:33 +03:00
|
|
|
struct ConstRemovingRefPtrTraits<const U>
|
2015-08-05 20:52:06 +03:00
|
|
|
{
|
|
|
|
static void AddRef(const U* aPtr) {
|
2016-05-03 21:45:00 +03:00
|
|
|
mozilla::RefPtrTraits<U>::AddRef(const_cast<U*>(aPtr));
|
2015-08-05 20:52:06 +03:00
|
|
|
}
|
|
|
|
static void Release(const U* aPtr) {
|
2016-05-03 21:45:00 +03:00
|
|
|
mozilla::RefPtrTraits<U>::Release(const_cast<U*>(aPtr));
|
2015-08-05 20:52:06 +03:00
|
|
|
}
|
|
|
|
};
|
2014-09-26 21:53:47 +04:00
|
|
|
};
|
|
|
|
|
2014-11-07 17:13:18 +03:00
|
|
|
class nsCycleCollectionTraversalCallback;
|
|
|
|
template <typename T>
|
|
|
|
void
|
|
|
|
CycleCollectionNoteChild(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
T* aChild, const char* aName, uint32_t aFlags);
|
|
|
|
|
2014-09-26 21:53:47 +04:00
|
|
|
template <typename T>
|
|
|
|
inline void
|
2015-10-18 08:24:48 +03:00
|
|
|
ImplCycleCollectionUnlink(RefPtr<T>& aField)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
aField = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T>& aField,
|
2014-09-26 21:53:47 +04:00
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
CycleCollectionNoteChild(aCallback, aField.get(), aName, aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2015-10-18 08:24:48 +03:00
|
|
|
inline RefPtr<T>*
|
|
|
|
address_of(RefPtr<T>& aPtr)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
return aPtr.get_address();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2015-10-18 08:24:48 +03:00
|
|
|
inline const RefPtr<T>*
|
|
|
|
address_of(const RefPtr<T>& aPtr)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
return aPtr.get_address();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2015-10-18 08:24:48 +03:00
|
|
|
class RefPtrGetterAddRefs
|
2014-09-26 21:53:47 +04: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-18 08:24:48 +03:00
|
|
|
RefPtr<IFoo> fooP;
|
2014-09-26 21:53:47 +04:00
|
|
|
...->GetAddRefedPointer(getter_AddRefs(fooP))
|
|
|
|
|
|
|
|
DO NOT USE THIS TYPE DIRECTLY IN YOUR CODE. Use |getter_AddRefs()| instead.
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
When initialized with a |RefPtr|, as in the example above, it returns
|
2014-09-26 21:53:47 +04:00
|
|
|
a |void**|, a |T**|, or an |nsISupports**| as needed, that the
|
|
|
|
outer call (|GetAddRefedPointer| in this case) can fill in.
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
This type should be a nested class inside |RefPtr<T>|.
|
2014-09-26 21:53:47 +04:00
|
|
|
*/
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtrGetterAddRefs(RefPtr<T>& aSmartPtr)
|
2014-09-26 21:53:47 +04: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-18 08:24:48 +03:00
|
|
|
RefPtr<T>& mTargetSmartPtr;
|
2014-09-26 21:53:47 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2015-10-18 08:24:48 +03:00
|
|
|
inline RefPtrGetterAddRefs<T>
|
|
|
|
getter_AddRefs(RefPtr<T>& aSmartPtr)
|
2014-09-26 21:53:47 +04:00
|
|
|
/*
|
2015-10-18 08:24:48 +03:00
|
|
|
Used around a |RefPtr| when
|
|
|
|
...makes the class |RefPtrGetterAddRefs<T>| invisible.
|
2014-09-26 21:53:47 +04:00
|
|
|
*/
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
return RefPtrGetterAddRefs<T>(aSmartPtr);
|
2014-09-26 21:53:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
// Comparing two |RefPtr|s
|
2014-09-26 21:53:47 +04:00
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator==(const RefPtr<T>& aLhs, const RefPtr<U>& aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(aLhs.get()) == static_cast<const U*>(aRhs.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator!=(const RefPtr<T>& aLhs, const RefPtr<U>& aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(aLhs.get()) != static_cast<const U*>(aRhs.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
// Comparing an |RefPtr| to a raw pointer
|
2014-09-26 21:53:47 +04:00
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator==(const RefPtr<T>& aLhs, const U* aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(aLhs.get()) == static_cast<const U*>(aRhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator==(const U* aLhs, const RefPtr<T>& aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
return static_cast<const U*>(aLhs) == static_cast<const T*>(aRhs.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator!=(const RefPtr<T>& aLhs, const U* aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(aLhs.get()) != static_cast<const U*>(aRhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator!=(const U* aLhs, const RefPtr<T>& aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
return static_cast<const U*>(aLhs) != static_cast<const T*>(aRhs.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator==(const RefPtr<T>& aLhs, U* aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(aLhs.get()) == const_cast<const U*>(aRhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator==(U* aLhs, const RefPtr<T>& aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
return const_cast<const U*>(aLhs) == static_cast<const T*>(aRhs.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator!=(const RefPtr<T>& aLhs, U* aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
return static_cast<const T*>(aLhs.get()) != const_cast<const U*>(aRhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator!=(U* aLhs, const RefPtr<T>& aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
|
|
|
return const_cast<const U*>(aLhs) != static_cast<const T*>(aRhs.get());
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
// Comparing an |RefPtr| to |nullptr|
|
2014-09-26 21:53:47 +04:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator==(const RefPtr<T>& aLhs, decltype(nullptr))
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
2015-08-19 21:06:05 +03:00
|
|
|
return aLhs.get() == nullptr;
|
2014-09-26 21:53:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator==(decltype(nullptr), const RefPtr<T>& aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
2015-08-19 21:06:05 +03:00
|
|
|
return nullptr == aRhs.get();
|
2014-09-26 21:53:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator!=(const RefPtr<T>& aLhs, decltype(nullptr))
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
2015-08-19 21:06:05 +03:00
|
|
|
return aLhs.get() != nullptr;
|
2014-09-26 21:53:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline bool
|
2015-10-18 08:24:48 +03:00
|
|
|
operator!=(decltype(nullptr), const RefPtr<T>& aRhs)
|
2014-09-26 21:53:47 +04:00
|
|
|
{
|
2015-08-19 21:06:05 +03:00
|
|
|
return nullptr != aRhs.get();
|
2014-09-26 21:53:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
template <class T>
|
|
|
|
inline already_AddRefed<T>
|
2016-04-01 22:43:40 +03:00
|
|
|
do_AddRef(T* aObj)
|
2015-07-10 06:21:46 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T> ref(aObj);
|
2015-07-10 06:21:46 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2016-06-30 01:06:10 +03:00
|
|
|
template <class T>
|
|
|
|
inline already_AddRefed<T>
|
|
|
|
do_AddRef(const RefPtr<T>& aObj)
|
|
|
|
{
|
|
|
|
RefPtr<T> ref(aObj);
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2015-09-23 05:23:10 +03: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-18 08:24:48 +03:00
|
|
|
RefPtr<T> p(new T(Forward<Args>(aArgs)...));
|
2015-09-23 05:23:10 +03:00
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
#endif /* mozilla_RefPtr_h */
|