[Bf-blender-cvs] [326878d82ce] functions-experimental-refactor: initial new function type
Jacques Lucke
noreply at git.blender.org
Tue Oct 15 15:56:18 CEST 2019
Commit: 326878d82ceb9a09e5350d56a5cd5cb8b231fe3e
Author: Jacques Lucke
Date: Sat Oct 5 20:25:46 2019 +0200
Branches: functions-experimental-refactor
https://developer.blender.org/rB326878d82ceb9a09e5350d56a5cd5cb8b231fe3e
initial new function type
===================================================================
A source/blender/blenkernel/BKE_function_cpp.h
M source/blender/blenkernel/BKE_tuple.h
M source/blender/blenkernel/CMakeLists.txt
A source/blender/blenkernel/intern/function_cpp.cc
===================================================================
diff --git a/source/blender/blenkernel/BKE_function_cpp.h b/source/blender/blenkernel/BKE_function_cpp.h
new file mode 100644
index 00000000000..508ba52da8e
--- /dev/null
+++ b/source/blender/blenkernel/BKE_function_cpp.h
@@ -0,0 +1,91 @@
+#ifndef __BKE_FUNCTION_CPP_H__
+#define __BKE_FUNCTION_CPP_H__
+
+#include "BKE_type_cpp.h"
+#include "BKE_tuple.h"
+
+#include "BLI_vector.h"
+#include "BLI_string_ref.h"
+
+namespace BKE {
+
+using BLI::StringRef;
+using BLI::StringRefNull;
+using BLI::Vector;
+
+class FunctionCPP;
+
+struct SignatureData {
+ std::string name;
+ Vector<TypeCPP *> input_types;
+ Vector<TypeCPP *> output_types;
+ Vector<std::string> input_names;
+ Vector<std::string> output_names;
+};
+
+class SignatureBuilderCPP {
+ private:
+ SignatureData m_data;
+
+ friend FunctionCPP;
+
+ public:
+ void set_name(StringRef name)
+ {
+ m_data.name = name;
+ }
+
+ void add_input(StringRef name, TypeCPP &type)
+ {
+ m_data.input_names.append(name);
+ m_data.input_types.append(&type);
+ }
+
+ void add_output(StringRef name, TypeCPP &type)
+ {
+ m_data.output_names.append(name);
+ m_data.input_types.append(&type);
+ }
+};
+
+class FunctionCPP {
+ private:
+ SignatureData m_signature;
+
+ public:
+ FunctionCPP();
+
+ virtual ~FunctionCPP();
+
+ StringRefNull name() const
+ {
+ return m_signature.name;
+ }
+
+ ArrayRef<TypeCPP *> input_types() const
+ {
+ return m_signature.input_types;
+ }
+
+ ArrayRef<TypeCPP *> output_types() const
+ {
+ return m_signature.output_types;
+ }
+
+ StringRefNull input_name(uint index) const
+ {
+ return m_signature.input_names[index];
+ }
+
+ StringRefNull output_name(uint index) const
+ {
+ return m_signature.output_names[index];
+ }
+
+ virtual void signature(SignatureBuilderCPP &signature) = 0;
+ virtual void call(TupleRef &fn_in, TupleRef &fn_out) const = 0;
+};
+
+} // namespace BKE
+
+#endif /* __BKE_FUNCTION_CPP_H__ */
\ No newline at end of file
diff --git a/source/blender/blenkernel/BKE_tuple.h b/source/blender/blenkernel/BKE_tuple.h
index 13ff792c654..cfbe3ab3ce0 100644
--- a/source/blender/blenkernel/BKE_tuple.h
+++ b/source/blender/blenkernel/BKE_tuple.h
@@ -125,7 +125,7 @@ class TupleRef {
BLI_assert(index < m_info->size());
BLI_assert(m_info->element_has_type<T>(index));
- T *dst = (T *)this->element_ptr<T>(index);
+ T *dst = (T *)this->element_ptr(index);
if (std::is_trivially_copyable<T>::value) {
std::memcpy(dst, &value, sizeof(T));
}
@@ -245,6 +245,12 @@ class TupleRef {
m_init[index] = false;
}
+ template<typename T> T get(uint index) const
+ {
+ BLI_STATIC_ASSERT(std::is_trivial<T>::value, "can only be used with trivial types");
+ return this->copy_out<T>(index);
+ }
+
template<typename T> T get_type_cpp(uint index) const
{
BLI_STATIC_ASSERT(std::is_trivial<T>::value, "can only be used with trivial types");
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index cc3301bcc29..7c3fea7e43c 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/function_cpp.cc
intern/generic_array_ref.cc
intern/gpencil.c
intern/gpencil_modifier.c
diff --git a/source/blender/blenkernel/intern/function_cpp.cc b/source/blender/blenkernel/intern/function_cpp.cc
new file mode 100644
index 00000000000..8457010a9c2
--- /dev/null
+++ b/source/blender/blenkernel/intern/function_cpp.cc
@@ -0,0 +1,53 @@
+#include "BKE_function_cpp.h"
+#include "BKE_types_cpp.h"
+#include "BKE_generic_array_ref.h"
+
+namespace BKE {
+
+FunctionCPP::FunctionCPP()
+{
+ SignatureBuilderCPP signature;
+ this->signature(signature);
+ m_signature = std::move(signature.m_data);
+}
+
+FunctionCPP::~FunctionCPP()
+{
+}
+
+class AddFloatsFunction : public FunctionCPP {
+ void signature(SignatureBuilderCPP &signature) override
+ {
+ signature.add_input("A", get_type_cpp<float>());
+ signature.add_input("B", get_type_cpp<float>());
+ signature.add_output("Result", get_type_cpp<float>());
+ }
+
+ void call(TupleRef &fn_in, TupleRef &fn_out) const override
+ {
+ float a = fn_in.get<float>(0);
+ float b = fn_in.get<float>(1);
+ float result = a + b;
+ fn_out.set<float>(0, result);
+ }
+};
+
+class AddFloatsArray : public FunctionCPP {
+ void signature(SignatureBuilderCPP &signature) override
+ {
+ signature.add_input("A", get_generic_array_ref_cpp_type(get_type_cpp<float>()));
+ signature.add_output("B", get_type_cpp<float>());
+ }
+
+ void call(TupleRef &fn_in, TupleRef &fn_out) const override
+ {
+ ArrayRef<float> values = fn_in.copy_out<GenericArrayRef>(0).get_ref<float>();
+ float sum = 0.0f;
+ for (float value : values) {
+ sum += value;
+ }
+ fn_out.set<float>(0, sum);
+ }
+};
+
+} // namespace BKE
\ No newline at end of file
More information about the Bf-blender-cvs
mailing list