[Bf-blender-cvs] [a8627ea66d7] master: Functions: Add debug print and destruct callback to CPPType

Jacques Lucke noreply at git.blender.org
Tue Jul 7 18:39:32 CEST 2020


Commit: a8627ea66d7825d676889e5e1b6fc915e5dd772f
Author: Jacques Lucke
Date:   Tue Jul 7 18:39:24 2020 +0200
Branches: master
https://developer.blender.org/rBa8627ea66d7825d676889e5e1b6fc915e5dd772f

Functions: Add debug print and destruct callback to CPPType

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

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 1681ff9fe8c..63e841e9612 100644
--- a/source/blender/functions/FN_cpp_type.hh
+++ b/source/blender/functions/FN_cpp_type.hh
@@ -104,6 +104,8 @@ class CPPType {
   using FillUninitializedF = void (*)(const void *value, void *dst, uint n);
   using FillUninitializedIndicesF = void (*)(const void *value, void *dst, IndexMask index_mask);
 
+  using DebugPrintF = void (*)(const void *value, std::stringstream &ss);
+
   CPPType(std::string name,
           uint size,
           uint alignment,
@@ -130,6 +132,7 @@ class CPPType {
           FillInitializedIndicesF fill_initialized_indices,
           FillUninitializedF fill_uninitialized,
           FillUninitializedIndicesF fill_uninitialized_indices,
+          DebugPrintF debug_print,
           const void *default_value)
       : size_(size),
         alignment_(alignment),
@@ -156,6 +159,7 @@ class CPPType {
         fill_initialized_indices_(fill_initialized_indices),
         fill_uninitialized_(fill_uninitialized),
         fill_uninitialized_indices_(fill_uninitialized_indices),
+        debug_print_(debug_print),
         default_value_(default_value),
         name_(name)
   {
@@ -277,6 +281,11 @@ class CPPType {
     destruct_indices_(ptr, index_mask);
   }
 
+  DestructF destruct_cb() const
+  {
+    return destruct_;
+  }
+
   /**
    * Copy an instance of this type from src to dst.
    *
@@ -457,6 +466,12 @@ class CPPType {
     fill_uninitialized_indices_(value, dst, index_mask);
   }
 
+  void debug_print(const void *value, std::stringstream &ss) const
+  {
+    BLI_assert(this->pointer_can_point_to_instance(value));
+    debug_print_(value, ss);
+  }
+
   /**
    * 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.
@@ -523,6 +538,8 @@ class CPPType {
   FillUninitializedF fill_uninitialized_;
   FillUninitializedIndicesF fill_uninitialized_indices_;
 
+  DebugPrintF debug_print_;
+
   const void *default_value_;
   std::string name_;
 };
@@ -683,6 +700,12 @@ void fill_uninitialized_indices_cb(const void *value, void *dst, IndexMask index
   index_mask.foreach_index([&](uint i) { new (dst_ + i) T(value_); });
 }
 
+template<typename T> void debug_print_cb(const void *value, std::stringstream &ss)
+{
+  const T &value_ = *(const T *)value;
+  ss << value_;
+}
+
 }  // namespace CPPTypeUtil
 
 template<typename T>
@@ -715,6 +738,7 @@ static std::unique_ptr<const CPPType> create_cpp_type(StringRef name, const T &d
                                     fill_initialized_indices_cb<T>,
                                     fill_uninitialized_cb<T>,
                                     fill_uninitialized_indices_cb<T>,
+                                    debug_print_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 1297ca471b7..6b6b1c846be 100644
--- a/tests/gtests/functions/FN_cpp_type_test.cc
+++ b/tests/gtests/functions/FN_cpp_type_test.cc
@@ -17,6 +17,7 @@
 #include "testing/testing.h"
 
 #include "FN_cpp_type.hh"
+#include "FN_cpp_types.hh"
 
 namespace blender::fn {
 
@@ -69,6 +70,12 @@ struct TestType {
     other.value = move_assigned_from_value;
     return *this;
   }
+
+  friend std::ostream &operator<<(std::ostream &stream, const TestType &value)
+  {
+    stream << value.value;
+    return stream;
+  }
 };
 
 MAKE_CPP_TYPE(TestType, TestType)
@@ -305,4 +312,13 @@ TEST(cpp_type, FillUninitialized)
   EXPECT_EQ(buffer2[9], 0);
 }
 
+TEST(cpp_type, DebugPrint)
+{
+  int value = 42;
+  std::stringstream ss;
+  CPPType_int32.debug_print((void *)&value, ss);
+  std::string text = ss.str();
+  EXPECT_EQ(text, "42");
+}
+
 }  // namespace blender::fn



More information about the Bf-blender-cvs mailing list