[Bf-blender-cvs] [a6c822733ac] master: BLI: improve CPPType system

Jacques Lucke noreply at git.blender.org
Sat Nov 12 18:36:13 CET 2022


Commit: a6c822733ac7da4921297804475adadd50c08ed9
Author: Jacques Lucke
Date:   Sat Nov 12 18:33:31 2022 +0100
Branches: master
https://developer.blender.org/rBa6c822733ac7da4921297804475adadd50c08ed9

BLI: improve CPPType system

* Support bidirectional type lookups. E.g. finding the base type of a
  field was supported, but not the other way around. This also removes
  the todo in `get_vector_type`. To achieve this, types have to be
  registered up-front.
* Separate `CPPType` from other "type traits". For example, previously
  `ValueOrFieldCPPType` adds additional behavior on top of `CPPType`.
  Previously, it was a subclass, now it just contains a reference to the
  `CPPType` it corresponds to. This follows the composition-over-inheritance
  idea. This makes it easier to have self-contained "type traits" without
  having to put everything into `CPPType`.

Differential Revision: https://developer.blender.org/D16479

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

A	source/blender/blenkernel/BKE_cpp_types.h
M	source/blender/blenkernel/CMakeLists.txt
A	source/blender/blenkernel/intern/cpp_types.cc
M	source/blender/blenkernel/intern/customdata.cc
M	source/blender/blenkernel/intern/instances.cc
M	source/blender/blenlib/BLI_cpp_type.hh
M	source/blender/blenlib/BLI_cpp_type_make.hh
A	source/blender/blenlib/BLI_cpp_types.hh
A	source/blender/blenlib/BLI_cpp_types_make.hh
M	source/blender/blenlib/BLI_parameter_pack_utils.hh
M	source/blender/blenlib/CMakeLists.txt
D	source/blender/blenlib/intern/cpp_type.cc
A	source/blender/blenlib/intern/cpp_types.cc
M	source/blender/blenlib/tests/BLI_cpp_type_test.cc
M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/FN_field_cpp_type.hh
A	source/blender/functions/FN_field_cpp_type_make.hh
A	source/blender/functions/FN_init.h
M	source/blender/functions/intern/cpp_types.cc
A	source/blender/functions/intern/field_cpp_type.cc
M	source/blender/modifiers/intern/MOD_nodes.cc
M	source/blender/nodes/geometry/CMakeLists.txt
D	source/blender/nodes/geometry/node_geometry_exec.cc
M	source/blender/nodes/intern/geometry_nodes_lazy_function.cc
M	source/blender/nodes/intern/geometry_nodes_log.cc
M	source/blender/nodes/intern/node_socket.cc
M	source/creator/creator.c

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

