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