From 8711483632823524019a6cc95575c5a4ba5aa831 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Sat, 19 Mar 2022 10:57:40 +0100 Subject: BLI: generalize converting CPPType to static type Previously, the conversion was done manually for a fixed set of types. Now, there is a more general utility that can be used in other contexts (outside of geometry nodes attribute processing) as well. --- source/blender/blenkernel/BKE_attribute_math.hh | 71 ++++++------------------- 1 file changed, 17 insertions(+), 54 deletions(-) (limited to 'source/blender/blenkernel/BKE_attribute_math.hh') diff --git a/source/blender/blenkernel/BKE_attribute_math.hh b/source/blender/blenkernel/BKE_attribute_math.hh index 9e97979fde9..3075c0689e9 100644 --- a/source/blender/blenkernel/BKE_attribute_math.hh +++ b/source/blender/blenkernel/BKE_attribute_math.hh @@ -8,71 +8,34 @@ #include "BLI_math_vector.h" #include "BLI_math_vector.hh" -#include "DNA_customdata_types.h" +#include "BKE_customdata.h" namespace blender::attribute_math { /** - * Utility function that simplifies calling a templated function based on a custom data type. + * Utility function that simplifies calling a templated function based on a run-time data type. */ template -inline void convert_to_static_type(const CustomDataType data_type, const Func &func) +inline void convert_to_static_type(const CPPType &cpp_type, const Func &func) { - switch (data_type) { - case CD_PROP_FLOAT: - func(float()); - break; - case CD_PROP_FLOAT2: - func(float2()); - break; - case CD_PROP_FLOAT3: - func(float3()); - break; - case CD_PROP_INT32: - func(int()); - break; - case CD_PROP_BOOL: - func(bool()); - break; - case CD_PROP_INT8: - func(int8_t()); - break; - case CD_PROP_COLOR: - func(ColorGeometry4f()); - break; - default: - BLI_assert_unreachable(); - break; - } + cpp_type.to_static_type_tag( + [&](auto type_tag) { + using T = typename decltype(type_tag)::type; + if constexpr (std::is_same_v) { + /* It's expected that the given cpp type is one of the supported once. */ + BLI_assert_unreachable(); + } + else { + func(T()); + } + }); } template -inline void convert_to_static_type(const CPPType &cpp_type, const Func &func) +inline void convert_to_static_type(const CustomDataType data_type, const Func &func) { - if (cpp_type.is()) { - func(float()); - } - else if (cpp_type.is()) { - func(float2()); - } - else if (cpp_type.is()) { - func(float3()); - } - else if (cpp_type.is()) { - func(int()); - } - else if (cpp_type.is()) { - func(bool()); - } - else if (cpp_type.is()) { - func(int8_t()); - } - else if (cpp_type.is()) { - func(ColorGeometry4f()); - } - else { - BLI_assert_unreachable(); - } + const CPPType &cpp_type = *bke::custom_data_type_to_cpp_type(data_type); + convert_to_static_type(cpp_type, func); } /* -------------------------------------------------------------------- */ -- cgit v1.2.3