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 <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2021-01-26 16:57:10 +00:00
parent 44bec80e5f
commit a458c57662
2 changed files with 38 additions and 0 deletions

View File

@ -63,6 +63,19 @@ class Manager {
return type; 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 type map
/// @returns the mapping from name string to type. /// @returns the mapping from name string to type.
const std::unordered_map<std::string, type::Type*>& types() const { const std::unordered_map<std::string, type::Type*>& types() const {

View File

@ -22,6 +22,16 @@ namespace tint {
namespace type { namespace type {
namespace { namespace {
template <typename T>
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; using TypeManagerTest = testing::Test;
TEST_F(TypeManagerTest, GetUnregistered) { TEST_F(TypeManagerTest, GetUnregistered) {
@ -53,6 +63,21 @@ TEST_F(TypeManagerTest, GetDifferentTypeReturnsDifferentPtr) {
EXPECT_TRUE(t2->Is<U32>()); EXPECT_TRUE(t2->Is<U32>());
} }
TEST_F(TypeManagerTest, WrapDoesntAffectInner) {
Manager inner;
Manager outer = Manager::Wrap(inner);
inner.Get<I32>();
EXPECT_EQ(count(inner), 1u);
EXPECT_EQ(count(outer), 0u);
outer.Get<U32>();
EXPECT_EQ(count(inner), 1u);
EXPECT_EQ(count(outer), 1u);
}
} // namespace } // namespace
} // namespace type } // namespace type
} // namespace tint } // namespace tint