mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-06-05 22:23:29 +00:00
traits: Replace FirstParamType with ParamType
Allows you to infer the N'th parameter type of a function. Change-Id: Iab7065cb37dbf1332cef601bca91894b8c6b4edf Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35662 Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
parent
1523f5c276
commit
f42b90dbe1
@ -119,7 +119,7 @@ class CloneContext {
|
|||||||
/// `T* (T*)`, where `T` derives from CastableBase
|
/// `T* (T*)`, where `T` derives from CastableBase
|
||||||
template <typename F>
|
template <typename F>
|
||||||
void ReplaceAll(F replacer) {
|
void ReplaceAll(F replacer) {
|
||||||
using TPtr = traits::FirstParamTypeT<F>;
|
using TPtr = traits::ParamTypeT<F, 0>;
|
||||||
using T = typename std::remove_pointer<TPtr>::type;
|
using T = typename std::remove_pointer<TPtr>::type;
|
||||||
transforms_.emplace_back([=](CastableBase* in) {
|
transforms_.emplace_back([=](CastableBase* in) {
|
||||||
auto* in_as_t = in->As<T>();
|
auto* in_as_t = in->As<T>();
|
||||||
|
@ -15,45 +15,55 @@
|
|||||||
#ifndef SRC_AST_TRAITS_H_
|
#ifndef SRC_AST_TRAITS_H_
|
||||||
#define SRC_AST_TRAITS_H_
|
#define SRC_AST_TRAITS_H_
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
namespace ast {
|
namespace ast {
|
||||||
namespace traits {
|
namespace traits {
|
||||||
|
|
||||||
/// FirstParamType is a traits helper that infers the type of the first
|
/// NthTypeOf returns the `N`th type in `Types`
|
||||||
/// parameter of the function, method, static method, lambda, or function-like
|
template <int N, typename... Types>
|
||||||
/// object `F`.
|
using NthTypeOf = typename std::tuple_element<N, std::tuple<Types...>>::type;
|
||||||
template <typename F>
|
|
||||||
struct FirstParamType {
|
/// ParamType is a traits helper that infers the type of the `N`th parameter
|
||||||
/// The type of the first parameter of the function-like object `F`
|
/// of the function, method, static method, lambda, or function-like object `F`.
|
||||||
using type = typename FirstParamType<decltype(&F::operator())>::type;
|
template <typename F, int N>
|
||||||
|
struct ParamType {
|
||||||
|
/// The type of the `N`th parameter of the function-like object `F`
|
||||||
|
using type = typename ParamType<decltype(&F::operator()), N>::type;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// FirstParamType specialization for a regular function or static method.
|
/// ParamType specialization for a regular function or static method.
|
||||||
template <typename R, typename Arg>
|
template <typename R, int N, typename... Args>
|
||||||
struct FirstParamType<R (*)(Arg)> {
|
struct ParamType<R (*)(Args...), N> {
|
||||||
/// The type of the first parameter of the function
|
/// Arg is the raw type of the `N`th parameter of the function
|
||||||
|
using Arg = NthTypeOf<N, Args...>;
|
||||||
|
/// The type of the `N`th parameter of the function
|
||||||
using type = typename std::decay<Arg>::type;
|
using type = typename std::decay<Arg>::type;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// FirstParamType specialization for a non-static method.
|
/// ParamType specialization for a non-static method.
|
||||||
template <typename R, typename C, typename Arg>
|
template <typename R, typename C, int N, typename... Args>
|
||||||
struct FirstParamType<R (C::*)(Arg)> {
|
struct ParamType<R (C::*)(Args...), N> {
|
||||||
/// The type of the first parameter of the function
|
/// Arg is the raw type of the `N`th parameter of the function
|
||||||
|
using Arg = NthTypeOf<N, Args...>;
|
||||||
|
/// The type of the `N`th parameter of the function
|
||||||
using type = typename std::decay<Arg>::type;
|
using type = typename std::decay<Arg>::type;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// FirstParamType specialization for a non-static, const method.
|
/// ParamType specialization for a non-static, const method.
|
||||||
template <typename R, typename C, typename Arg>
|
template <typename R, typename C, int N, typename... Args>
|
||||||
struct FirstParamType<R (C::*)(Arg) const> {
|
struct ParamType<R (C::*)(Args...) const, N> {
|
||||||
/// The type of the first parameter of the function
|
/// Arg is the raw type of the `N`th parameter of the function
|
||||||
|
using Arg = NthTypeOf<N, Args...>;
|
||||||
|
/// The type of the `N`th parameter of the function
|
||||||
using type = typename std::decay<Arg>::type;
|
using type = typename std::decay<Arg>::type;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// FirstParamTypeT is an alias to `typename FirstParamType<F>::type`.
|
/// ParamTypeT is an alias to `typename ParamType<F, N>::type`.
|
||||||
template <typename F>
|
template <typename F, int N>
|
||||||
using FirstParamTypeT = typename FirstParamType<F>::type;
|
using ParamTypeT = typename ParamType<F, N>::type;
|
||||||
|
|
||||||
/// If T is a base of BASE then EnableIfIsType resolves to type T, otherwise an
|
/// If T is a base of BASE then EnableIfIsType resolves to type T, otherwise an
|
||||||
/// invalid type.
|
/// invalid type.
|
||||||
|
@ -24,49 +24,80 @@ namespace traits {
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
struct S {};
|
struct S {};
|
||||||
void F(S) {}
|
void F1(S) {}
|
||||||
|
void F3(int, S, float) {}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
TEST(FirstParamType, Function) {
|
TEST(ParamType, Function) {
|
||||||
F({}); // Avoid unused method warning
|
F1({}); // Avoid unused method warning
|
||||||
static_assert(std::is_same<FirstParamTypeT<decltype(&F)>, S>::value, "");
|
F3(0, {}, 0); // Avoid unused method warning
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&F1), 0>, S>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&F3), 0>, int>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&F3), 1>, S>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&F3), 2>, float>::value, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FirstParamType, Method) {
|
TEST(ParamType, Method) {
|
||||||
class C {
|
class C {
|
||||||
public:
|
public:
|
||||||
void f(S) {}
|
void F1(S) {}
|
||||||
|
void F3(int, S, float) {}
|
||||||
};
|
};
|
||||||
C().f({}); // Avoid unused method warning
|
C().F1({}); // Avoid unused method warning
|
||||||
static_assert(std::is_same<FirstParamTypeT<decltype(&C::f)>, S>::value, "");
|
C().F3(0, {}, 0); // Avoid unused method warning
|
||||||
}
|
static_assert(std::is_same<ParamTypeT<decltype(&C::F1), 0>, S>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&C::F3), 0>, int>::value, "");
|
||||||
TEST(FirstParamType, ConstMethod) {
|
static_assert(std::is_same<ParamTypeT<decltype(&C::F3), 1>, S>::value, "");
|
||||||
class C {
|
static_assert(std::is_same<ParamTypeT<decltype(&C::F3), 2>, float>::value,
|
||||||
public:
|
|
||||||
void f(S) const {}
|
|
||||||
};
|
|
||||||
C().f({}); // Avoid unused method warning
|
|
||||||
static_assert(std::is_same<FirstParamTypeT<decltype(&C::f)>, S>::value, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(FirstParamType, StaticMethod) {
|
|
||||||
class C {
|
|
||||||
public:
|
|
||||||
static void f(S) {}
|
|
||||||
};
|
|
||||||
C().f({}); // Avoid unused method warning
|
|
||||||
static_assert(std::is_same<FirstParamTypeT<decltype(&C::f)>, S>::value, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(FirstParamType, FunctionLike) {
|
|
||||||
static_assert(std::is_same<FirstParamTypeT<std::function<void(S)>>, S>::value,
|
|
||||||
"");
|
"");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FirstParamType, Lambda) {
|
TEST(ParamType, ConstMethod) {
|
||||||
auto l = [](S) {};
|
class C {
|
||||||
static_assert(std::is_same<FirstParamTypeT<decltype(l)>, S>::value, "");
|
public:
|
||||||
|
void F1(S) const {}
|
||||||
|
void F3(int, S, float) const {}
|
||||||
|
};
|
||||||
|
C().F1({}); // Avoid unused method warning
|
||||||
|
C().F3(0, {}, 0); // Avoid unused method warning
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&C::F1), 0>, S>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&C::F3), 0>, int>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&C::F3), 1>, S>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&C::F3), 2>, float>::value,
|
||||||
|
"");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ParamType, StaticMethod) {
|
||||||
|
class C {
|
||||||
|
public:
|
||||||
|
static void F1(S) {}
|
||||||
|
static void F3(int, S, float) {}
|
||||||
|
};
|
||||||
|
C::F1({}); // Avoid unused method warning
|
||||||
|
C::F3(0, {}, 0); // Avoid unused method warning
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&C::F1), 0>, S>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&C::F3), 0>, int>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&C::F3), 1>, S>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(&C::F3), 2>, float>::value,
|
||||||
|
"");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ParamType, FunctionLike) {
|
||||||
|
using F1 = std::function<void(S)>;
|
||||||
|
using F3 = std::function<void(int, S, float)>;
|
||||||
|
static_assert(std::is_same<ParamTypeT<F1, 0>, S>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<F3, 0>, int>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<F3, 1>, S>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<F3, 2>, float>::value, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ParamType, Lambda) {
|
||||||
|
auto l1 = [](S) {};
|
||||||
|
auto l3 = [](int, S, float) {};
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(l1), 0>, S>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(l3), 0>, int>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(l3), 1>, S>::value, "");
|
||||||
|
static_assert(std::is_same<ParamTypeT<decltype(l3), 2>, float>::value, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace traits
|
} // namespace traits
|
||||||
|
Loading…
x
Reference in New Issue
Block a user