[Bf-blender-cvs] [1f1dc4ade30] temp-geometry-nodes-fields--fields-jacques: field cpp type

Jacques Lucke noreply at git.blender.org
Tue Aug 31 13:22:56 CEST 2021


Commit: 1f1dc4ade307446a9bde561eb478587bdd01fcb2
Author: Jacques Lucke
Date:   Tue Aug 31 10:22:43 2021 +0200
Branches: temp-geometry-nodes-fields--fields-jacques
https://developer.blender.org/rB1f1dc4ade307446a9bde561eb478587bdd01fcb2

field cpp type

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

M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/FN_field.hh
A	source/blender/functions/FN_field_cpp_type.hh
M	source/blender/functions/intern/cpp_types.cc

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

diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index 5941034de78..3c27e9d5e19 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -41,6 +41,7 @@ set(SRC
   FN_cpp_type.hh
   FN_cpp_type_make.hh
   FN_field.hh
+  FN_field_cpp_type.hh
   FN_generic_pointer.hh
   FN_generic_span.hh
   FN_generic_value_map.hh
diff --git a/source/blender/functions/FN_field.hh b/source/blender/functions/FN_field.hh
index 18a4c6a7734..7327ec7a499 100644
--- a/source/blender/functions/FN_field.hh
+++ b/source/blender/functions/FN_field.hh
@@ -109,14 +109,20 @@ template<typename T> class Field {
   GField field_;
 
  public:
-  Field(GField field) : field_(field)
+  Field(GField field) : field_(std::move(field))
   {
+    BLI_assert(field_.cpp_type().is<T>());
   }
 
   const GField *operator->() const
   {
     return &field_;
   }
+
+  const GField &operator*() const
+  {
+    return field_;
+  }
 };
 
 /**
diff --git a/source/blender/functions/FN_field_cpp_type.hh b/source/blender/functions/FN_field_cpp_type.hh
new file mode 100644
index 00000000000..b7eff8f8cf0
--- /dev/null
+++ b/source/blender/functions/FN_field_cpp_type.hh
@@ -0,0 +1,73 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#pragma once
+
+/** \file
+ * \ingroup fn
+ */
+
+#include "FN_cpp_type_make.hh"
+#include "FN_field.hh"
+
+namespace blender::fn {
+
+template<typename T> struct FieldCPPTypeParam {
+};
+
+class FieldCPPType : public CPPType {
+ private:
+  const CPPType &field_type_;
+
+ public:
+  template<typename T>
+  FieldCPPType(FieldCPPTypeParam<Field<T>> /* unused */, StringRef debug_name)
+      : CPPType(CPPTypeParam<Field<T>, CPPTypeFlags::None>(), debug_name),
+        field_type_(CPPType::get<T>())
+  {
+  }
+
+  const CPPType &field_type() const
+  {
+    return field_type_;
+  }
+
+  /* Ensure that a #GField and #Field<T> have the same layout, so pointers can be cast between the
+   * two. */
+  static_assert(sizeof(Field<int>) == sizeof(GField));
+  static_assert(sizeof(Field<int>) == sizeof(Field<std::string>));
+
+  const GField &get_gfield(const void *field) const
+  {
+    return *(const GField *)field;
+  }
+
+  void construct_from_gfield(void *r_value, const GField &gfield) const
+  {
+    new (r_value) GField(gfield);
+  }
+};
+
+}  // namespace blender::fn
+
+#define MAKE_FIELD_CPP_TYPE(DEBUG_NAME, FIELD_TYPE) \
+  template<> \
+  const blender::fn::CPPType &blender::fn::CPPType::get_impl<blender::fn::Field<FIELD_TYPE>>() \
+  { \
+    static blender::fn::FieldCPPType cpp_type{ \
+        blender::fn::FieldCPPTypeParam<blender::fn::Field<FIELD_TYPE>>(), STRINGIFY(DEBUG_NAME)}; \
+    return cpp_type; \
+  }
diff --git a/source/blender/functions/intern/cpp_types.cc b/source/blender/functions/intern/cpp_types.cc
index 7be34d2a1bf..92ebfd5edf2 100644
--- a/source/blender/functions/intern/cpp_types.cc
+++ b/source/blender/functions/intern/cpp_types.cc
@@ -15,6 +15,7 @@
  */
 
 #include "FN_cpp_type_make.hh"
+#include "FN_field_cpp_type.hh"
 
 #include "BLI_color.hh"
 #include "BLI_float2.hh"
@@ -39,4 +40,11 @@ MAKE_CPP_TYPE(ColorGeometry4b, blender::ColorGeometry4b, CPPTypeFlags::BasicType
 
 MAKE_CPP_TYPE(string, std::string, CPPTypeFlags::BasicType)
 
+MAKE_FIELD_CPP_TYPE(FloatField, float);
+MAKE_FIELD_CPP_TYPE(Float2Field, float2);
+MAKE_FIELD_CPP_TYPE(Float3Field, float3);
+MAKE_FIELD_CPP_TYPE(ColorGeometry4fField, blender::ColorGeometry4f);
+MAKE_FIELD_CPP_TYPE(BoolField, bool);
+MAKE_FIELD_CPP_TYPE(Int32Field, int32_t);
+
 }  // namespace blender::fn



More information about the Bf-blender-cvs mailing list