Bug 1287623, part 3 - Add more methods involving StaticRefPtr. r=froydnj

This patch adds a number of standard conversions to and from RefPtr<>
and already_AddRefed<>.
This commit is contained in:
Andrew McCreight 2016-07-20 15:19:00 -07:00
parent 07f2ce5ee2
commit 155e619376
2 changed files with 45 additions and 0 deletions

View File

@ -19,6 +19,7 @@ class nsCOMPtr_helper;
namespace mozilla { namespace mozilla {
template<class T> class OwningNonNull; template<class T> class OwningNonNull;
template<class T> class StaticRefPtr;
// Traditionally, RefPtr supports automatic refcounting of any pointer type // Traditionally, RefPtr supports automatic refcounting of any pointer type
// with AddRef() and Release() methods that follow the traditional semantics. // with AddRef() and Release() methods that follow the traditional semantics.
@ -148,6 +149,10 @@ public:
template<class U> template<class U>
MOZ_IMPLICIT RefPtr(const mozilla::OwningNonNull<U>& aOther); MOZ_IMPLICIT RefPtr(const mozilla::OwningNonNull<U>& aOther);
// Defined in StaticPtr.h
template<class U>
MOZ_IMPLICIT RefPtr(const mozilla::StaticRefPtr<U>& aOther);
// Assignment operators // Assignment operators
RefPtr<T>& RefPtr<T>&
@ -208,6 +213,11 @@ public:
RefPtr<T>& RefPtr<T>&
operator=(const mozilla::OwningNonNull<U>& aOther); operator=(const mozilla::OwningNonNull<U>& aOther);
// Defined in StaticPtr.h
template<class U>
RefPtr<T>&
operator=(const mozilla::StaticRefPtr<U>& aOther);
// Other pointer operators // Other pointer operators
void void

View File

@ -7,8 +7,10 @@
#ifndef mozilla_StaticPtr_h #ifndef mozilla_StaticPtr_h
#define mozilla_StaticPtr_h #define mozilla_StaticPtr_h
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/Assertions.h" #include "mozilla/Assertions.h"
#include "mozilla/Attributes.h" #include "mozilla/Attributes.h"
#include "mozilla/RefPtr.h"
namespace mozilla { namespace mozilla {
@ -110,6 +112,26 @@ public:
return (this = aRhs.mRawPtr); return (this = aRhs.mRawPtr);
} }
StaticRefPtr<T>& operator=(already_AddRefed<T>& aRhs)
{
AssignAssumingAddRef(aRhs.take());
return *this;
}
StaticRefPtr<T>& operator=(already_AddRefed<T>&& aRhs)
{
AssignAssumingAddRef(aRhs.take());
return *this;
}
already_AddRefed<T>
forget()
{
T* temp = mRawPtr;
mRawPtr = nullptr;
return already_AddRefed<T>(temp);
}
T* get() const { return mRawPtr; } T* get() const { return mRawPtr; }
operator T*() const { return get(); } operator T*() const { return get(); }
@ -232,4 +254,17 @@ REFLEXIVE_EQUALITY_OPERATORS(const StaticRefPtr<T>&, StaticPtr_internal::Zero*,
} // namespace mozilla } // namespace mozilla
// Declared in mozilla/RefPtr.h
template<class T> template<class U>
RefPtr<T>::RefPtr(const mozilla::StaticRefPtr<U>& aOther)
: RefPtr(aOther.get())
{}
template<class T> template<class U>
RefPtr<T>&
RefPtr<T>::operator=(const mozilla::StaticRefPtr<U>& aOther)
{
return operator=(aOther.get());
}
#endif #endif