2013-09-10 11:03:37 +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/. */
|
|
|
|
|
|
|
|
/* A class for non-null strong pointers to reference-counted objects. */
|
|
|
|
|
2015-08-05 15:28:27 +03:00
|
|
|
#ifndef mozilla_OwningNonNull_h
|
|
|
|
#define mozilla_OwningNonNull_h
|
2013-09-10 11:03:37 +04:00
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
2014-08-01 11:19:00 +04:00
|
|
|
#include "nsCycleCollectionNoteChild.h"
|
2013-09-10 11:03:37 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class OwningNonNull
|
|
|
|
{
|
|
|
|
public:
|
2015-04-19 15:28:49 +03:00
|
|
|
OwningNonNull() {}
|
|
|
|
|
|
|
|
MOZ_IMPLICIT OwningNonNull(T& aValue)
|
|
|
|
{
|
|
|
|
init(&aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class U>
|
|
|
|
MOZ_IMPLICIT OwningNonNull(already_AddRefed<U>&& aValue)
|
|
|
|
{
|
2015-04-24 14:27:34 +03:00
|
|
|
init(aValue);
|
2015-04-19 15:28:49 +03:00
|
|
|
}
|
2013-09-10 11:03:37 +04:00
|
|
|
|
2014-06-24 00:03:57 +04:00
|
|
|
// This is no worse than get() in terms of const handling.
|
|
|
|
operator T&() const
|
2013-09-10 11:03:37 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mInited);
|
|
|
|
MOZ_ASSERT(mPtr, "OwningNonNull<T> was set to null");
|
|
|
|
return *mPtr;
|
|
|
|
}
|
|
|
|
|
2014-06-24 00:03:57 +04:00
|
|
|
operator T*() const
|
2013-12-20 12:51:03 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mInited);
|
|
|
|
MOZ_ASSERT(mPtr, "OwningNonNull<T> was set to null");
|
|
|
|
return mPtr;
|
|
|
|
}
|
|
|
|
|
2015-04-19 15:28:49 +03: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 15:28:50 +03:00
|
|
|
OwningNonNull<T>&
|
|
|
|
operator=(T* aValue)
|
2013-09-10 11:03:37 +04:00
|
|
|
{
|
|
|
|
init(aValue);
|
2015-04-19 15:28:50 +03:00
|
|
|
return *this;
|
2013-09-10 11:03:37 +04:00
|
|
|
}
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
OwningNonNull<T>&
|
|
|
|
operator=(T& aValue)
|
2014-06-06 01:16:54 +04:00
|
|
|
{
|
|
|
|
init(&aValue);
|
2015-04-19 15:28:50 +03:00
|
|
|
return *this;
|
2014-06-06 01:16:54 +04:00
|
|
|
}
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
OwningNonNull<T>&
|
2015-07-31 20:30:55 +03:00
|
|
|
operator=(already_AddRefed<T>&& aValue)
|
2013-09-10 11:03:37 +04:00
|
|
|
{
|
|
|
|
init(aValue);
|
2015-04-19 15:28:50 +03:00
|
|
|
return *this;
|
2013-09-10 11:03:37 +04:00
|
|
|
}
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
// Don't allow assigning nullptr, it makes no sense
|
|
|
|
void operator=(decltype(nullptr)) = delete;
|
|
|
|
|
2013-09-10 11:03:37 +04:00
|
|
|
already_AddRefed<T> forget()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInited = false;
|
|
|
|
#endif
|
|
|
|
return mPtr.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 15:28:49 +03:00
|
|
|
template<typename U>
|
|
|
|
void swap(U& aOther)
|
|
|
|
{
|
|
|
|
mPtr.swap(aOther);
|
|
|
|
}
|
|
|
|
|
2013-09-10 11:03:37 +04:00
|
|
|
protected:
|
|
|
|
template<typename U>
|
2015-04-24 14:27:34 +03:00
|
|
|
void init(U&& aValue)
|
2013-09-10 11:03:37 +04:00
|
|
|
{
|
|
|
|
mPtr = aValue;
|
|
|
|
MOZ_ASSERT(mPtr);
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInited = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T> mPtr;
|
2013-09-10 11:03:37 +04:00
|
|
|
#ifdef DEBUG
|
2015-04-19 15:28:49 +03:00
|
|
|
bool mInited = false;
|
2013-09-10 11:03:37 +04:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2014-08-01 11:19:00 +04: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 11:03:37 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-04-19 15:28:50 +03:00
|
|
|
// Declared in nsCOMPtr.h
|
|
|
|
template<class T> template<class U>
|
2015-08-05 15:28:27 +03:00
|
|
|
nsCOMPtr<T>::nsCOMPtr(const mozilla::OwningNonNull<U>& aOther)
|
2015-04-19 15:28:50 +03:00
|
|
|
: nsCOMPtr(aOther.get())
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class T> template<class U>
|
|
|
|
nsCOMPtr<T>&
|
2015-08-05 15:28:27 +03:00
|
|
|
nsCOMPtr<T>::operator=(const mozilla::OwningNonNull<U>& aOther)
|
2015-04-19 15:28:50 +03:00
|
|
|
{
|
|
|
|
return operator=(aOther.get());
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
// Declared in mozilla/RefPtr.h
|
2015-04-19 15:28:50 +03:00
|
|
|
template<class T> template<class U>
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T>::RefPtr(const mozilla::OwningNonNull<U>& aOther)
|
|
|
|
: RefPtr(aOther.get())
|
2015-04-19 15:28:50 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
template<class T> template<class U>
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T>&
|
|
|
|
RefPtr<T>::operator=(const mozilla::OwningNonNull<U>& aOther)
|
2015-04-19 15:28:50 +03:00
|
|
|
{
|
|
|
|
return operator=(aOther.get());
|
|
|
|
}
|
|
|
|
|
2015-08-05 15:28:27 +03:00
|
|
|
#endif // mozilla_OwningNonNull_h
|