diff --git a/source/blender/blenkernel/BKE_cpp_types.h b/source/blender/blenkernel/BKE_cpp_types.h
new file mode 100644
index 00000000000..7f103f563c3
--- /dev/null
+++ b/source/blender/blenkernel/BKE_cpp_types.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Register cpp types and their relations for later use.
+ */
+void BKE_cpp_types_init(void);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 462ccc19601..bb34ba3f135 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -102,6 +102,7 @@ set(SRC
   intern/compute_contexts.cc
   intern/constraint.c
   intern/context.c
+  intern/cpp_types.cc
   intern/crazyspace.cc
   intern/cryptomatte.cc
   intern/curve.cc
@@ -354,6 +355,7 @@ set(SRC
   BKE_compute_contexts.hh
   BKE_constraint.h
   BKE_context.h
+  BKE_cpp_types.h
   BKE_crazyspace.h
   BKE_crazyspace.hh
   BKE_cryptomatte.h
diff --git a/source/blender/blenkernel/intern/cpp_types.cc b/source/blender/blenkernel/intern/cpp_types.cc
new file mode 100644
index 00000000000..e6b33dd5b1a
--- /dev/null
+++ b/source/blender/blenkernel/intern/cpp_types.cc
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "BLI_cpp_type_make.hh"
+#include "BLI_cpp_types_make.hh"
+
+#include "BKE_cpp_types.h"
+#include "BKE_geometry_set.hh"
+#include "BKE_instances.hh"
+
+#include "DNA_meshdata_types.h"
+
+#include "FN_init.h"
+
+struct Tex;
+struct Image;
+struct Material;
+
+BLI_CPP_TYPE_MAKE(GeometrySet, CPPTypeFlags::Printable);
+BLI_CPP_TYPE_MAKE(blender::bke::InstanceReference, CPPTypeFlags::None)
+
+BLI_VECTOR_CPP_TYPE_MAKE(GeometrySet);
+
+BLI_CPP_TYPE_MAKE(Object *, CPPTypeFlags::BasicType)
+BLI_CPP_TYPE_MAKE(Collection *, CPPTypeFlags::BasicType)
+BLI_CPP_TYPE_MAKE(Tex *, CPPTypeFlags::BasicType)
+BLI_CPP_TYPE_MAKE(Image *, CPPTypeFlags::BasicType)
+BLI_CPP_TYPE_MAKE(Material *, CPPTypeFlags::BasicType)
+
+BLI_CPP_TYPE_MAKE(MStringProperty, CPPTypeFlags::None);
+
+void BKE_cpp_types_init()
+{
+  blender::register_cpp_types();
+  FN_register_cpp_types();
+
+  BLI_CPP_TYPE_REGISTER(GeometrySet);
+  BLI_CPP_TYPE_REGISTER(blender::bke::InstanceReference);
+
+  BLI_VECTOR_CPP_TYPE_REGISTER(GeometrySet);
+
+  BLI_CPP_TYPE_REGISTER(Object *);
+  BLI_CPP_TYPE_REGISTER(Collection *);
+  BLI_CPP_TYPE_REGISTER(Tex *);
+  BLI_CPP_TYPE_REGISTER(Image *);
+  BLI_CPP_TYPE_REGISTER(Material *);
+
+  BLI_CPP_TYPE_REGISTER(MStringProperty);
+}
diff --git a/source/blender/blenkernel/intern/customdata.cc b/source/blender/blenkernel/intern/customdata.cc
index bccb625feb2..c8d7c4f2fdc 100644
--- a/source/blender/blenkernel/intern/customdata.cc
+++ b/source/blender/blenkernel/intern/customdata.cc
@@ -19,7 +19,6 @@
 
 #include "BLI_bitmap.h"
 #include "BLI_color.hh"
-#include "BLI_cpp_type_make.hh"
 #include "BLI_endian_switch.h"
 #include "BLI_index_range.hh"
 #include "BLI_math.h"
@@ -5407,5 +5406,3 @@ size_t CustomData_get_elem_size(CustomDataLayer *layer)
 {
   return LAYERTYPEINFO[layer->type].size;
 }
-
-BLI_CPP_TYPE_MAKE(MStringProperty, MStringProperty, CPPTypeFlags::None);
diff --git a/source/blender/blenkernel/intern/instances.cc b/source/blender/blenkernel/intern/instances.cc
index 4675562e927..a2c344e918b 100644
--- a/source/blender/blenkernel/intern/instances.cc
+++ b/source/blender/blenkernel/intern/instances.cc
@@ -1,7 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
 
 #include "BLI_array_utils.hh"
-#include "BLI_cpp_type_make.hh"
 #include "BLI_rand.hh"
 #include "BLI_task.hh"
 
@@ -9,8 +8,6 @@
 #include "BKE_geometry_set.hh"
 #include "BKE_instances.hh"
 
-BLI_CPP_TYPE_MAKE(InstanceReference, blender::bke::InstanceReference, CPPTypeFlags::None)
-
 namespace blender::bke {
 
 InstanceReference::InstanceReference(GeometrySet geometry_set)
diff --git a/source/blender/blenlib/BLI_cpp_type.hh b/source/blender/blenlib/BLI_cpp_type.hh
index 568ccbb5a64..2f61583589b 100644
--- a/source/blender/blenlib/BLI_cpp_type.hh
+++ b/source/blender/blenlib/BLI_cpp_type.hh
@@ -74,6 +74,7 @@
 #include "BLI_index_mask.hh"
 #include "BLI_map.hh"
 #include "BLI_math_base.h"
+#include "BLI_parameter_pack_utils.hh"
 #include "BLI_string_ref.hh"
 #include "BLI_utility_mixins.hh"
 
@@ -94,10 +95,6 @@ ENUM_OPERATORS(CPPTypeFlags, CPPTypeFlags::EqualityComparable)
 
 namespace blender {
 
-/** Utility class to pass template parameters to constructor of `CPPType`. */
-template<typename T, CPPTypeFlags Flags> struct CPPTypeParam {
-};
-
 class CPPType : NonCopyable, NonMovable {
  private:
   int64_t size_ = 0;
@@ -148,7 +145,8 @@ class CPPType : NonCopyable, NonMovable {
   std::string debug_name_;
 
  public:
-  template<typename T, CPPTypeFlags Flags> CPPType(CPPTypeParam<T, Flags>, StringRef debug_name);
+  template<typename T, CPPTypeFlags Flags>
+  CPPType(TypeTag<T> /*type*/, TypeForValue<CPPTypeFlags, Flags> /*flags*/, StringRef debug_name);
   virtual ~CPPType() = default;
 
   /**
@@ -173,7 +171,7 @@ class CPPType : NonCopyable, NonMovable {
   template<typename T> static const CPPType &get()
   {
     /* Store the #CPPType locally to avoid making the function call in most cases. */
-    static const CPPType &type = CPPType::get_impl<std::remove_cv_t<T>>();
+    static const CPPType &type = CPPType::get_impl<std::decay_t<T>>();
     return type;
   }
   template<typename T> static const CPPType &get_impl();
@@ -745,30 +743,26 @@ class CPPType : NonCopyable, NonMovable {
     }
   }
 
-  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>{});
+      fn(TypeTag<T>{});
     }
 
     void operator()() const
     {
-      fn(type_tag<void>{});
+      fn(TypeTag<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;`
+   * expected to take a single `auto TypeTag` parameter. To extract the static type, use:
+   * `using T = typename decltype(TypeTag)::type;`
    *
    * If the current #CPPType is not in #Types, the type tag is `void`.
    */
@@ -779,6 +773,11 @@ class CPPType : NonCopyable, NonMovable {
   }
 };
 
+/**
+ * Initialize and register basic cpp types.
+ */
+void register_cpp_types();
+
 }  // namespace blender
 
 /* Utility for allocating an uninitialized buffer for a single value of the given #CPPType. */
diff --git a/source/blender/blenlib/BLI_cpp_type_make.hh b/source/blender/blenlib/BLI_cpp_type_make.hh
index 1f494624821..725e73dbb5d 100644
--- a/source/blender/blenlib/BLI_cpp_type_make.hh
+++ b/source/blender/blenlib/BLI_cpp_type_make.hh
@@ -206,7 +206,9 @@ template<typename T> uint64_t hash_cb(const void *value)
 namespace blender {
 
 template<typename T, CPPTypeFlags Flags>
-CPPType::CPPType(CPPTypeParam<T, Flags> /* unused */, StringRef debug_name)
+CPPType::CPPType(TypeTag<T> /*type*/,
+                 TypeForValue<CPPTypeFlags, Flags> /*flags*/,
+                 const StringRef debug_name)
 {
   using namespace cpp_type_util;
 
@@ -278,9 +280,15 @@ CPPType::CPPType(CPPTypeParam<T, Flags> /* unused */, StringRef debug_name)
 
 }  // namespace blender
 
-#define BLI_CPP_TYPE_MAKE(IDENTIFIER, TYPE_NAME, FLAGS) \
+/** Create a new #CPPType that can be accessed through `CPPType::get<T>()`. */
+#define BLI_CPP_TYPE_MAKE(TYPE_NAME, FLAGS) \
   template<> const blender::CPPType &blender::CPPType::get_impl<TYPE_NAME>() \
   { \
-    static CPPType cpp_type{blender::CPPTypeParam<TYPE_NAME, FLAGS>(), STRINGIFY(IDENTIFIER)}; \
-    return cpp_type; \
+    static CPPType type{blender::TypeTag<TYPE_NAME>(), \
+                        TypeForValue<CPPTypeFlags, FLAGS>(), \
+                        STRINGIFY(TYPE_NAME)}; \
+    return type; \
   }
+
+/** Register a #CPPType created with #BLI_CPP_TYPE_MAKE. */
+#define BLI_CPP_TYPE_REGISTER(TYPE_NAME) blender::CPPType::get<TYPE_NAME>()
diff --git a/source/blender/blenlib/BLI_cpp_types.hh b/source/blender/blenlib/BLI_cpp_types.hh
new file mode 100644
index 00000000000..596090b95e5
--- /dev/null
+++ b/source/blender/blenlib/BLI_cpp_types.hh
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#pragma once
+
+#include "BLI_cpp_type.hh"
+#include "BLI_vector.hh"
+
+namespace blender {
+
+/**
+ * Contains information about how to deal with a #Vector<T> generically.
+ */
+class VectorCPPType {
+ public:
+  /** The #Vector<T> itself. */
+  const CPPType &self;
+  /** The type stored in the vector. */
+  const CPPType &value;
+
+  template<typename ValueType> VectorCPPType(TypeTag<ValueType> /*value_type*/);
+
+  /**
+   * Try to find the #VectorCPPType that corresponds to a #CPPType.
+   */
+  static const VectorCPPType *get_from_self(const CPPType &self);
+  /**
+   * Try to find the #VectorCPPType that wraps a vector containing the given value type.
+   * This only works when the vector type has been created with #BLI_VECTOR_CPP_TYPE_MAKE.
+   */
+  static const VectorCPPType *get_from_value(const CPPType &value);
+
+  template<typename ValueType> static const VectorCPPType &get()
+  {
+    static const VectorCPPType &type = VectorCPPType::get_impl<std::decay_t<ValueType>>();
+    return type;
+  }
+
+  template<typename ValueType> static const VectorCPPType &get_impl();
+
+ private:
+  void register_self();
+};
+
+}  // namespace blender
diff --git a/source/blender/blenlib/BLI_cpp_types_make.hh b/source/blender/blenlib/BLI_cpp_types_make.hh
new file mode 100644
index 00000000000..ba729c4cc69
--- /dev/null
+++ b/source/blender/blenlib/BLI_cpp_types_make.hh
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#pragma once
+
+#include "BLI_cpp_type_make.hh"
+#include "BLI_cpp_types.hh"
+
+namespace blender {
+
+template<typename ValueType>
+inline VectorCPPType::VectorCPPType(TypeTag<ValueType> /*value_type*/)
+    : self(CPPType::get<Vector<ValueType>>()), va

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list