From d99af036634c3dc612d18042465bfcc45203fffc Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Fri, 20 May 2022 09:03:50 +0000 Subject: [PATCH] tint: Add utils::UniqueVector::data() Change-Id: Ibbd4f595c5fdaacf93c13757878ed06675e735c6 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/91022 Commit-Queue: Ben Clayton Reviewed-by: Antonio Maiorano --- src/tint/utils/unique_vector.h | 3 +++ src/tint/utils/unique_vector_test.cc | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/src/tint/utils/unique_vector.h b/src/tint/utils/unique_vector.h index 96d9cbf2b3..0f3f18d8a1 100644 --- a/src/tint/utils/unique_vector.h +++ b/src/tint/utils/unique_vector.h @@ -75,6 +75,9 @@ struct UniqueVector { /// @returns the number of items in the vector size_t size() const { return vector.size(); } + /// @returns the pointer to the first element in the vector, or nullptr if the vector is empty. + const T* data() const { return vector.empty() ? nullptr : vector.data(); } + /// @returns an iterator to the beginning of the vector ConstIterator begin() const { return vector.begin(); } diff --git a/src/tint/utils/unique_vector_test.cc b/src/tint/utils/unique_vector_test.cc index 7d586e99a2..035ebf8de7 100644 --- a/src/tint/utils/unique_vector_test.cc +++ b/src/tint/utils/unique_vector_test.cc @@ -139,5 +139,14 @@ TEST(UniqueVectorTest, PopBack) { EXPECT_EQ(unique_vec.empty(), true); } +TEST(UniqueVectorTest, Data) { + UniqueVector unique_vec; + EXPECT_EQ(unique_vec.data(), nullptr); + + unique_vec.add(42); + EXPECT_EQ(unique_vec.data(), &unique_vec[0]); + EXPECT_EQ(*unique_vec.data(), 42); +} + } // namespace } // namespace tint::utils