Bug 1610066 - Make nsTArray::SafeElementAt not require the complete type for smart pointers. r=froydnj

Differential Revision: https://phabricator.services.mozilla.com/D60338

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Emilio Cobos Álvarez 2020-01-20 16:21:59 +00:00
Родитель 73177747e8
Коммит 71e16d9992
2 изменённых файлов: 26 добавлений и 22 удалений

Просмотреть файл

@ -264,12 +264,20 @@ struct nsTArray_SafeElementAtSmartPtrHelper {
typedef size_t index_type; typedef size_t index_type;
elem_type SafeElementAt(index_type aIndex) { elem_type SafeElementAt(index_type aIndex) {
return static_cast<Derived*>(this)->SafeElementAt(aIndex, nullptr); auto* derived = static_cast<Derived*>(this);
if (aIndex < derived->Length()) {
return derived->Elements()[aIndex];
}
return nullptr;
} }
// XXX: Probably should return const_elem_type, but callsites must be fixed. // XXX: Probably should return const_elem_type, but callsites must be fixed.
elem_type SafeElementAt(index_type aIndex) const { elem_type SafeElementAt(index_type aIndex) const {
return static_cast<const Derived*>(this)->SafeElementAt(aIndex, nullptr); auto* derived = static_cast<const Derived*>(this);
if (aIndex < derived->Length()) {
return derived->Elements()[aIndex];
}
return nullptr;
} }
}; };
@ -290,26 +298,8 @@ class OwningNonNull;
} // namespace mozilla } // namespace mozilla
template <class E, class Derived> template <class E, class Derived>
struct nsTArray_SafeElementAtHelper<mozilla::OwningNonNull<E>, Derived> { struct nsTArray_SafeElementAtHelper<mozilla::OwningNonNull<E>, Derived>
typedef E* elem_type; : public nsTArray_SafeElementAtSmartPtrHelper<E, Derived> {};
typedef const E* const_elem_type;
typedef size_t index_type;
elem_type SafeElementAt(index_type aIndex) {
if (aIndex < static_cast<Derived*>(this)->Length()) {
return static_cast<Derived*>(this)->ElementAt(aIndex);
}
return nullptr;
}
// XXX: Probably should return const_elem_type, but callsites must be fixed.
elem_type SafeElementAt(index_type aIndex) const {
if (aIndex < static_cast<const Derived*>(this)->Length()) {
return static_cast<const Derived*>(this)->ElementAt(aIndex);
}
return nullptr;
}
};
// Servo bindings. // Servo bindings.
extern "C" void Gecko_EnsureTArrayCapacity(void* aArray, size_t aCapacity, extern "C" void Gecko_EnsureTArrayCapacity(void* aArray, size_t aCapacity,

Просмотреть файл

@ -7,6 +7,7 @@
#include "nsTArray.h" #include "nsTArray.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "mozilla/ArrayUtils.h" #include "mozilla/ArrayUtils.h"
#include "mozilla/RefPtr.h"
using namespace mozilla; using namespace mozilla;
@ -461,4 +462,17 @@ TEST(TArray, MakeBackInserter)
ASSERT_EQ(expected, dst); ASSERT_EQ(expected, dst);
} }
// This should compile:
struct RefCounted;
class Foo {
~Foo(); // Intentionally out of line
nsTArray<RefPtr<RefCounted>> mArray;
const RefCounted* GetFirst() const {
return mArray.SafeElementAt(0);
}
};
} // namespace TestTArray } // namespace TestTArray