[Bf-blender-cvs] [264719c0b4f] functions: simplify string type

Jacques Lucke noreply at git.blender.org
Fri Sep 6 16:55:57 CEST 2019


Commit: 264719c0b4f3fd5b9b1505a9f3a7fb576466b38e
Author: Jacques Lucke
Date:   Fri Sep 6 14:50:13 2019 +0200
Branches: functions
https://developer.blender.org/rB264719c0b4f3fd5b9b1505a9f3a7fb576466b38e

simplify string type

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

M	source/blender/functions/backends/cpp/cpp_type_info.hpp
M	source/blender/functions/backends/llvm/llvm_type_info.hpp
M	source/blender/functions/frontends/data_flow_nodes/mappings/socket_loaders.cpp
M	source/blender/functions/functions/string.cpp
M	source/blender/functions/types/string_type.cpp
M	source/blender/functions/types/string_type.hpp
M	source/blender/simulations/bparticles/node_frontend.cpp
M	source/blender/simulations/bparticles/particle_function_builder.cpp

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

diff --git a/source/blender/functions/backends/cpp/cpp_type_info.hpp b/source/blender/functions/backends/cpp/cpp_type_info.hpp
index 2211df83dd0..74d76f20f12 100644
--- a/source/blender/functions/backends/cpp/cpp_type_info.hpp
+++ b/source/blender/functions/backends/cpp/cpp_type_info.hpp
@@ -105,6 +105,11 @@ class CPPTypeInfo : public TypeExtension {
 #endif
 };
 
+/**
+ * This implements all necessary functions based on the default-constructor, copy-constructor,
+ * move-constructor and destructor of the given class. To get appropriate
+ * construction/copy/relocate/free semantics for pointer types, use one of the wrappers below.
+ */
 template<typename T> class CPPTypeInfoForType : public CPPTypeInfo {
  public:
   CPPTypeInfoForType()
@@ -220,6 +225,85 @@ template<typename T> class ReferencedPointerWrapper {
   }
 };
 
+template<typename T> class UniquePointerWrapper {
+ private:
+  T *m_ptr;
+
+ public:
+  UniquePointerWrapper() : m_ptr(nullptr)
+  {
+  }
+
+  UniquePointerWrapper(T *ptr) : m_ptr(ptr)
+  {
+  }
+
+  UniquePointerWrapper(const UniquePointerWrapper &other)
+  {
+    m_ptr = new T(other.ref());
+  }
+
+  UniquePointerWrapper(UniquePointerWrapper &&other)
+  {
+    m_ptr = other.m_ptr;
+    other.m_ptr = nullptr;
+  }
+
+  ~UniquePointerWrapper()
+  {
+    if (m_ptr != nullptr) {
+      delete m_ptr;
+    }
+  }
+
+  UniquePointerWrapper &operator=(const UniquePointerWrapper &other)
+  {
+    if (this == &other) {
+      return *this;
+    }
+
+    this->~UniquePointerWrapper();
+    new (this) UniquePointerWrapper(other);
+    return *this;
+  }
+
+  UniquePointerWrapper &operator=(UniquePointerWrapper &&other)
+  {
+    if (this == &other) {
+      return *this;
+    }
+
+    this->~UniquePointerWrapper();
+    new (this) UniquePointerWrapper(std::move(other));
+    return *this;
+  }
+
+  const T *ptr() const
+  {
+    return m_ptr;
+  }
+
+  T *ptr()
+  {
+    return m_ptr;
+  }
+
+  const T &ref() const
+  {
+    return *m_ptr;
+  }
+
+  T &ref()
+  {
+    return *m_ptr;
+  }
+
+  T *operator->()
+  {
+    return m_ptr;
+  }
+};
+
 /**
  * The class has to have a clone() method.
  */
@@ -252,6 +336,13 @@ template<typename T> class UniqueVirtualPointerWrapper {
     other.m_ptr = nullptr;
   }
 
+  ~UniqueVirtualPointerWrapper()
+  {
+    if (m_ptr != nullptr) {
+      delete m_ptr;
+    }
+  }
+
   UniqueVirtualPointerWrapper &operator=(const UniqueVirtualPointerWrapper &other)
   {
     if (this == &other) {
diff --git a/source/blender/functions/backends/llvm/llvm_type_info.hpp b/source/blender/functions/backends/llvm/llvm_type_info.hpp
index a8cb4f861e5..2f8b5b548c6 100644
--- a/source/blender/functions/backends/llvm/llvm_type_info.hpp
+++ b/source/blender/functions/backends/llvm/llvm_type_info.hpp
@@ -255,6 +255,23 @@ class UniqueVirtualPointerLLVMTypeInfo
   }
 };
 
