From 3edd2832b2f6cb24581951ee592917c524900fb5 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Fri, 10 Jul 2020 12:54:19 +0200 Subject: Functions: make generic types hashable --- source/blender/functions/FN_cpp_type.hh | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'source/blender') diff --git a/source/blender/functions/FN_cpp_type.hh b/source/blender/functions/FN_cpp_type.hh index bf4c81070fa..17976c8acb0 100644 --- a/source/blender/functions/FN_cpp_type.hh +++ b/source/blender/functions/FN_cpp_type.hh @@ -21,8 +21,9 @@ * \ingroup fn * * The CPPType class is the core of the runtime-type-system used by the functions system. It can - * represent C++ types that are default-constructible, destructible, movable, copyable and - * equality comparable. In the future we might want to make some of these properties optional. + * represent C++ types that are default-constructible, destructible, movable, copyable, + * equality comparable and hashable. In the future we might want to make some of these properties + * optional. * * Every type has a size and an alignment. Every function dealing with C++ types in a generic way, * has to make sure that alignment rules are followed. The methods provided by a CPPType instance @@ -106,6 +107,7 @@ class CPPType { using DebugPrintF = void (*)(const void *value, std::stringstream &ss); using IsEqualF = bool (*)(const void *a, const void *b); + using HashF = uint32_t (*)(const void *value); private: uint size_; @@ -145,6 +147,7 @@ class CPPType { DebugPrintF debug_print_; IsEqualF is_equal_; + HashF hash_; const void *default_value_; std::string name_; @@ -178,6 +181,7 @@ class CPPType { FillUninitializedIndicesF fill_uninitialized_indices, DebugPrintF debug_print, IsEqualF is_equal, + HashF hash, const void *default_value) : size_(size), alignment_(alignment), @@ -206,6 +210,7 @@ class CPPType { fill_uninitialized_indices_(fill_uninitialized_indices), debug_print_(debug_print), is_equal_(is_equal), + hash_(hash), default_value_(default_value), name_(name) { @@ -541,6 +546,12 @@ class CPPType { return is_equal_(a, b); } + uint32_t hash(const void *value) const + { + BLI_assert(this->pointer_can_point_to_instance(value)); + return hash_(value); + } + /** * Get a pointer to a constant value of this type. The specific value depends on the type. * It is usually a zero-initialized or default constructed value. @@ -728,6 +739,12 @@ template bool is_equal_cb(const void *a, const void *b) return a_ == b_; } +template uint32_t hash_cb(const void *value) +{ + const T &value_ = *(const T *)value; + return DefaultHash{}(value_); +} + } // namespace cpp_type_util template @@ -762,6 +779,7 @@ inline std::unique_ptr create_cpp_type(StringRef name, const T &d fill_uninitialized_indices_cb, debug_print_cb, is_equal_cb, + hash_cb, (const void *)&default_value); return std::unique_ptr(type); } -- cgit v1.2.3