[Bf-blender-cvs] [23501591013] master: Cleanup: make it easier to check if a CPPType is a specific compile time type

Jacques Lucke noreply at git.blender.org
Sat Jun 27 13:31:33 CEST 2020


Commit: 235015910133aaa50f8398680357af4a92e12778
Author: Jacques Lucke
Date:   Sat Jun 27 13:28:26 2020 +0200
Branches: master
https://developer.blender.org/rB235015910133aaa50f8398680357af4a92e12778

Cleanup: make it easier to check if a CPPType is a specific compile time type

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

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

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

diff --git a/source/blender/functions/FN_array_spans.hh b/source/blender/functions/FN_array_spans.hh
index fac2ef42c9d..acd3e921b50 100644
--- a/source/blender/functions/FN_array_spans.hh
+++ b/source/blender/functions/FN_array_spans.hh
@@ -184,7 +184,7 @@ class GVArraySpan : public VArraySpanBase<void> {
 
   template<typename T> VArraySpan<T> typed() const
   {
-    BLI_assert(CPPType::get<T>() == *m_type);
+    BLI_assert(m_type->is<T>());
     return VArraySpan<T>(*this);
   }
 
diff --git a/source/blender/functions/FN_cpp_type.hh b/source/blender/functions/FN_cpp_type.hh
index e44ce858b74..706ff85bdf3 100644
--- a/source/blender/functions/FN_cpp_type.hh
+++ b/source/blender/functions/FN_cpp_type.hh
@@ -483,6 +483,11 @@ class CPPType {
 
   template<typename T> static const CPPType &get();
 
+  template<typename T> bool is() const
+  {
+    return this == &CPPType::get<T>();
+  }
+
  private:
   uint m_size;
   uint m_alignment;
diff --git a/source/blender/functions/FN_generic_vector_array.hh b/source/blender/functions/FN_generic_vector_array.hh
index 1c8f74f2abe..6be1b68da4d 100644
--- a/source/blender/functions/FN_generic_vector_array.hh
+++ b/source/blender/functions/FN_generic_vector_array.hh
@@ -167,7 +167,7 @@ template<typename T> class GVectorArrayRef {
  public:
   GVectorArrayRef(GVectorArray &vector_array) : m_vector_array(&vector_array)
   {
-    BLI_assert(vector_array.m_type == CPPType::get<T>());
+    BLI_assert(vector_array.m_type.is<T>());
   }
 
   void append(uint index, const T &value)
diff --git a/source/blender/functions/FN_spans.hh b/source/blender/functions/FN_spans.hh
index d3497c05eb9..b4607527fb5 100644
--- a/source/blender/functions/FN_spans.hh
+++ b/source/blender/functions/FN_spans.hh
@@ -100,7 +100,7 @@ class GSpan {
 
   template<typename T> Span<T> typed() const
   {
-    BLI_assert(CPPType::get<T>() == *m_type);
+    BLI_assert(m_type->is<T>());
     return Span<T>((const T *)m_buffer, m_size);
   }
 };
@@ -166,7 +166,7 @@ class GMutableSpan {
 
   template<typename T> MutableSpan<T> typed()
   {
-    BLI_assert(CPPType::get<T>() == *m_type);
+    BLI_assert(m_type->is<T>());
     return MutableSpan<T>((T *)m_buffer, m_size);
   }
 };
@@ -372,7 +372,7 @@ class GVSpan : public VSpanBase<void> {
 
   template<typename T> VSpan<T> typed() const
   {
-    BLI_assert(CPPType::get<T>() == *m_type);
+    BLI_assert(m_type->is<T>());
     return VSpan<T>(*this);
   }
 
diff --git a/tests/gtests/functions/FN_cpp_type_test.cc b/tests/gtests/functions/FN_cpp_type_test.cc
index 83dc327e381..33e6fbee7f6 100644
--- a/tests/gtests/functions/FN_cpp_type_test.cc
+++ b/tests/gtests/functions/FN_cpp_type_test.cc
@@ -84,6 +84,12 @@ TEST(cpp_type, Alignment)
   EXPECT_EQ(CPPType_TestType.alignment(), alignof(TestType));
 }
 
+TEST(cpp_type, Is)
+{
+  EXPECT_TRUE(CPPType_TestType.is<TestType>());
+  EXPECT_FALSE(CPPType_TestType.is<int>());
+}
+
 TEST(cpp_type, DefaultConstruction)
 {
   int buffer[10] = {0};



More information about the Bf-blender-cvs mailing list