+template<typename T>
+class UniquePointerLLVMTypeInfo : public OwnedPointerLLVMTypeInfo<UniquePointerLLVMTypeInfo<T>> {
+ public:
+  static T *copy_value(const T *value)
+  {
+    T *ptr = new T(*value);
+    return ptr;
+  }
+
+  static void free_value(T *value)
+  {
+    if (value != nullptr) {
+      delete value;
+    }
+  }
+};
+
 inline llvm::Type *get_llvm_type(Type *type, llvm::LLVMContext &context)
 {
   return type->extension<LLVMTypeInfo>().get_type(context);
diff --git a/source/blender/functions/frontends/data_flow_nodes/mappings/socket_loaders.cpp b/source/blender/functions/frontends/data_flow_nodes/mappings/socket_loaders.cpp
index e1ab74e491d..c66806aca53 100644
--- a/source/blender/functions/frontends/data_flow_nodes/mappings/socket_loaders.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/mappings/socket_loaders.cpp
@@ -57,7 +57,7 @@ static void LOAD_text(PointerRNA *rna, Tuple &tuple, uint index)
   int length = RNA_string_length(rna, "value");
   char *stack_str = (char *)alloca(length + 1);
   RNA_string_get(rna, "value", stack_str);
-  MyString str(stack_str);
+  StringW str(new std::string(stack_str));
   tuple.move_in(index, str);
 }
 
diff --git a/source/blender/functions/functions/string.cpp b/source/blender/functions/functions/string.cpp
index a366b844ef4..efe68f8e5a8 100644
--- a/source/blender/functions/functions/string.cpp
+++ b/source/blender/functions/functions/string.cpp
@@ -13,8 +13,8 @@ using namespace Types;
 class StringLength : public TupleCallBody {
   void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override
   {
-    MyString str = fn_in.relocate_out<MyString>(0);
-    int length = str.size();
+    StringW str = fn_in.relocate_out<StringW>(0);
+    int length = str->size();
     fn_out.set<int32_t>(0, length);
   }
 };
