2013-09-10 07:03:37 +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/. */
|
|
|
|
|
|
|
|
/* A class for non-null strong pointers to reference-counted objects. */
|
|
|
|
|
2015-08-05 12:28:27 +00:00
|
|
|
#ifndef mozilla_OwningNonNull_h
|
|
|
|
#define mozilla_OwningNonNull_h
|
2013-09-10 07:03:37 +00:00
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
2014-08-01 07:19:00 +00:00
|
|
|
#include "nsCycleCollectionNoteChild.h"
|
2013-09-10 07:03:37 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class OwningNonNull
|
|
|
|
{
|
|
|
|
public:
|
2015-04-19 12:28:49 +00:00
|
|
|
OwningNonNull() {}
|
|
|
|
|
|
|
|
MOZ_IMPLICIT OwningNonNull(T& aValue)
|
|
|
|
{
|
|
|
|
init(&aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class U>
|
|
|
|
MOZ_IMPLICIT OwningNonNull(already_AddRefed<U>&& aValue)
|
|
|
|
{
|
2015-04-24 11:27:34 +00:00
|
|
|
init(aValue);
|
2015-04-19 12:28:49 +00:00
|
|
|
}
|
2013-09-10 07:03:37 +00:00
|
|
|
|
2016-05-01 13:15:39 +00:00
|
|
|
template<class U>
|
|
|
|
MOZ_IMPLICIT OwningNonNull(const OwningNonNull<U>& aValue)
|
|
|
|
{
|
|
|
|
init(aValue);
|
|
|
|
}
|
|
|
|
|
2014-06-23 20:03:57 +00:00
|
|
|
// This is no worse than get() in terms of const handling.
|
|
|
|
operator T&() const
|
2013-09-10 07:03:37 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mInited);
|
|
|
|
MOZ_ASSERT(mPtr, "OwningNonNull<T> was set to null");
|
|
|
|
return *mPtr;
|
|
|
|
}
|
|
|
|
|
2014-06-23 20:03:57 +00:00
|
|
|
operator T*() const
|
2013-12-20 08:51:03 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mInited);
|
|
|
|
MOZ_ASSERT(mPtr, "OwningNonNull<T> was set to null");
|
|
|
|
return mPtr;
|
|
|
|
}
|
|
|
|
|
2015-04-19 12:28:49 +00:00
|
|
|
// Conversion to bool is always true, so delete to catch errors
|
|
|
|
explicit operator bool() const = delete;
|
|
|
|
|
|
|
|
T*
|
|
|
|
operator->() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mInited);
|
|
|
|
MOZ_ASSERT(mPtr, "OwningNonNull<T> was set to null");
|
|
|
|
return mPtr;
|
|
|
|
}
|
|
|
|
|
2015-04-19 12:28:50 +00:00
|
|
|
OwningNonNull<T>&
|
|
|
|
operator=(T* aValue)
|
2013-09-10 07:03:37 +00:00
|
|
|
{
|
|
|
|
init(aValue);
|
2015-04-19 12:28:50 +00:00
|
|
|
return *this;
|
2013-09-10 07:03:37 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 12:28:50 +00:00
|
|
|
OwningNonNull<T>&
|
|
|
|
operator=(T& aValue)
|
2014-06-05 21:16:54 +00:00
|
|
|
{
|
|
|
|
init(&aValue);
|
2015-04-19 12:28:50 +00:00
|
|
|
return *this;
|
2014-06-05 21:16:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 13:15:39 +00:00
|
|
|
template<class U>
|
|
|
|
OwningNonNull<T>&
|
|
|
|
operator=(already_AddRefed<U>&& aValue)
|
|
|
|
{
|
|
|
|
init(aValue);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class U>
|
2015-04-19 12:28:50 +00:00
|
|
|
OwningNonNull<T>&
|
2016-05-01 13:15:39 +00:00
|
|
|
operator=(const OwningNonNull<U>& aValue)
|
2013-09-10 07:03:37 +00:00
|
|
|
{
|
|
|
|
init(aValue);
|
2015-04-19 12:28:50 +00:00
|
|
|
return *this;
|
2013-09-10 07:03:37 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 12:28:50 +00:00
|
|
|
// Don't allow assigning nullptr, it makes no sense
|
|
|
|
void operator=(decltype(nullptr)) = delete;
|
|
|
|
|
2013-09-10 07:03:37 +00:00
|
|
|
already_AddRefed<T> forget()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInited = false;
|
|
|
|
#endif
|
|
|
|
return mPtr.forget();
|
|
|
|
}
|
|
|
|
|
2016-05-01 13:15:39 +00:00
|
|
|
template<class U>
|
|
|
|
void
|
|
|
|
forget(U** aOther)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInited = false;
|
|
|
|
#endif
|
|
|
|
mPtr.forget(aOther);
|
|
|
|
}
|
|
|
|
|
2013-09-10 07:03:37 +00:00
|
|
|
// Make us work with smart pointer helpers that expect a get().
|
|
|
|
T* get() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mInited);
|
|
|
|
MOZ_ASSERT(mPtr);
|
|
|
|
return mPtr;
|
|
|
|
}
|
|
|
|
|
2015-04-19 12:28:49 +00:00
|
|
|
template<typename U>
|
|
|
|
void swap(U& aOther)
|
|
|
|
{
|
|
|
|
mPtr.swap(aOther);
|
2016-05-18 16:23:35 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
mInited = mPtr;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have some consumers who want to check whether we're inited in non-debug
|
|
|
|
// builds as well. Luckily, we have the invariant that we're inited precisely
|
|
|
|
// when mPtr is non-null.
|
|
|
|
bool isInitialized() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!!mPtr == mInited, "mInited out of sync with mPtr?");
|
|
|
|
return mPtr;
|
2015-04-19 12:28:49 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 07:03:37 +00:00
|
|
|
protected:
|
|
|
|
template<typename U>
|
2015-04-24 11:27:34 +00:00
|
|
|
void init(U&& aValue)
|
2013-09-10 07:03:37 +00:00
|
|
|
{
|
|
|
|
mPtr = aValue;
|
|
|
|
MOZ_ASSERT(mPtr);
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInited = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<T> mPtr;
|
2013-09-10 07:03:37 +00:00
|
|
|
#ifdef DEBUG
|
2015-04-19 12:28:49 +00:00
|
|
|
bool mInited = false;
|
2013-09-10 07:03:37 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2014-08-01 07:19:00 +00:00
|
|
|
template <typename T>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
OwningNonNull<T>& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
CycleCollectionNoteChild(aCallback, aField.get(), aName, aFlags);
|
|
|
|
}
|
|
|
|
|
2013-09-10 07:03:37 +00:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-04-19 12:28:50 +00:00
|
|
|
// Declared in nsCOMPtr.h
|
|
|
|
template<class T> template<class U>
|
2015-08-05 12:28:27 +00:00
|
|
|
nsCOMPtr<T>::nsCOMPtr(const mozilla::OwningNonNull<U>& aOther)
|
2015-04-19 12:28:50 +00:00
|
|
|
: nsCOMPtr(aOther.get())
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class T> template<class U>
|
|
|
|
nsCOMPtr<T>&
|
2015-08-05 12:28:27 +00:00
|
|
|
nsCOMPtr<T>::operator=(const mozilla::OwningNonNull<U>& aOther)
|
2015-04-19 12:28:50 +00:00
|
|
|
{
|
|
|
|
return operator=(aOther.get());
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
// Declared in mozilla/RefPtr.h
|
2015-04-19 12:28:50 +00:00
|
|
|
template<class T> template<class U>
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<T>::RefPtr(const mozilla::OwningNonNull<U>& aOther)
|
|
|
|
: RefPtr(aOther.get())
|
2015-04-19 12:28:50 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
template<class T> template<class U>
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<T>&
|
|
|
|
RefPtr<T>::operator=(const mozilla::OwningNonNull<U>& aOther)
|
2015-04-19 12:28:50 +00:00
|
|
|
{
|
|
|
|
return operator=(aOther.get());
|
|
|
|
}
|
|
|
|
|
2015-08-05 12:28:27 +00:00
|
|
|
#endif // mozilla_OwningNonNull_h
|