Cleanup: Adjust GSpan and GArray asserts

Previously, 4e9e44ad made changes to allow GSpan and GMutableSpan to not
have a type when they are empty. This mirrors the same change in the
conversion from GArray to both span types and when converting to an
actual typed Span<> or MutableSpan<> via typed().

Fixes #125013

Pull Request: https://projects.blender.org/blender/blender/pulls/125018
This commit is contained in:
Sean Kim
2024-07-22 16:35:36 +02:00
committed by Sean Kim
parent 27c877f684
commit 07c613b485
3 changed files with 17 additions and 6 deletions

View File

@@ -169,14 +169,14 @@ class GArray {
operator GSpan() const
{
BLI_assert(type_ != nullptr);
return GSpan(*type_, data_, size_);
BLI_assert(size_ == 0 || type_ != nullptr);
return GSpan(type_, data_, size_);
}
operator GMutableSpan()
{
BLI_assert(type_ != nullptr);
return GMutableSpan(*type_, data_, size_);
BLI_assert(size_ == 0 || type_ != nullptr);
return GMutableSpan(type_, data_, size_);
}
GSpan as_span() const

View File

@@ -85,7 +85,8 @@ class GSpan {
template<typename T> Span<T> typed() const
{
BLI_assert(type_->is<T>());
BLI_assert(size_ == 0 || type_ != nullptr);
BLI_assert(type_ == nullptr || type_->is<T>());
return Span<T>(static_cast<const T *>(data_), size_);
}
@@ -212,7 +213,8 @@ class GMutableSpan {
template<typename T> MutableSpan<T> typed() const
{
BLI_assert(type_->is<T>());
BLI_assert(size_ == 0 || type_ != nullptr);
BLI_assert(type_ == nullptr || type_->is<T>());
return MutableSpan<T>(static_cast<T *>(data_), size_);
}

View File

@@ -133,4 +133,13 @@ TEST(generic_array, AssignDefault)
EXPECT_EQ(array.data(), nullptr);
}
TEST(generic_array, DefaultConstructor)
{
GArray<> array;
EXPECT_TRUE(array.is_empty());
EXPECT_TRUE(array.as_mutable_span().is_empty());
EXPECT_TRUE(array.as_span().is_empty());
}
} // namespace blender::tests