dawn-cmake/src/tint/reflection.h
Ben Clayton 648bd7b4be tint: Add TINT_REFLECT() & ForeachField()
This uses template and macro magic to reflect the fields of a class.

Dawn:
* Reflect the fields of the types that are used by Dawn's stream::Stream<T> specializations, and use tint::ForeachField() to call StreamIn().

Fuzzers:
* Replace tint::fuzzers::DataBuilder::BuildImpl<T> specializations with the new reflection system.
* static_assert that the type is either POD or reflected. Add a specialization for std::optional which was missing.

Move tint::transform::BindingPoints into MultiplanarExternalTexture, as this is only used by MultiplanarExternalTexture.

All this reduces fragility of the struct declarations slipping out of sync with the uses.

Bug: tint:1640
Change-Id: I08729c1c356f1b427e85983efe3c2678fc2ce717
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/101001
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
2022-09-02 11:40:19 +00:00

68 lines
2.5 KiB
C++

// Copyright 2022 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.
#ifndef SRC_TINT_REFLECTION_H_
#define SRC_TINT_REFLECTION_H_
#include "src/tint/traits.h"
#include "src/tint/utils/concat.h"
#include "src/tint/utils/foreach_macro.h"
namespace tint {
namespace detail {
/// Helper for detecting whether the type T contains a nested Reflection class.
template <typename T, typename ENABLE = void>
struct HasReflection : std::false_type {};
/// Specialization for types that have a nested Reflection class.
template <typename T>
struct HasReflection<T, std::void_t<typename T::Reflection>> : std::true_type {};
} // namespace detail
/// Is true if the class T has reflected its fields with TINT_REFLECT()
template <typename T>
static constexpr bool HasReflection = detail::HasReflection<T>::value;
/// Calls @p callback with each field of @p object
/// @param object the object
/// @param callback a function that is called for each field of @p object.
/// @tparam CB a function with the signature `void(FIELD)`
template <typename OBJECT, typename CB>
void ForeachField(OBJECT&& object, CB&& callback) {
using T = std::decay_t<OBJECT>;
static_assert(HasReflection<T>, "object type requires a tint::Reflect<> specialization");
T::Reflection::Fields(object, callback);
}
/// Macro used by TINT_FOREACH() in TINT_REFLECT() to call the callback function with each field in
/// the variadic.
#define TINT_REFLECT_CALLBACK_FIELD(field) callback(object.field);
// TINT_REFLECT(...) reflects each of the fields arguments so that the types can be used with
// tint::ForeachField().
#define TINT_REFLECT(...) \
struct Reflection { \
template <typename OBJECT, typename CB> \
static void Fields(OBJECT&& object, CB&& callback) { \
TINT_FOREACH(TINT_REFLECT_CALLBACK_FIELD, __VA_ARGS__) \
} \
}
} // namespace tint
#endif // SRC_TINT_REFLECTION_H_