Commit: a1947b3f41b8d0fa55a770aaca62417d496ca238
Author: Jacques Lucke
Date:   Thu Feb 20 12:12:26 2020 +0100
Branches: simulation-tree
https://developer.blender.org/rBa1947b3f41b8d0fa55a770aaca62417d496ca238

initial socket init/copy/free interface

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

M       source/blender/blenkernel/BKE_node.h
M       source/blender/simulations/nodes/my_test_node.cc

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

diff --git a/source/blender/blenkernel/BKE_node.h 
b/source/blender/blenkernel/BKE_node.h
index ffc2150516e..6a2122ec8d7 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -139,6 +139,13 @@ typedef struct bNodeSocketType {
                                 struct bNode *node,
                                 struct bNodeSocket *sock);
 
+  void (*init_fn)(struct bNodeTree *ntree, struct bNode *node, struct 
bNodeSocket *socket);
+  void (*copy_fn)(struct bNodeTree *dst_ntree,
+                  struct bNode *dst_node,
+                  struct bNodeSocket *dst_socket,
+                  const struct bNodeSocket *src_socket);
+  void (*free_fn)(struct bNodeTree *ntree, struct bNode *node, struct 
bNodeSocket *socket);
+
   /* RNA integration */
   ExtensionRNA ext_socket;
   ExtensionRNA ext_interface;
diff --git a/source/blender/simulations/nodes/my_test_node.cc 
b/source/blender/simulations/nodes/my_test_node.cc
index c5340cf7054..7bd3414adf3 100644
--- a/source/blender/simulations/nodes/my_test_node.cc
+++ b/source/blender/simulations/nodes/my_test_node.cc
@@ -336,17 +336,26 @@ class SocketTypeDefinition {
                                           struct PointerRNA *ptr,
                                           struct PointerRNA *node_ptr,
                                           const char *text)>;
+  using InitStorageFn = std::function<void *()>;
+  using CopyStorageFn = std::function<void *(void *)>;
+  using FreeStorageFn = std::function<void(void *)>;
+  template<typename T> using TypedInitStorageFn = std::function<void(T *)>;
 
  private:
   bNodeSocketType m_stype;
   DrawInNodeFn m_draw_in_node_fn;
   rgba_f m_color = {0.0f, 0.0f, 0.0f, 1.0f};
+  std::string m_storage_struct_name;
+  InitStorageFn m_init_storage_fn;
+  CopyStorageFn m_copy_storage_fn;
+  FreeStorageFn m_free_storage_fn;
 
  public:
   SocketTypeDefinition(StringRef idname)
   {
+    memset(&m_stype, 0, sizeof(bNodeSocketType));
     idname.copy(m_stype.idname);
-
+    m_stype.type = SOCK_CUSTOM;
     m_stype.draw = SocketTypeDefinition::draw_in_node;
     m_draw_in_node_fn = [](struct bContext *UNUSED(C),
                            struct uiLayout *layout,
@@ -357,6 +366,30 @@ class SocketTypeDefinition {
     m_stype.draw_color = SocketTypeDefinition::get_draw_color;
     m_stype.free_self = [](bNodeSocketType *UNUSED(stype)) {};
 
+    m_init_storage_fn = []() { return nullptr; };
+    m_copy_storage_fn = [](void *storage) {
+      BLI_assert(storage == nullptr);
+      UNUSED_VARS_NDEBUG(storage);
+      return nullptr;
+    };
+    m_free_storage_fn = [](void *storage) {
+      BLI_assert(storage == nullptr);
+      UNUSED_VARS_NDEBUG(storage);
+    };
+
+    m_stype.init_fn = [](bNodeTree *UNUSED(ntree), bNode *UNUSED(node), 
bNodeSocket *socket) {
+      std::cout << "Init: " << socket->name << '\n';
+    };
+    m_stype.copy_fn = [](bNodeTree *UNUSED(dst_ntree),
+                         bNode *UNUSED(dst_node),
+                         bNodeSocket *dst_socket,
+                         const bNodeSocket *src_socket) {
+      std::cout << "Copy: " << src_socket->name << " -> " << dst_socket->name 
<< '\n';
+    };
+    m_stype.free_fn = [](bNodeTree *UNUSED(ntree), bNode *UNUSED(node), 
bNodeSocket *socket) {
+      std::cout << "Free: " << socket->name << '\n';
+    };
+
     m_stype.userdata = (void *)this;
   }
 
@@ -365,6 +398,35 @@ class SocketTypeDefinition {
     m_color = color;
   }
 
+  void add_dna_storage(StringRef struct_name,
+                       InitStorageFn init_storage_fn,
+                       CopyStorageFn copy_storage_fn,
+                       FreeStorageFn free_storage_fn)
+  {
+    m_storage_struct_name = struct_name;
+    m_init_storage_fn = init_storage_fn;
+    m_copy_storage_fn = copy_storage_fn;
+    m_free_storage_fn = free_storage_fn;
+  }
+
+  template<typename T>
+  void add_dna_storage(StringRef struct_name, TypedInitStorageFn<T> 
init_storage_fn)
+  {
+    this->add_dna_storage(
+        struct_name,
+        [init_storage_fn]() {
+          void *buffer = MEM_callocN(sizeof(T), __func__);
+          init_storage_fn((T *)buffer);
+          return buffer;
+        },
+        [](void *buffer) {
+          void *new_buffer = MEM_callocN(sizeof(T), __func__);
+          memcpy(new_buffer, buffer, sizeof(T));
+          return new_buffer;
+        },
+        [](void *buffer) { MEM_freeN(buffer); });
+  }
+
   void register_type()
   {
     nodeRegisterSocketType(&m_stype);
@@ -659,6 +721,8 @@ void init_socket_data_types()
   {
     static SocketTypeDefinition stype("MyFloatSocket");
     stype.set_color({1, 1, 1, 1});
+    stype.add_dna_storage<bNodeSocketValueFloat>(
+        "bNodeSocketValueFloat", [](bNodeSocketValueFloat *storage) { 
storage->value = 11.5f; });
     stype.register_type();
   }

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to