Commit Graph

8 Commits

Author SHA1 Message Date
Ben Clayton 26cd48603d Castable: Add FLAGS template argument for Is,As
[Is,As] contain a static_assert() that checks a cast is actually possible (TO -> FROM share a common base class).
This has been extremely valuable - it's caught numerious impossible casts due to stupid mistakes - however it makes certain generic templates impossible to write.

Add a compile-time FLAGS argument to Is() and As(), which accepts a new kDontErrorOnImpossibleCast flag.
When specified, this static_assert will always pass, allowing impossible casts to be attempted.

Change-Id: I5ff434b329c04d007f4e6976301bf30c73ab3f8d
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/47775
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
2021-04-16 15:05:44 +00:00
David Neto 5a01b72b98 Remove unusued param
Fixes GCC build

Change-Id: I0d89d48bdaa24f4199afa03197e8bbf1a0576b28
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/44803
Commit-Queue: David Neto <dneto@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Auto-Submit: David Neto <dneto@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
2021-03-15 21:28:22 +00:00
Antonio Maiorano 5106a0653c Add CastableBase::To() overload with predicate
This allows for a more optimal way to filter the result of To(). Updated
Type query functions to make use of it. Added tests.

Change-Id: If3a65259345fbe6b92c6d367ab01fa718bb7cfee
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/44463
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
2021-03-15 19:55:02 +00:00
Antonio Maiorano a089a56c73 Add Castsable::IsAnyOf<T1, T2, ...>()
This makes it a little easier to check if an object is one of any of the
types provided. Updated Type query functions to make use of IsAnyOf.
Added tests.

Change-Id: I12ea62b32042b6675d998ab85b86f2fe15861330
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/44462
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
2021-03-15 17:29:03 +00:00
Ben Clayton 7732ca51c4 Rename TINT_INSTANTIATE_CLASS_ID()
to TINT_INSTANTIATE_TYPEINFO()

ClassID isn't a thing any more.

Change-Id: Ie1c0d4a95e58ef7166d3cab5ef733a2dfc702345
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/42921
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
2021-03-02 20:51:18 +00:00
Ben Clayton 5f8061bd39 Castable: Minor tweaks
Prefix the fully-qualified Unique name in TINT_INSTANTIATE_CLASS_ID with :: as there might (however unlikely) be a nested 'tint' namespace.

Move the test structures in castable_test back into the anonymous namespace. This means `TINT_INSTANTIATE_CLASS_ID` needs to sit outside the anonymous namespace, but prevents global namespace pollution.

Change-Id: I035e9568c081fee120726106dc2150c4990c3881
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34567
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
2020-12-03 17:44:19 +00:00
Ben Clayton 89ea705766 Add ClassID::Unique template and TINT_INSTANTIATE_CLASS_ID
We're seeing some chrome bots fail unittests in ways that suspiciously
look like dynamic casts are doing Wrong Things.

The ClassID::Of() logic depends on the linker folding away duplicate
compilation unit definitions based on ODR rules. If we were to somehow
end up with different definitions, then we'd have two or more different
ClassIDs for the same T type - leading to issues similar to what we're
seeing.

I'm not entirely sure why/how this could happen - and we've so far been
entirely unable to locally reproduce - but it _might_ have something to
do with the goma cache.

In an attempt to work around this, move the static symbol definition out
of a header-local-static and into the .cc file for each of the types.

Change-Id: If914d3045b9dac6fbe8824dac71153a768cfceb9
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34563
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
2020-12-02 18:19:28 +00:00
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