diff options
author | Jacques Lucke <jacques@blender.org> | 2022-03-19 12:57:40 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2022-03-19 12:57:40 +0300 |
commit | 8711483632823524019a6cc95575c5a4ba5aa831 (patch) | |
tree | 76c2f8355d0fce66f4f9e35b5ec1282960e51209 /source/blender/blenkernel/BKE_attribute_math.hh | |
parent | 3e16f3b3ef4b8f385b30fe4a1e00860620f610ee (diff) |
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.
Diffstat (limited to 'source/blender/blenkernel/BKE_attribute_math.hh')
-rw-r--r-- | source/blender/blenkernel/BKE_attribute_math.hh | 71 |
1 files changed, 17 insertions, 54 deletions
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<typename Func> -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<float, float2, float3, int, bool, int8_t, ColorGeometry4f>( + [&](auto type_tag) { + using T = typename decltype(type_tag)::type; + if constexpr (std::is_same_v<T, void>) { + /* It's expected that the given cpp type is one of the supported once. */ + BLI_assert_unreachable(); + } + else { + func(T()); + } + }); } template<typename Func> -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<float>()) { - func(float()); - } - else if (cpp_type.is<float2>()) { - func(float2()); - } - else if (cpp_type.is<float3>()) { - func(float3()); - } - else if (cpp_type.is<int>()) { - func(int()); - } - else if (cpp_type.is<bool>()) { - func(bool()); - } - else if (cpp_type.is<int8_t>()) { - func(int8_t()); - } - else if (cpp_type.is<ColorGeometry4f>()) { - 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); } /* -------------------------------------------------------------------- */ |