dawn-cmake/src/castable_test.cc
Ben Clayton 95feb99a9d Add Castable template class
Implements `As<Blah>()` and `Is<Blah>()` automatically.

There are several benefits to using this over the pattern of hand-rolled `IsBlah()`, `AsBlah()` methods:
(1) We don't have to maintain a whole lot of hand written code.
(2) These allow us to cast from the base type to _any_ derived type in a single cast. The existing hand-rolled methods usually require a couple of intermediary casts to go from the base type to the leaf type.
(3) The use of a template parameter means these casts can be called from other template logic.

Note: Unlike the hand-rolled `AsBlah()` methods, it is safe to call `As<T>()` even if the type does not derive from `T`. If the object does not derive from `T` then  `As` will simply return `nullptr`. This allows the calling logic to replace the common pattern of:

```
if (obj.IsBlah()) {
   auto* b = obj.AsBlah();
   ...
}
```

with:

```
if (auto* b = obj.As<Blah>()) {
   ...
}
```

This halves the number of virtual method calls, and is one line shorter.

Change-Id: I4312e9831d7de6703a97184640864b8050a34177
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34260
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
2020-11-30 22:50:25 +00:00

145 lines
4.4 KiB
C++

// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/castable.h"
#include <memory>
#include <string>
#include "gtest/gtest.h"
namespace tint {
namespace {
struct Animal : public Castable<Animal> {
explicit Animal(std::string n) : name(n) {}
const std::string name;
};
struct Amphibian : public Castable<Amphibian, Animal> {
explicit Amphibian(std::string n) : Base(n) {}
};
struct Mammal : public Castable<Mammal, Animal> {
explicit Mammal(std::string n) : Base(n) {}
};
struct Reptile : public Castable<Reptile, Animal> {
explicit Reptile(std::string n) : Base(n) {}
};
struct Frog : public Castable<Frog, Amphibian> {
Frog() : Base("Frog") {}
};
struct Bear : public Castable<Bear, Mammal> {
Bear() : Base("Bear") {}
};
struct Gecko : public Castable<Gecko, Reptile> {
Gecko() : Base("Gecko") {}
};
} // namespace
TEST(CastableBase, Is) {
std::unique_ptr<CastableBase> frog = std::make_unique<Frog>();
std::unique_ptr<CastableBase> bear = std::make_unique<Bear>();
std::unique_ptr<CastableBase> gecko = std::make_unique<Gecko>();
ASSERT_TRUE(frog->Is<Animal>());
ASSERT_TRUE(bear->Is<Animal>());
ASSERT_TRUE(gecko->Is<Animal>());
ASSERT_TRUE(frog->Is<Amphibian>());
ASSERT_FALSE(bear->Is<Amphibian>());
ASSERT_FALSE(gecko->Is<Amphibian>());
ASSERT_FALSE(frog->Is<Mammal>());
ASSERT_TRUE(bear->Is<Mammal>());
ASSERT_FALSE(gecko->Is<Mammal>());
ASSERT_FALSE(frog->Is<Reptile>());
ASSERT_FALSE(bear->Is<Reptile>());
ASSERT_TRUE(gecko->Is<Reptile>());
}
TEST(CastableBase, As) {
std::unique_ptr<CastableBase> frog = std::make_unique<Frog>();
std::unique_ptr<CastableBase> bear = std::make_unique<Bear>();
std::unique_ptr<CastableBase> gecko = std::make_unique<Gecko>();
ASSERT_EQ(frog->As<Animal>(), static_cast<Animal*>(frog.get()));
ASSERT_EQ(bear->As<Animal>(), static_cast<Animal*>(bear.get()));
ASSERT_EQ(gecko->As<Animal>(), static_cast<Animal*>(gecko.get()));
ASSERT_EQ(frog->As<Amphibian>(), static_cast<Amphibian*>(frog.get()));
ASSERT_EQ(bear->As<Amphibian>(), nullptr);
ASSERT_EQ(gecko->As<Amphibian>(), nullptr);
ASSERT_EQ(frog->As<Mammal>(), nullptr);
ASSERT_EQ(bear->As<Mammal>(), static_cast<Mammal*>(bear.get()));
ASSERT_EQ(gecko->As<Mammal>(), nullptr);
ASSERT_EQ(frog->As<Reptile>(), nullptr);
ASSERT_EQ(bear->As<Reptile>(), nullptr);
ASSERT_EQ(gecko->As<Reptile>(), static_cast<Reptile*>(gecko.get()));
}
TEST(Castable, Is) {
std::unique_ptr<Animal> frog = std::make_unique<Frog>();
std::unique_ptr<Animal> bear = std::make_unique<Bear>();
std::unique_ptr<Animal> gecko = std::make_unique<Gecko>();
ASSERT_TRUE(frog->Is<Animal>());
ASSERT_TRUE(bear->Is<Animal>());
ASSERT_TRUE(gecko->Is<Animal>());
ASSERT_TRUE(frog->Is<Amphibian>());
ASSERT_FALSE(bear->Is<Amphibian>());
ASSERT_FALSE(gecko->Is<Amphibian>());
ASSERT_FALSE(frog->Is<Mammal>());
ASSERT_TRUE(bear->Is<Mammal>());
ASSERT_FALSE(gecko->Is<Mammal>());
ASSERT_FALSE(frog->Is<Reptile>());
ASSERT_FALSE(bear->Is<Reptile>());
ASSERT_TRUE(gecko->Is<Reptile>());
}
TEST(Castable, As) {
std::unique_ptr<Animal> frog = std::make_unique<Frog>();
std::unique_ptr<Animal> bear = std::make_unique<Bear>();
std::unique_ptr<Animal> gecko = std::make_unique<Gecko>();
ASSERT_EQ(frog->As<Animal>(), static_cast<Animal*>(frog.get()));
ASSERT_EQ(bear->As<Animal>(), static_cast<Animal*>(bear.get()));
ASSERT_EQ(gecko->As<Animal>(), static_cast<Animal*>(gecko.get()));
ASSERT_EQ(frog->As<Amphibian>(), static_cast<Amphibian*>(frog.get()));
ASSERT_EQ(bear->As<Amphibian>(), nullptr);
ASSERT_EQ(gecko->As<Amphibian>(), nullptr);
ASSERT_EQ(frog->As<Mammal>(), nullptr);
ASSERT_EQ(bear->As<Mammal>(), static_cast<Mammal*>(bear.get()));
ASSERT_EQ(gecko->As<Mammal>(), nullptr);
ASSERT_EQ(frog->As<Reptile>(), nullptr);
ASSERT_EQ(bear->As<Reptile>(), nullptr);
ASSERT_EQ(gecko->As<Reptile>(), static_cast<Reptile*>(gecko.get()));
}
} // namespace tint