From 51a131ddbc2eeebd13cdc6a71b2d356267fda73e Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Mon, 27 Dec 2021 16:08:11 +0100 Subject: 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 --- source/blender/blenlib/BLI_memory_utils.hh | 6 ++++++ source/blender/blenlib/BLI_virtual_array.hh | 6 +++--- source/blender/blenlib/tests/BLI_memory_utils_test.cc | 7 +++++++ source/blender/functions/FN_generic_virtual_array.hh | 3 +-- source/blender/modifiers/intern/MOD_volume_displace.cc | 7 ++++--- source/blender/nodes/NOD_geometry_exec.hh | 8 ++------ source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc | 3 +-- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/source/blender/blenlib/BLI_memory_utils.hh b/source/blender/blenlib/BLI_memory_utils.hh index 14eca49d126..9a5be79b61e 100644 --- a/source/blender/blenlib/BLI_memory_utils.hh +++ b/source/blender/blenlib/BLI_memory_utils.hh @@ -497,6 +497,12 @@ inline constexpr bool is_span_convertible_pointer_v = /* Allow casting any pointer to const void pointers. */ std::is_same_v); +/** + * Same as #std::is_same_v but allows for checking multiple types at the same time. + */ +template +inline constexpr bool is_same_any_v = (std::is_same_v || ...); + /** * Inline buffers for small-object-optimization should be disable by default. Otherwise we might * get large unexpected allocations on the stack. diff --git a/source/blender/blenlib/BLI_virtual_array.hh b/source/blender/blenlib/BLI_virtual_array.hh index 3ed9f14712e..86ac95e2c77 100644 --- a/source/blender/blenlib/BLI_virtual_array.hh +++ b/source/blender/blenlib/BLI_virtual_array.hh @@ -477,9 +477,9 @@ template struct VArrayAnyExtraInfo { template static VArrayAnyExtraInfo get() { /* These are the only allowed types in the #Any. */ - static_assert(std::is_base_of_v, StorageT> || - std::is_same_v *> || - std::is_same_v>>); + static_assert( + std::is_base_of_v, StorageT> || + is_same_any_v *, std::shared_ptr>>); /* Depending on how the virtual array implementation is stored in the #Any, a different * #get_varray function is required. */ diff --git a/source/blender/blenlib/tests/BLI_memory_utils_test.cc b/source/blender/blenlib/tests/BLI_memory_utils_test.cc index 23415e69b04..207f310d902 100644 --- a/source/blender/blenlib/tests/BLI_memory_utils_test.cc +++ b/source/blender/blenlib/tests/BLI_memory_utils_test.cc @@ -169,4 +169,11 @@ static_assert(is_span_convertible_pointer_v); static_assert(!is_span_convertible_pointer_v); static_assert(!is_span_convertible_pointer_v); +static_assert(is_same_any_v); +static_assert(is_same_any_v); +static_assert(is_same_any_v); +static_assert(!is_same_any_v); +static_assert(!is_same_any_v); +static_assert(!is_same_any_v); + } // namespace blender::tests diff --git a/source/blender/functions/FN_generic_virtual_array.hh b/source/blender/functions/FN_generic_virtual_array.hh index fc8612d6f87..6aebca51219 100644 --- a/source/blender/functions/FN_generic_virtual_array.hh +++ b/source/blender/functions/FN_generic_virtual_array.hh @@ -753,8 +753,7 @@ namespace detail { template inline GVArrayAnyExtraInfo GVArrayAnyExtraInfo::get() { static_assert(std::is_base_of_v || - std::is_same_v || - std::is_same_v>); + is_same_any_v>); if constexpr (std::is_base_of_v) { return {[](const void *buffer) { diff --git a/source/blender/modifiers/intern/MOD_volume_displace.cc b/source/blender/modifiers/intern/MOD_volume_displace.cc index fcf75040a9a..a1ca29f454c 100644 --- a/source/blender/modifiers/intern/MOD_volume_displace.cc +++ b/source/blender/modifiers/intern/MOD_volume_displace.cc @@ -203,9 +203,10 @@ struct DisplaceGridOp { template void operator()() { - if constexpr (std::is_same_v || - std::is_same_v || - std::is_same_v) { + if constexpr (blender::is_same_any_v) { /* We don't support displacing these grid types yet. */ return; } diff --git a/source/blender/nodes/NOD_geometry_exec.hh b/source/blender/nodes/NOD_geometry_exec.hh index f225b3b94b2..2246cc29dc4 100644 --- a/source/blender/nodes/NOD_geometry_exec.hh +++ b/source/blender/nodes/NOD_geometry_exec.hh @@ -134,12 +134,8 @@ class GeoNodeExecParams { } template - static inline constexpr bool is_field_base_type_v = std::is_same_v || - std::is_same_v || - std::is_same_v || - std::is_same_v || - std::is_same_v || - std::is_same_v; + static inline constexpr bool is_field_base_type_v = + is_same_any_v; /** * Get the input value for the input socket with the given identifier. diff --git a/source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc b/source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc index bf7a9f49829..1d483709a0a 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc @@ -180,8 +180,7 @@ static void join_component_type(Span src_geometry_sets, GeometrySet InstancesComponent &instances = instances_geometry_set.get_component_for_write(); - if constexpr (std::is_same_v || - std::is_same_v) { + if constexpr (is_same_any_v) { join_components(components, result); } else { -- cgit v1.2.3