[Bf-blender-cvs] [bdfc3e9a6bd] functions-experimental-refactor: GenericArrayRef type
Jacques Lucke
noreply at git.blender.org
Tue Oct 15 15:56:15 CEST 2019
Commit: bdfc3e9a6bd4cd3c278ef2a6e1e45b777e126c5a
Author: Jacques Lucke
Date: Sat Oct 5 13:43:18 2019 +0200
Branches: functions-experimental-refactor
https://developer.blender.org/rBbdfc3e9a6bd4cd3c278ef2a6e1e45b777e126c5a
GenericArrayRef type
===================================================================
A source/blender/blenkernel/BKE_generic_array_ref.h
M source/blender/blenkernel/BKE_type_cpp.h
M source/blender/blenkernel/CMakeLists.txt
A source/blender/blenkernel/intern/generic_array_ref.cc
A source/blender/blenkernel/intern/type_cpp.cc
M source/blender/blenkernel/intern/types_cpp.cc
===================================================================
diff --git a/source/blender/blenkernel/BKE_generic_array_ref.h b/source/blender/blenkernel/BKE_generic_array_ref.h
new file mode 100644
index 00000000000..2eaa7991e49
--- /dev/null
+++ b/source/blender/blenkernel/BKE_generic_array_ref.h
@@ -0,0 +1,62 @@
+#ifndef __BKE_GENERIC_ARRAY_REF_H__
+#define __BKE_GENERIC_ARRAY_REF_H__
+
+#include "BKE_type_cpp.h"
+#include "BKE_types_cpp.h"
+
+#include "BLI_array_ref.h"
+
+namespace BKE {
+
+using BLI::ArrayRef;
+
+class GenericArrayRef {
+ private:
+ const TypeCPP *m_type;
+ void *m_buffer;
+ uint m_size;
+
+ public:
+ GenericArrayRef(const TypeCPP *type) : GenericArrayRef(type, nullptr, 0)
+ {
+ }
+
+ GenericArrayRef(const TypeCPP *type, void *buffer, uint size)
+ : m_type(type), m_buffer(buffer), m_size(size)
+ {
+ BLI_assert(type != nullptr);
+ BLI_assert(buffer != nullptr || size == 0);
+ BLI_assert(type->pointer_has_valid_alignment(buffer));
+ }
+
+ uint size() const
+ {
+ return m_size;
+ }
+
+ template<typename T> ArrayRef<T> get_ref() const
+ {
+ BLI_assert(get_type_cpp<T>().is_same_or_generalization(m_type));
+ return ArrayRef<T>((const T *)m_buffer, m_size);
+ }
+};
+
+class ArrayRefTypeCPP : public TypeCPP {
+ private:
+ TypeCPP &m_base_type;
+
+ public:
+ ArrayRefTypeCPP(TypeCPP &base_type, TypeCPP &generalization);
+
+ static void ConstructDefaultCB(const TypeCPP *self, void *ptr)
+ {
+ const ArrayRefTypeCPP *self_ = dynamic_cast<const ArrayRefTypeCPP *>(self);
+ new (ptr) GenericArrayRef(&self_->m_base_type);
+ }
+};
+
+ArrayRefTypeCPP &get_generic_array_ref_cpp_type(TypeCPP &base);
+
+} // namespace BKE
+
+#endif /* __BKE_GENERIC_ARRAY_REF_H__ */
\ No newline at end of file
diff --git a/source/blender/blenkernel/BKE_type_cpp.h b/source/blender/blenkernel/BKE_type_cpp.h
index def1077e2b5..c68dceda287 100644
--- a/source/blender/blenkernel/BKE_type_cpp.h
+++ b/source/blender/blenkernel/BKE_type_cpp.h
@@ -10,9 +10,9 @@ namespace BKE {
using BLI::StringRef;
using BLI::StringRefNull;
-class TypeCPP final {
+class TypeCPP {
public:
- using ConstructDefaultF = void (*)(void *ptr);
+ using ConstructDefaultF = void (*)(const TypeCPP *self, void *ptr);
using DestructF = void (*)(void *ptr);
using CopyToInitializedF = void (*)(void *src, void *dst);
using CopyToUninitializedF = void (*)(void *src, void *dst);
@@ -28,7 +28,8 @@ class TypeCPP final {
CopyToInitializedF copy_to_initialized,
CopyToUninitializedF copy_to_uninitialized,
RelocateToInitializedF relocate_to_initialized,
- RelocateToUninitializedF relocate_to_uninitialized)
+ RelocateToUninitializedF relocate_to_uninitialized,
+ TypeCPP *generalization)
: m_size(size),
m_alignment(alignment),
m_trivially_destructible(trivially_destructible),
@@ -38,13 +39,33 @@ class TypeCPP final {
m_copy_to_uninitialized(copy_to_uninitialized),
m_relocate_to_initialized(relocate_to_initialized),
m_relocate_to_uninitialized(relocate_to_uninitialized),
+ m_generalization(generalization),
m_name(name)
{
BLI_assert(is_power_of_2_i(m_alignment));
+ BLI_assert(generalization == nullptr ||
+ (generalization->size() == size && generalization->alignment() <= alignment));
m_alignment_mask = m_alignment - 1;
}
+ TypeCPP(std::string name, TypeCPP &generalization, ConstructDefaultF construct_default)
+ : TypeCPP(std::move(name),
+ generalization.m_size,
+ generalization.m_alignment,
+ generalization.m_trivially_destructible,
+ construct_default,
+ generalization.m_destruct,
+ generalization.m_copy_to_initialized,
+ generalization.m_copy_to_uninitialized,
+ generalization.m_relocate_to_initialized,
+ generalization.m_relocate_to_uninitialized,
+ &generalization)
+ {
+ }
+
+ virtual ~TypeCPP();
+
StringRefNull name() const
{
return m_name;
@@ -60,6 +81,11 @@ class TypeCPP final {
return m_alignment;
}
+ TypeCPP *generalization() const
+ {
+ return m_generalization;
+ }
+
bool trivially_destructible() const
{
return m_trivially_destructible;
@@ -74,7 +100,7 @@ class TypeCPP final {
{
BLI_assert(this->pointer_has_valid_alignment(ptr));
- m_construct_default(ptr);
+ m_construct_default(this, ptr);
}
void destruct(void *ptr) const
@@ -116,6 +142,17 @@ class TypeCPP final {
m_relocate_to_uninitialized(src, dst);
}
+ bool is_same_or_generalization(const TypeCPP &other) const
+ {
+ if (&other == this) {
+ return true;
+ }
+ if (m_generalization == nullptr) {
+ return false;
+ }
+ return m_generalization->is_same_or_generalization(other);
+ }
+
private:
uint m_size;
uint m_alignment;
@@ -127,6 +164,7 @@ class TypeCPP final {
CopyToUninitializedF m_copy_to_uninitialized;
RelocateToInitializedF m_relocate_to_initialized;
RelocateToUninitializedF m_relocate_to_uninitialized;
+ TypeCPP *m_generalization;
std::string m_name;
};
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 1649fb3afd9..cc3301bcc29 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -120,6 +120,7 @@ set(SRC
intern/fmodifier.c
intern/font.c
intern/freestyle.c
+ intern/generic_array_ref.cc
intern/gpencil.c
intern/gpencil_modifier.c
intern/icons.c
@@ -232,6 +233,7 @@ set(SRC
intern/tracking_solver.c
intern/tracking_stabilize.c
intern/tracking_util.c
+ intern/type_cpp.cc
intern/types_cpp.cc
intern/undo_system.c
intern/unit.c
diff --git a/source/blender/blenkernel/intern/generic_array_ref.cc b/source/blender/blenkernel/intern/generic_array_ref.cc
new file mode 100644
index 00000000000..00ed0ddcb30
--- /dev/null
+++ b/source/blender/blenkernel/intern/generic_array_ref.cc
@@ -0,0 +1,42 @@
+#include <mutex>
+
+#include "BLI_lazy_init_cxx.h"
+#include "BLI_map.h"
+
+#include "BKE_generic_array_ref.h"
+
+namespace BKE {
+
+using BLI::Map;
+
+using TypeMapping = Map<const TypeCPP *, std::unique_ptr<ArrayRefTypeCPP>>;
+
+BLI_LAZY_INIT_STATIC(TypeMapping, get_mapping)
+{
+ return TypeMapping{};
+}
+
+static std::mutex mapping_mutex;
+
+ArrayRefTypeCPP &get_generic_array_ref_cpp_type(TypeCPP &base)
+{
+ std::lock_guard<std::mutex> lock(mapping_mutex);
+
+ TypeMapping &mapping = get_mapping();
+ auto &type = mapping.lookup_or_add(&base, [&]() {
+ TypeCPP &generalization = get_type_cpp<GenericArrayRef>();
+ ArrayRefTypeCPP *new_type = new ArrayRefTypeCPP(base, generalization);
+ return std::unique_ptr<ArrayRefTypeCPP>(new_type);
+ });
+ return *type;
+}
+
+ArrayRefTypeCPP::ArrayRefTypeCPP(TypeCPP &base_type, TypeCPP &generalization)
+ : TypeCPP("GenericArrayRef for " + base_type.name(),
+ generalization,
+ ArrayRefTypeCPP::ConstructDefaultCB),
+ m_base_type(base_type)
+{
+}
+
+} // namespace BKE
\ No newline at end of file
diff --git a/source/blender/blenkernel/intern/type_cpp.cc b/source/blender/blenkernel/intern/type_cpp.cc
new file mode 100644
index 00000000000..c0c8b191c6a
--- /dev/null
+++ b/source/blender/blenkernel/intern/type_cpp.cc
@@ -0,0 +1,9 @@
+#include "BKE_type_cpp.h"
+
+namespace BKE {
+
+TypeCPP::~TypeCPP()
+{
+}
+
+} // namespace BKE
\ No newline at end of file
diff --git a/source/blender/blenkernel/intern/types_cpp.cc b/source/blender/blenkernel/intern/types_cpp.cc
index 32196fd5ac7..58555a94ce3 100644
--- a/source/blender/blenkernel/intern/types_cpp.cc
+++ b/source/blender/blenkernel/intern/types_cpp.cc
@@ -1,4 +1,5 @@
#include "BKE_types_cpp.h"
+#include "BKE_generic_array_ref.h"
#include "DNA_object_types.h"
@@ -18,10 +19,25 @@ void free_data_types()
}
}
-template<typename T> void ConstructDefault_CB(void *ptr)
+template<typename T> void ConstructDefault_CB(const TypeCPP *UNUSED(self), void *ptr)
{
BLI::construct_default((T *)ptr);
}
+
+template<typename T, bool IsDefaultConstructible> struct DefaultConstructor;
+template<typename T> struct DefaultConstructor<T, true> {
+ static TypeCPP::ConstructDefaultF get_callback()
+ {
+ return ConstructDefault_CB<T>;
+ }
+};
+template<typename T> struct DefaultConstructor<T, false> {
+ static TypeCPP::ConstructDefaultF get_callback()
+ {
+ return nullptr;
+ }
+};
+
template<typename T> void Destruct_CB(void *ptr)
{
BLI::destruct((T *)ptr);
@@ -51,6 +67,7 @@ CPP_TYPE_DECLARE(ObjectPtr);
CPP_TYPE_DECLARE(int32);
CPP_TYPE_DECLARE(rgba_f);
CPP_TYPE_DECLARE(string);
+CPP_TYPE_DECLARE(GenericArrayRef);
#undef CPP_TYPE_DECLARE
@@ -58,16 +75,19 @@ void init_data_types()
{
#define CPP_TYPE_CONSTRUCTION(IDENTIFIER, TYPE_NAME) \
- TYPE_##IDENTIFIER = new TypeCPP(STRINGIFY(IDENTIFIER), \
- sizeof(TYPE_NAME), \
- alignof(TYPE_NAME), \
- std::is_trivially_destructible<TYPE_NAME>::value, \
- ConstructDefault_CB<TYPE_NAME>, \
- Destruct_CB<TYPE_NAME>, \
- CopyToInitialized_CB<TYPE_NAME>, \
- CopyToUninitialized_CB<TYPE_NAME>, \
- RelocateToInitialized_CB<TYPE_NAME>, \
- RelocateToUninitialized_CB<TYPE_NAME>); \
+ TYPE_##IDENTIFIER = new TypeCPP( \
+ STRINGIFY(IDENTIFIER), \
+ sizeof(TYPE_NAME), \
+ alignof(TYPE_NAME), \
+ std::is_trivially_destructible<TYPE_NAME>::value, \
+ DefaultConstructor<TYPE_NAME, \
+ std::is_default_constructible<TYPE_NAME>::value>::get_callback(), \
+ Destruct_CB<TYPE_NAME>, \
+ CopyToInitialized_CB<TYPE_NAME>, \
+ CopyToUninitialized_CB<TYPE_NAME>, \
+ RelocateToInitialized_CB<TYPE_NAME>, \
+ RelocateToUninitialized_CB<TYPE_NAME>, \
+ nullptr); \
allocated_types.append(TYPE_##IDENTIFIER)
CPP_TYPE_CONSTRUCTION(float, float);
@@ -76,6 +96,7
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list