From a458c576625d334b59db532808265ad3c810f5ef Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 26 Jan 2021 16:57:10 +0000 Subject: [PATCH] type::Manager: Add Wrap() Wrap returns a new Manager created with the types of `inner`. The Manager returned by Wrap is intended to temporarily extend the types of an existing immutable Manager. Bug: tint:390 Change-Id: I46bebf8b83cf7987ddcf2513c54f9f885a028c60 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38551 Reviewed-by: dan sinclair --- src/type/type_manager.h | 13 +++++++++++++ src/type/type_manager_test.cc | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/type/type_manager.h b/src/type/type_manager.h index b157328260..779521b3b4 100644 --- a/src/type/type_manager.h +++ b/src/type/type_manager.h @@ -63,6 +63,19 @@ class Manager { return type; } + /// Wrap returns a new Manager created with the types of `inner`. + /// The Manager returned by Wrap is intended to temporarily extend the types + /// of an existing immutable Manager. + /// As the copied types are owned by `inner`, `inner` must not be destructed + /// or assigned while using the returned Manager. + /// @param inner the immutable Manager to extend + /// @return the Manager that wraps `inner` + static Manager Wrap(const Manager& inner) { + Manager out; + out.by_name_ = inner.by_name_; + return out; + } + /// Returns the type map /// @returns the mapping from name string to type. const std::unordered_map& types() const { diff --git a/src/type/type_manager_test.cc b/src/type/type_manager_test.cc index 5a938d5766..71846ef926 100644 --- a/src/type/type_manager_test.cc +++ b/src/type/type_manager_test.cc @@ -22,6 +22,16 @@ namespace tint { namespace type { namespace { +template +size_t count(const T& range_loopable) { + size_t n = 0; + for (auto it : range_loopable) { + (void)it; + n++; + } + return n; +} + using TypeManagerTest = testing::Test; TEST_F(TypeManagerTest, GetUnregistered) { @@ -53,6 +63,21 @@ TEST_F(TypeManagerTest, GetDifferentTypeReturnsDifferentPtr) { EXPECT_TRUE(t2->Is()); } +TEST_F(TypeManagerTest, WrapDoesntAffectInner) { + Manager inner; + Manager outer = Manager::Wrap(inner); + + inner.Get(); + + EXPECT_EQ(count(inner), 1u); + EXPECT_EQ(count(outer), 0u); + + outer.Get(); + + EXPECT_EQ(count(inner), 1u); + EXPECT_EQ(count(outer), 1u); +} + } // namespace } // namespace type } // namespace tint