Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2022-03-19 12:57:40 +0300
committerJacques Lucke <jacques@blender.org>2022-03-19 12:57:40 +0300
commit8711483632823524019a6cc95575c5a4ba5aa831 (patch)
tree76c2f8355d0fce66f4f9e35b5ec1282960e51209 /source/blender/blenlib
parent3e16f3b3ef4b8f385b30fe4a1e00860620f610ee (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/blenlib')
-rw-r--r--source/blender/blenlib/BLI_cpp_type.hh73
-rw-r--r--source/blender/blenlib/intern/cpp_type.cc9
-rw-r--r--source/blender/blenlib/tests/BLI_cpp_type_test.cc24
3 files changed, 104 insertions, 2 deletions
diff --git a/source/blender/blenlib/BLI_cpp_type.hh b/source/blender/blenlib/BLI_cpp_type.hh
index ae6a87b4b68..881408f460b 100644
--- a/source/blender/blenlib/BLI_cpp_type.hh
+++ b/source/blender/blenlib/BLI_cpp_type.hh
@@ -22,6 +22,7 @@
* - If the code is not performance sensitive, it usually makes sense to use #CPPType instead.
* - Sometimes a combination can make sense. Optimized code can be be generated at compile-time for
* some types, while there is a fallback code path using #CPPType for all other types.
+ * #CPPType::to_static_type allows dispatching between both versions based on the type.
*
* Under some circumstances, #CPPType serves a similar role as #std::type_info. However, #CPPType
* has much more utility because it contains methods for actually working with instances of the
@@ -71,6 +72,7 @@
#include "BLI_hash.hh"
#include "BLI_index_mask.hh"
+#include "BLI_map.hh"
#include "BLI_math_base.h"
#include "BLI_string_ref.hh"
#include "BLI_utility_mixins.hh"
@@ -643,6 +645,77 @@ class CPPType : NonCopyable, NonMovable {
{
return this == &CPPType::get<std::decay_t<T>>();
}
+
+ /**
+ * Convert a #CPPType that is only known at run-time, to a static type that is known at
+ * compile-time. This allows the compiler to optimize a function for specific types, while all
+ * other types can still use a generic fallback function.
+ *
+ * \param Types The types that code should be generated for.
+ * \param fn The function object to call. This is expected to have a templated `operator()` and a
+ * non-templated `operator()`. The templated version will be called if the current #CPPType
+ * matches any of the given types. Otherwise, the non-templated function is called.
+ */
+ template<typename... Types, typename Fn> void to_static_type(const Fn &fn) const
+ {
+ using Callback = void (*)(const Fn &fn);
+
+ /* Build a lookup table to avoid having to compare the current #CPPType with every type in
+ * #Types one after another. */
+ static const Map<const CPPType *, Callback> callback_map = []() {
+ Map<const CPPType *, Callback> callback_map;
+ /* This adds an entry in the map for every type in #Types. */
+ (callback_map.add_new(&CPPType::get<Types>(),
+ [](const Fn &fn) {
+ /* Call the templated `operator()` of the given function object. */
+ fn.template operator()<Types>();
+ }),
+ ...);
+ return callback_map;
+ }();
+
+ const Callback callback = callback_map.lookup_default(this, nullptr);
+ if (callback != nullptr) {
+ callback(fn);
+ }
+ else {
+ /* Call the non-templated `operator()` of the given function object. */
+ fn();
+ }
+ }
+
+ template<typename T> struct type_tag {
+ using type = T;
+ };
+
+ private:
+ template<typename Fn> struct TypeTagExecutor {
+ const Fn &fn;
+
+ template<typename T> void operator()() const
+ {
+ fn(type_tag<T>{});
+ }
+
+ void operator()() const
+ {
+ fn(type_tag<void>{});
+ }
+ };
+
+ public:
+ /**
+ * Similar to #to_static_type but is easier to use with a lambda function. The function is
+ * expected to take a single `auto type_tag` parameter. To extract the static type, use:
+ * `using T = typename decltype(type_tag)::type;`
+ *
+ * If the current #CPPType is not in #Types, the type tag is `void`.
+ */
+ template<typename... Types, typename Fn> void to_static_type_tag(const Fn &fn) const
+ {
+ TypeTagExecutor<Fn> executor{fn};
+ this->to_static_type<Types...>(executor);
+ }
};
} // namespace blender
diff --git a/source/blender/blenlib/intern/cpp_type.cc b/source/blender/blenlib/intern/cpp_type.cc
index 72ccc54e552..d6a087cf175 100644
--- a/source/blender/blenlib/intern/cpp_type.cc
+++ b/source/blender/blenlib/intern/cpp_type.cc
@@ -12,10 +12,15 @@ BLI_CPP_TYPE_MAKE(float2, blender::float2, CPPTypeFlags::BasicType)
BLI_CPP_TYPE_MAKE(float3, blender::float3, CPPTypeFlags::BasicType)
BLI_CPP_TYPE_MAKE(float4x4, blender::float4x4, CPPTypeFlags::BasicType)
-BLI_CPP_TYPE_MAKE(int32, int32_t, CPPTypeFlags::BasicType)
BLI_CPP_TYPE_MAKE(int8, int8_t, CPPTypeFlags::BasicType)
-BLI_CPP_TYPE_MAKE(uint32, uint32_t, CPPTypeFlags::BasicType)
+BLI_CPP_TYPE_MAKE(int16, int16_t, CPPTypeFlags::BasicType)
+BLI_CPP_TYPE_MAKE(int32, int32_t, CPPTypeFlags::BasicType)
+BLI_CPP_TYPE_MAKE(int64, int64_t, CPPTypeFlags::BasicType)
+
BLI_CPP_TYPE_MAKE(uint8, uint8_t, CPPTypeFlags::BasicType)
+BLI_CPP_TYPE_MAKE(uint16, uint16_t, CPPTypeFlags::BasicType)
+BLI_CPP_TYPE_MAKE(uint32, uint32_t, CPPTypeFlags::BasicType)
+BLI_CPP_TYPE_MAKE(uint64, uint64_t, CPPTypeFlags::BasicType)
BLI_CPP_TYPE_MAKE(ColorGeometry4f, blender::ColorGeometry4f, CPPTypeFlags::BasicType)
BLI_CPP_TYPE_MAKE(ColorGeometry4b, blender::ColorGeometry4b, CPPTypeFlags::BasicType)
diff --git a/source/blender/blenlib/tests/BLI_cpp_type_test.cc b/source/blender/blenlib/tests/BLI_cpp_type_test.cc
index c7e6ed47b24..94456e1ee28 100644
--- a/source/blender/blenlib/tests/BLI_cpp_type_test.cc
+++ b/source/blender/blenlib/tests/BLI_cpp_type_test.cc
@@ -323,4 +323,28 @@ TEST(cpp_type, DebugPrint)
EXPECT_EQ(text, "42");
}
+TEST(cpp_type, ToStaticType)
+{
+ Vector<const CPPType *> types;
+ bool found_unsupported_type = false;
+ auto fn = [&](auto type_tag) {
+ using T = typename decltype(type_tag)::type;
+ if constexpr (!std::is_same_v<T, void>) {
+ types.append(&CPPType::get<T>());
+ }
+ else {
+ found_unsupported_type = true;
+ }
+ };
+ CPPType::get<std::string>().to_static_type_tag<int, float, std::string>(fn);
+ CPPType::get<float>().to_static_type_tag<int, float, std::string>(fn);
+ EXPECT_FALSE(found_unsupported_type);
+ CPPType::get<int64_t>().to_static_type_tag<int, float, std::string>(fn);
+ EXPECT_TRUE(found_unsupported_type);
+
+ EXPECT_EQ(types.size(), 2);
+ EXPECT_EQ(types[0], &CPPType::get<std::string>());
+ EXPECT_EQ(types[1], &CPPType::get<float>());
+}
+
} // namespace blender::tests