[Bf-blender-cvs] [302e1e9a86f] functions-experimental-refactor: initial cpp type

Jacques Lucke noreply at git.blender.org
Tue Oct 15 15:56:08 CEST 2019


Commit: 302e1e9a86fc27eb9d851d397b6af16ff6cf4e8b
Author: Jacques Lucke
Date:   Tue Oct 1 20:10:08 2019 +0200
Branches: functions-experimental-refactor
https://developer.blender.org/rB302e1e9a86fc27eb9d851d397b6af16ff6cf4e8b

initial cpp type

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

A	source/blender/blenkernel/BKE_type_cpp.h
A	source/blender/blenkernel/BKE_types_cpp.h
M	source/blender/blenkernel/CMakeLists.txt
A	source/blender/blenkernel/intern/types_cpp.cc
M	source/blender/blenlib/BLI_memory_utils_cxx.h

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

diff --git a/source/blender/blenkernel/BKE_type_cpp.h b/source/blender/blenkernel/BKE_type_cpp.h
new file mode 100644
index 00000000000..4c183ddc29c
--- /dev/null
+++ b/source/blender/blenkernel/BKE_type_cpp.h
@@ -0,0 +1,86 @@
+#ifndef __BKE_DATA_TYPE_H__
+#define __BKE_DATA_TYPE_H__
+
+#include "BLI_string_ref.h"
+#include "BLI_utility_mixins.h"
+#include "BLI_vector.h"
+
+namespace BKE {
+
+using BLI::StringRef;
+using BLI::StringRefNull;
+
+class TypeCPP final {
+ public:
+  using ConstructDefaultF = void (*)(void *ptr);
+  using DestructF = void (*)(void *ptr);
+  using CopyToInitializedF = void (*)(void *src, void *dst);
+  using CopyToUninitializedF = void (*)(void *src, void *dst);
+  using RelocateToInitializedF = void (*)(void *src, void *dst);
+  using RelocateToUninitializedF = void (*)(void *src, void *dst);
+
+  TypeCPP(std::string name,
+          ConstructDefaultF construct_default,
+          DestructF destruct,
+          CopyToInitializedF copy_to_initialized,
+          CopyToUninitializedF copy_to_uninitialized,
+          RelocateToInitializedF relocate_to_initialized,
+          RelocateToUninitializedF relocate_to_uninitialized)
+      : m_construct_default(construct_default),
+        m_destruct(destruct),
+        m_copy_to_initialized(copy_to_initialized),
+        m_copy_to_uninitialized(copy_to_uninitialized),
+        m_relocate_to_initialized(relocate_to_initialized),
+        m_relocate_to_uninitialized(relocate_to_uninitialized),
+        m_name(name)
+  {
+  }
+
+  StringRefNull name() const
+  {
+    return m_name;
+  }
+
+  void construct_default(void *ptr) const
+  {
+    m_construct_default(ptr);
+  }
+
+  void destruct(void *ptr) const
+  {
+    m_destruct(ptr);
+  }
+
+  void copy_to_initialized(void *src, void *dst) const
+  {
+    m_copy_to_initialized(src, dst);
+  }
+
+  void copy_to_uninitialized(void *src, void *dst) const
+  {
+    m_copy_to_uninitialized(src, dst);
+  }
+
+  void relocate_to_initialized(void *src, void *dst) const
+  {
+    m_relocate_to_initialized(src, dst);
+  }
+
+  void relocate_to_uninitialized(void *src, void *dst) const
+  {
+    m_relocate_to_uninitialized(src, dst);
+  }
+
+ private:
+  ConstructDefaultF m_construct_default;
+  DestructF m_destruct;
+  CopyToInitializedF m_copy_to_initialized;
+  CopyToUninitializedF m_copy_to_uninitialized;
+  RelocateToInitializedF m_relocate_to_initialized;
+  RelocateToUninitializedF m_relocate_to_uninitialized;
+  std::string m_name;
+};
+
+}  // namespace BKE
+
+#endif /* __BKE_DATA_TYPE_H__ */
diff --git a/source/blender/blenkernel/BKE_types_cpp.h b/source/blender/blenkernel/BKE_types_cpp.h
new file mode 100644
index 00000000000..4f5c97c1997
--- /dev/null
+++ b/source/blender/blenkernel/BKE_types_cpp.h
@@ -0,0 +1,15 @@
+#ifndef __BKE_DATA_TYPES_H__
+#define __BKE_DATA_TYPES_H__
+
+#include "BKE_type_cpp.h"
+
+namespace BKE {
+
+void init_data_types();
+void free_data_types();
+
+template<typename T> TypeCPP &get_type_cpp();
+
+}  // namespace BKE
+
+#endif /* __BKE_DATA_TYPES_H__ */
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index c702beee776..9b003f787af 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -231,6 +231,7 @@ set(SRC
   intern/tracking_solver.c
   intern/tracking_stabilize.c
   intern/tracking_util.c
+  intern/types_cpp.cc
   intern/undo_system.c
   intern/unit.c
   intern/workspace.c
diff --git a/source/blender/blenkernel/intern/types_cpp.cc b/source/blender/blenkernel/intern/types_cpp.cc
new file mode 100644
index 00000000000..242d6951650
--- /dev/null
+++ b/source/blender/blenkernel/intern/types_cpp.cc
@@ -0,0 +1,92 @@
+#include "BKE_types_cpp.h"
+
+#include "DNA_object_types.h"
+
+#include "BLI_math_cxx.h"
+
+namespace BKE {
+
+static Vector<TypeCPP *, 4, BLI::RawAllocator> allocated_types;
+
+void free_data_types()
+{
+  for (TypeCPP *type : allocated_types) {
+    delete type;
+  }
+}
+
+template<typename T> void ConstructDefault_CB(void *ptr)
+{
+  BLI::construct_default((T *)ptr);
+}
+template<typename T> void Destruct_CB(void *ptr)
+{
+  BLI::destruct((T *)ptr);
+}
+template<typename T> void CopyToInitialized_CB(void *src, void *dst)
+{
+  *(T *)src = *(T *)dst;
+}
+template<typename T> void CopyToUninitialized_CB(void *src, void *dst)
+{
+  BLI::uninitialized_copy_n((T *)src, 1, (T *)dst);
+}
+template<typename T> void RelocateToInitialized_CB(void *src, void *dst)
+{
+  BLI::relocate((T *)src, (T *)dst);
+}
+template<typename T> void RelocateToUninitialized_CB(void *src, void *dst)
+{
+  BLI::uninitialized_relocate((T *)src, (T *)dst);
+}
+
+#define CPP_TYPE_DECLARE(IDENTIFIER) static TypeCPP *TYPE_##IDENTIFIER = nullptr
+
+CPP_TYPE_DECLARE(float);
+CPP_TYPE_DECLARE(bool);
+CPP_TYPE_DECLARE(ObjectPtr);
+CPP_TYPE_DECLARE(int32);
+CPP_TYPE_DECLARE(rgba_f);
+CPP_TYPE_DECLARE(string);
+
+#undef CPP_TYPE_DECLARE
+
+void init_data_types()
+{
+
+#define CPP_TYPE_CONSTRUCTION(IDENTIFIER, TYPE_NAME) \
+  TYPE_##IDENTIFIER = new TypeCPP(STRINGIFY(IDENTIFIER), \
+                                  ConstructDefault_CB<TYPE_NAME>, \
+                                  Destruct_CB<TYPE_NAME>, \
+                                  CopyToInitialized_CB<TYPE_NAME>, \
+                                  CopyToUninitialized_CB<TYPE_NAME>, \
+                                  RelocateToInitialized_CB<TYPE_NAME>, \
+                                  RelocateToUninitialized_CB<TYPE_NAME>); \
+  allocated_types.append(TYPE_##IDENTIFIER)
+
+  CPP_TYPE_CONSTRUCTION(float, float);
+  CPP_TYPE_CONSTRUCTION(bool, bool);
+  CPP_TYPE_CONSTRUCTION(ObjectPtr, Object *);
+  CPP_TYPE_CONSTRUCTION(int32, int32_t);
+  CPP_TYPE_CONSTRUCTION(rgba_f, BLI::rgba_f);
+  CPP_TYPE_CONSTRUCTION(string, std::string);
+
+#undef CPP_TYPE_CONSTRUCTION
+}
+
+#define CPP_TYPE_GETTER(IDENTIFIER, TYPE_NAME) \
+  template<> TypeCPP &get_type_cpp<TYPE_NAME>() \
+  { \
+    return *TYPE_##IDENTIFIER; \
+  }
+
+CPP_TYPE_GETTER(float, float)
+CPP_TYPE_GETTER(bool, bool)
+CPP_TYPE_GETTER(ObjectPtr, Object *)
+CPP_TYPE_GETTER(int32, int32_t)
+CPP_TYPE_GETTER(rgba_f, BLI::rgba_f)
+CPP_TYPE_GETTER(string, std::string)
+
+#undef CPP_TYPE_GETTER
+
+}  // namespace BKE
\ No newline at end of file
diff --git a/source/blender/blenlib/BLI_memory_utils_cxx.h b/source/blender/blenlib/BLI_memory_utils_cxx.h
index c814b9dce3e..8e80524fc36 100644
--- a/source/blender/blenlib/BLI_memory_utils_cxx.h
+++ b/source/blender/blenlib/BLI_memory_utils_cxx.h
@@ -33,6 +33,11 @@ using std::uninitialized_copy_n;
 using std::uninitialized_fill;
 using std::uninitialized_fill_n;
 
+template<typename T> void construct_default(T *ptr)
+{
+  new (ptr) T();
+}
+
 template<typename T> void destruct(T *ptr)
 {
   ptr->~T();



More information about the Bf-blender-cvs mailing list