[Bf-blender-cvs] [3edd2832b2f] master: Functions: make generic types hashable

Jacques Lucke noreply at git.blender.org
Fri Jul 10 12:58:23 CEST 2020


Commit: 3edd2832b2f6cb24581951ee592917c524900fb5
Author: Jacques Lucke
Date:   Fri Jul 10 12:54:19 2020 +0200
Branches: master
https://developer.blender.org/rB3edd2832b2f6cb24581951ee592917c524900fb5

Functions: make generic types hashable

===================================================================

M	source/blender/functions/FN_cpp_type.hh
M	tests/gtests/functions/FN_cpp_type_test.cc

===================================================================

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<typename T> bool is_equal_cb(const void *a, const void *b)
   return a_ == b_;
 }
 
+template<typename T> uint32_t hash_cb(const void *value)
+{
+  const T &value_ = *(const T *)value;
+  return DefaultHash<T>{}(value_);
+}
+
 }  // namespace cpp_type_util
 
 template<typename T>
@@ -762,6 +779,7 @@ inline std::unique_ptr<const CPPType> create_cpp_type(StringRef name, const T &d
                                     fill_uninitialized_indices_cb<T>,
                                     debug_print_cb<T>,
                                     is_equal_cb<T>,
+                                    hash_cb<T>,
                                     (const void *)&default_value);
   return std::unique_ptr<const CPPType>(type);
 }
diff --git a/tests/gtests/functions/FN_cpp_type_test.cc b/tests/gtests/functions/FN_cpp_type_test.cc
index c014dd0e1f7..da5ce3416ce 100644
--- a/tests/gtests/functions/FN_cpp_type_test.cc
+++ b/tests/gtests/functions/FN_cpp_type_test.cc
@@ -67,6 +67,11 @@ struct TestType {
   {
     return false;
   }
+
+  uint32_t hash() const
+  {
+    return 0;
+  }
 };
 
 MAKE_CPP_TYPE(TestType, TestType)



More information about the Bf-blender-cvs mailing list