BLI: add utility to check if type is any specific type

This adds `blender::is_same_any_v` which is the almost the same as
`std::is_same_v`. The difference is that it allows for checking multiple
types at the same time.

Differential Revision: https://developer.blender.org/D13673
This commit is contained in:
2021-12-27 16:08:11 +01:00
parent 594438ef0d
commit 51a131ddbc
7 changed files with 24 additions and 16 deletions

View File

@@ -497,6 +497,12 @@ inline constexpr bool is_span_convertible_pointer_v =
/* Allow casting any pointer to const void pointers. */ /* Allow casting any pointer to const void pointers. */
std::is_same_v<To, const void *>); std::is_same_v<To, const void *>);
/**
* Same as #std::is_same_v but allows for checking multiple types at the same time.
*/
template<typename T, typename... Args>
inline constexpr bool is_same_any_v = (std::is_same_v<T, Args> || ...);
/** /**
* Inline buffers for small-object-optimization should be disable by default. Otherwise we might * Inline buffers for small-object-optimization should be disable by default. Otherwise we might
* get large unexpected allocations on the stack. * get large unexpected allocations on the stack.

View File

@@ -477,9 +477,9 @@ template<typename T> struct VArrayAnyExtraInfo {
template<typename StorageT> static VArrayAnyExtraInfo get() template<typename StorageT> static VArrayAnyExtraInfo get()
{ {
/* These are the only allowed types in the #Any. */ /* These are the only allowed types in the #Any. */
static_assert(std::is_base_of_v<VArrayImpl<T>, StorageT> || static_assert(
std::is_same_v<StorageT, const VArrayImpl<T> *> || std::is_base_of_v<VArrayImpl<T>, StorageT> ||
std::is_same_v<StorageT, std::shared_ptr<const VArrayImpl<T>>>); is_same_any_v<StorageT, const VArrayImpl<T> *, std::shared_ptr<const VArrayImpl<T>>>);
/* Depending on how the virtual array implementation is stored in the #Any, a different /* Depending on how the virtual array implementation is stored in the #Any, a different
* #get_varray function is required. */ * #get_varray function is required. */

View File

@@ -169,4 +169,11 @@ static_assert(is_span_convertible_pointer_v<int *, const void *>);
static_assert(!is_span_convertible_pointer_v<TestBaseClass *, TestChildClass *>); static_assert(!is_span_convertible_pointer_v<TestBaseClass *, TestChildClass *>);
static_assert(!is_span_convertible_pointer_v<TestChildClass *, TestBaseClass *>); static_assert(!is_span_convertible_pointer_v<TestChildClass *, TestBaseClass *>);
static_assert(is_same_any_v<int, float, bool, int>);
static_assert(is_same_any_v<int, int, float>);
static_assert(is_same_any_v<int, int>);
static_assert(!is_same_any_v<int, float, bool>);
static_assert(!is_same_any_v<int, float>);
static_assert(!is_same_any_v<int>);
} // namespace blender::tests } // namespace blender::tests

View File

@@ -753,8 +753,7 @@ namespace detail {
template<typename StorageT> inline GVArrayAnyExtraInfo GVArrayAnyExtraInfo::get() template<typename StorageT> inline GVArrayAnyExtraInfo GVArrayAnyExtraInfo::get()
{ {
static_assert(std::is_base_of_v<GVArrayImpl, StorageT> || static_assert(std::is_base_of_v<GVArrayImpl, StorageT> ||
std::is_same_v<StorageT, const GVArrayImpl *> || is_same_any_v<StorageT, const GVArrayImpl *, std::shared_ptr<const GVArrayImpl>>);
std::is_same_v<StorageT, std::shared_ptr<const GVArrayImpl>>);
if constexpr (std::is_base_of_v<GVArrayImpl, StorageT>) { if constexpr (std::is_base_of_v<GVArrayImpl, StorageT>) {
return {[](const void *buffer) { return {[](const void *buffer) {

View File

@@ -203,9 +203,10 @@ struct DisplaceGridOp {
template<typename GridType> void operator()() template<typename GridType> void operator()()
{ {
if constexpr (std::is_same_v<GridType, openvdb::points::PointDataGrid> || if constexpr (blender::is_same_any_v<GridType,
std::is_same_v<GridType, openvdb::StringGrid> || openvdb::points::PointDataGrid,
std::is_same_v<GridType, openvdb::MaskGrid>) { openvdb::StringGrid,
openvdb::MaskGrid>) {
/* We don't support displacing these grid types yet. */ /* We don't support displacing these grid types yet. */
return; return;
} }

View File

@@ -134,12 +134,8 @@ class GeoNodeExecParams {
} }
template<typename T> template<typename T>
static inline constexpr bool is_field_base_type_v = std::is_same_v<T, float> || static inline constexpr bool is_field_base_type_v =
std::is_same_v<T, int> || is_same_any_v<T, float, int, bool, ColorGeometry4f, float3, std::string>;
std::is_same_v<T, bool> ||
std::is_same_v<T, ColorGeometry4f> ||
std::is_same_v<T, float3> ||
std::is_same_v<T, std::string>;
/** /**
* Get the input value for the input socket with the given identifier. * Get the input value for the input socket with the given identifier.

View File

@@ -180,8 +180,7 @@ static void join_component_type(Span<GeometrySet> src_geometry_sets, GeometrySet
InstancesComponent &instances = InstancesComponent &instances =
instances_geometry_set.get_component_for_write<InstancesComponent>(); instances_geometry_set.get_component_for_write<InstancesComponent>();
if constexpr (std::is_same_v<Component, InstancesComponent> || if constexpr (is_same_any_v<Component, InstancesComponent, VolumeComponent>) {
std::is_same_v<Component, VolumeComponent>) {
join_components(components, result); join_components(components, result);
} }
else { else {