Bug 1463011 - Constexpr-ify mozilla::NotNull r=njn
MozReview-Commit-ID: EyXmRskjtfU
--- a/mfbt/NotNull.h
+++ b/mfbt/NotNull.h
@@ -97,33 +97,35 @@ namespace mozilla {
// checks.
//
// NotNull currently doesn't work with UniquePtr. See
// https://github.com/Microsoft/GSL/issues/89 for some discussion.
//
template <typename T>
class NotNull
{
- template <typename U> friend NotNull<U> WrapNotNull(U aBasePtr);
+ template <typename U> friend constexpr NotNull<U> WrapNotNull(U aBasePtr);
template<typename U, typename... Args>
- friend NotNull<U> MakeNotNull(Args&&... aArgs);
+ friend constexpr NotNull<U> MakeNotNull(Args&&... aArgs);
T mBasePtr;
// This constructor is only used by WrapNotNull() and MakeNotNull<U>().
template <typename U>
- explicit NotNull(U aBasePtr) : mBasePtr(aBasePtr) {}
+ constexpr explicit NotNull(U aBasePtr) : mBasePtr(aBasePtr) {}
public:
// Disallow default construction.
NotNull() = delete;
// Construct/assign from another NotNull with a compatible base pointer type.
template <typename U>
- MOZ_IMPLICIT NotNull(const NotNull<U>& aOther) : mBasePtr(aOther.get()) {
+ constexpr MOZ_IMPLICIT NotNull(const NotNull<U>& aOther)
+ : mBasePtr(aOther.get())
+ {
static_assert(sizeof(T) == sizeof(NotNull<T>),
"NotNull must have zero space overhead.");
static_assert(offsetof(NotNull<T>, mBasePtr) == 0,
"mBasePtr must have zero offset.");
}
// Default copy/move construction and assignment.
NotNull(const NotNull<T>&) = default;
@@ -131,28 +133,28 @@ public:
NotNull(NotNull<T>&&) = default;
NotNull<T>& operator=(NotNull<T>&&) = default;
// Disallow null checks, which are unnecessary for this type.
explicit operator bool() const = delete;
// Explicit conversion to a base pointer. Use only to resolve ambiguity or to
// get a castable pointer.
- const T& get() const { return mBasePtr; }
+ constexpr const T& get() const { return mBasePtr; }
// Implicit conversion to a base pointer. Preferable to get().
- operator const T&() const { return get(); }
+ constexpr operator const T&() const { return get(); }
// Dereference operators.
- const T& operator->() const { return get(); }
- decltype(*mBasePtr) operator*() const { return *mBasePtr; }
+ constexpr const T& operator->() const { return get(); }
+ constexpr decltype(*mBasePtr) operator*() const { return *mBasePtr; }
};
template <typename T>
-NotNull<T>
+constexpr NotNull<T>
WrapNotNull(const T aBasePtr)
{
NotNull<T> notNull(aBasePtr);
MOZ_RELEASE_ASSERT(aBasePtr);
return notNull;
}
namespace detail {
@@ -187,62 +189,62 @@ struct PointedTo<const T*>
};
} // namespace detail
// Allocate an object with infallible new, and wrap its pointer in NotNull.
// |MakeNotNull<Ptr<Ob>>(args...)| will run |new Ob(args...)|
// and return NotNull<Ptr<Ob>>.
template<typename T, typename... Args>
-NotNull<T>
+constexpr NotNull<T>
MakeNotNull(Args&&... aArgs)
{
using Pointee = typename detail::PointedTo<T>::NonConstType;
static_assert(!IsArray<Pointee>::value,
"MakeNotNull cannot construct an array");
return NotNull<T>(new Pointee(Forward<Args>(aArgs)...));
}
// Compare two NotNulls.
template <typename T, typename U>
-inline bool
+constexpr bool
operator==(const NotNull<T>& aLhs, const NotNull<U>& aRhs)
{
return aLhs.get() == aRhs.get();
}
template <typename T, typename U>
-inline bool
+constexpr bool
operator!=(const NotNull<T>& aLhs, const NotNull<U>& aRhs)
{
return aLhs.get() != aRhs.get();
}
// Compare a NotNull to a base pointer.
template <typename T, typename U>
-inline bool
+constexpr bool
operator==(const NotNull<T>& aLhs, const U& aRhs)
{
return aLhs.get() == aRhs;
}
template <typename T, typename U>
-inline bool
+constexpr bool
operator!=(const NotNull<T>& aLhs, const U& aRhs)
{
return aLhs.get() != aRhs;
}
// Compare a base pointer to a NotNull.
template <typename T, typename U>
-inline bool
+constexpr bool
operator==(const T& aLhs, const NotNull<U>& aRhs)
{
return aLhs == aRhs.get();
}
template <typename T, typename U>
-inline bool
+constexpr bool
operator!=(const T& aLhs, const NotNull<U>& aRhs)
{
return aLhs != aRhs.get();
}
// Disallow comparing a NotNull to a nullptr.
template <typename T>
bool