diff --git a/source/blender/functions/types/string_type.cpp b/source/blender/functions/types/string_type.cpp
index af6079d3665..3ea047d5d5e 100644
--- a/source/blender/functions/types/string_type.cpp
+++ b/source/blender/functions/types/string_type.cpp
@@ -11,88 +11,11 @@ namespace Types {
 Type *TYPE_string = nullptr;
 Type *TYPE_string_list = nullptr;
 
-class LLVMString : public LLVMTypeInfo {
- public:
-  llvm::Type *get_type(llvm::LLVMContext &context) const override
-  {
-    return llvm::Type::getInt8PtrTy(context);
-  }
-
-  static char *copy_string(char *str)
-  {
-    if (str == nullptr) {
-      return nullptr;
-    }
-    return (char *)MEM_dupallocN(str);
-  }
-
-  static void free_string(char *str)
-  {
-    if (str != nullptr) {
-      MEM_freeN(str);
-    }
-  }
-
-  static void store_relocate(char *str, void *address)
-  {
-    new (address) MyString(str);
-    free_string(str);
-  }
-
-  static void store_copy(char *str, void *address)
-  {
-    new (address) MyString(str);
-  }
-
-  llvm::Value *build_copy_ir(CodeBuilder &builder, llvm::Value *value) const override
-  {
-    return builder.CreateCallPointer(
-        (void *)copy_string, {value}, builder.getInt8PtrTy(), "copy string");
-  }
-
-  void build_free_ir(CodeBuilder &builder, llvm::Value *value) const override
-  {
-    builder.CreateCallPointer((void *)free_string, {value}, builder.getVoidTy(), "free string");
-  }
-
-  void build_store_ir__relocate(CodeBuilder &builder,
-                                llvm::Value *value,
-                                llvm::Value *address) const override
-  {
-    builder.CreateCallPointer(
-        (void *)store_relocate, {value, address}, builder.getVoidTy(), "store string relocate");
-  }
-
-  void build_store_ir__copy(CodeBuilder &builder,
-                            llvm::Value *value,
-                            llvm::Value *address) const override
-  {
-    builder.CreateCallPointer(
-        (void *)store_copy, {value, address}, builder.getVoidTy(), "store string copy");
-  }
-
-  llvm::Value *build_load_ir__relocate(CodeBuilder &builder, llvm::Value *address) const override
-  {
-    llvm::Value *data_address = builder.CastToPointerOf(address, builder.getInt8PtrTy());
-    llvm::Value *str = builder.CreateLoad(data_address);
-    builder.CreateStore(builder.getInt8Ptr(nullptr), data_address);
-    return str;
-  }
-
-  llvm::Value *build_load_ir__copy(CodeBuilder &builder, llvm::Value *address) const override
-  {
-    llvm::Value *data_address = builder.CastToPointerOf(address, builder.getInt8PtrTy());
-    llvm::Value *str = builder.CreateLoad(data_address);
-    llvm::Value *str_copy = this->build_copy_ir(builder, str);
-    return str_copy;
-  }
-};
-
 void INIT_string(Vector<Type *> &types_to_free)
 {
   TYPE_string = new Type("String");
-  TYPE_string->add_extension<CPPTypeInfoForType<MyString>>();
-  TYPE_string->add_extension<LLVMString>();
+  TYPE_string->add_extension<CPPTypeInfoForType<StringW>>();
+  TYPE_string->add_extension<UniquePointerLLVMTypeInfo<std::string>>();
 
   TYPE_string_list = new_list_type(TYPE_string);
 
diff --git a/source/blender/functions/types/string_type.hpp b/source/blender/functions/types/string_type.hpp
index 5133e159ad3..c6c3c7e5783 100644
--- a/source/blender/functions/types/string_type.hpp
+++ b/source/blender/functions/types/string_type.hpp
@@ -1,92 +1,12 @@
 #pragma once
 
 #include "FN_core.hpp"
+#include "FN_cpp.hpp"
 
 namespace FN {
 namespace Types {
 
-/* Still have to figure out a better way to handle strings. Calling it MyString for now until a
- * better name is found. std::string cannot easily be used because it would need special handling
- * as llvm type as well. */
-class MyString {
- private:
-  char *m_string;
-
- public:
-  MyString() : m_string(nullptr)
-  {
-  }
-
-  MyString(StringRef str_ref)
-  {
-    m_string = (char *)MEM_mallocN(str_ref.size() + 1, __func__);
-    str_ref.copy_to__with_null(m_string);
-  }
-
-  ~MyString()
-  {
-    if (m_string != nullptr) {
-      MEM_freeN(m_string);
-    }
-  }
-
-  MyString(const MyString &other) : MyString(StringRef(other))
-  {
-  }
-
-  MyString(MyString &&other)
-  {
-    m_string = other.m_string;
-    other.m_string = nullptr;
-  }
-
-  MyString &operator=(const MyString &other)
-  {
-    if (this == &other) {
-      return *this;
-    }
-
-    this->~MyString();
-    new (this) MyString(other);
-    return *this;
-  }
-
-  MyString &operator=(MyString &&other)
-  {
-    if (this == &other) {
-      return *this;
-    }
-
-    this->~MyString();
-    new (this) MyString(std::move(other));
-    return *this;
-  }
-
-  const char *data() const
-  {
-    return m_string;
-  }
-
-  operator StringRefNull() const
-  {
-    if (m_string == nullptr) {
-      return StringRefNull();
-    }
-    else {
-      return StringRefNull(m_string);
-    }
-  }
-
-  uint size() const
-  {
-    if (m_string == nullptr) {
-      return 0;
-    }
-    else {
-      return strlen(m_string);
-    }
-  }
-};
+using StringW = UniquePointerWrapper<std::string>;
 
 void INIT_string(Vector<Type *> &types_to_free);
 
diff --git a/source/blender/simulations/bparticles/node_frontend.cpp b/source/blender/simulations/bparticles/node_frontend.cpp
index e6f68d9594d..398a18325a4 100644
--- a/source/blender/simulations/bparticles/node_frontend.cpp
+++ b/source/blender/simulations/bparticles/node_frontend.cpp
@@ -25,6 +25,7 @@ using FN::FunctionGraph;
 using FN::SharedDataGraph;
 using FN::DataFlowNodes::VTreeDataGraph;
 using FN::Types::ObjectW;
+using FN::Types::

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list