[Bf-blender-cvs] [434e2afe3c4] functions: more generic dynamic composition of types and functions

Jacques Lucke noreply at git.blender.org
Sun Feb 10 20:25:49 CET 2019


Commit: 434e2afe3c4596b83ac7cc0211f2fbb745949a43
Author: Jacques Lucke
Date:   Thu Jan 31 17:49:37 2019 +0100
Branches: functions
https://developer.blender.org/rB434e2afe3c4596b83ac7cc0211f2fbb745949a43

more generic dynamic composition of types and functions

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

M	source/blender/functions/c_wrapper.cpp
M	source/blender/functions/core/core.hpp
M	source/blender/functions/core/cpu.hpp
M	source/blender/functions/types/numeric.hpp
M	source/blender/modifiers/intern/MOD_functiondeform.c

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

diff --git a/source/blender/functions/c_wrapper.cpp b/source/blender/functions/c_wrapper.cpp
index dd7e3ea45f4..3449cf07a59 100644
--- a/source/blender/functions/c_wrapper.cpp
+++ b/source/blender/functions/c_wrapper.cpp
@@ -114,9 +114,12 @@ public:
 FnFunction FN_get_deform_function(int type)
 {
 	FN::Signature signature({FN::Types::floatvec3d_ty, FN::Types::float_ty}, {FN::Types::floatvec3d_ty});
-	FN::FunctionBodies bodies;
-	if (type == 0) bodies.add(new Deform1());
-	else bodies.add(new Deform2());
-
-	return wrap(new FN::Function(signature, bodies));
+	auto fn = new FN::Function(signature);
+	if (type == 0) {
+		fn->add_body(new Deform1());
+	}
+	else {
+		fn->add_body(new Deform2());
+	}
+	return wrap(fn);
 }
\ No newline at end of file
diff --git a/source/blender/functions/core/core.hpp b/source/blender/functions/core/core.hpp
index 75f0a94773e..eb6dc3a73e8 100644
--- a/source/blender/functions/core/core.hpp
+++ b/source/blender/functions/core/core.hpp
@@ -15,6 +15,36 @@ namespace FN {
 
 	using SmallTypeVector = SmallVector<const Type *>;
 
+	class Composition {
+	public:
+		template<typename T>
+		void add(const T *value)
+		{
+			this->m_elements.add(this->get_key<T>(), (void *)value);
+		}
+
+		template<typename T>
+		inline const T *get() const
+		{
+			uint64_t key = this->get_key<T>();
+			if (this->m_elements.contains(key)) {
+				return (T *)this->m_elements.lookup(key);
+			}
+			else {
+				return nullptr;
+			}
+		}
+
+	private:
+		template<typename T>
+		static uint64_t get_key()
+		{
+			return (uint64_t)T::identifier;
+		}
+
+		BLI::SmallMap<uint64_t, void *> m_elements;
+	};
+
 	class Type {
 	public:
 		const std::string &name() const
@@ -22,12 +52,24 @@ namespace FN {
 			return this->m_name;
 		}
 
+		template<typename T>
+		inline const T *extension() const
+		{
+			return this->m_extensions.get<T>();
+		}
+
+		template<typename T>
+		void extend(const T *extension)
+		{
+			BLI_assert(this->m_extensions.get<T>() == nullptr);
+			this->m_extensions.add(extension);
+		}
+
 	protected:
 		std::string m_name;
 
-	public:
-		/* will be removed */
-		uint m_size;
+	private:
+		Composition m_extensions;
 	};
 
 	class Signature {
@@ -53,41 +95,10 @@ namespace FN {
 		const SmallTypeVector m_outputs;
 	};
 
-	class FunctionBodies {
-	private:
-		BLI::SmallMap<uint64_t, void *> m_bodies;
-
-	public:
-		template<typename T>
-		void add(const T *body)
-		{
-			this->m_bodies.add(this->get_key<T>(), (void *)body);
-		}
-
-		template<typename T>
-		inline const T *get() const
-		{
-			uint64_t key = this->get_key<T>();
-			if (this->m_bodies.contains(key)) {
-				return (T *)this->m_bodies.lookup(key);
-			}
-			else {
-				return nullptr;
-			}
-		}
-
-	private:
-		template<typename T>
-		static uint64_t get_key()
-		{
-			return (uint64_t)T::identifier;
-		}
-	};
-
 	class Function {
 	public:
-		Function(const Signature &signature, const FunctionBodies &bodies)
-			: m_signature(signature), m_bodies(bodies) {}
+		Function(const Signature &signature)
+			: m_signature(signature) {}
 
 		virtual ~Function() {}
 
@@ -102,9 +113,16 @@ namespace FN {
 			return this->m_bodies.get<T>();
 		}
 
+		template<typename T>
+		void add_body(const T *body)
+		{
+			BLI_assert(this->m_bodies.get<T>() == nullptr);
+			this->m_bodies.add(body);
+		}
+
 	private:
 		const Signature m_signature;
-		const FunctionBodies m_bodies;
+		Composition m_bodies;
 	};
 
 } /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/core/cpu.hpp b/source/blender/functions/core/cpu.hpp
index 21ff5bdd97a..1bf65982e91 100644
--- a/source/blender/functions/core/cpu.hpp
+++ b/source/blender/functions/core/cpu.hpp
@@ -4,9 +4,38 @@
 
 namespace FN {
 
+	class Tuple;
+	class TupleCallBody;
+	class TypeSize;
+
+	class TupleCallBody {
+	public:
+		static constexpr const char *identifier = "Tuple Call Body";
+
+		virtual void call(const Tuple &fn_in, Tuple &fn_out) const = 0;
+	};
+
+	class TypeSize {
+	public:
+		static constexpr const char *identifier = "Type Size";
+
+		TypeSize(uint size)
+			: m_size(size) {}
+
+		virtual uint size() const
+		{
+			return this->m_size;
+		}
+
+	private:
+		uint m_size;
+	};
+
 	inline uint get_type_size(const Type *type)
 	{
-		return type->m_size;
+		auto extension = type->extension<TypeSize>();
+		BLI_assert(extension);
+		return extension->size();
 	}
 
 	class Tuple {
@@ -80,11 +109,4 @@ namespace FN {
 		void *data;
 	};
 
-	class TupleCallBody {
-	public:
-		static constexpr const char *identifier = "Tuple Call Body";
-
-		virtual void call(const Tuple &fn_in, Tuple &fn_out) const = 0;
-	};
-
 } /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/types/numeric.hpp b/source/blender/functions/types/numeric.hpp
index 1757fccf148..621f338a5fa 100644
--- a/source/blender/functions/types/numeric.hpp
+++ b/source/blender/functions/types/numeric.hpp
@@ -9,7 +9,7 @@ namespace FN::Types {
 		FloatType()
 		{
 			this->m_name = "Float";
-			this->m_size = sizeof(float);
+			this->extend(new FN::TypeSize(sizeof(float)));
 		}
 	};
 
@@ -18,7 +18,7 @@ namespace FN::Types {
 		Int32Type()
 		{
 			this->m_name = "Int32";
-			this->m_size = sizeof(int32_t);
+			this->extend(new FN::TypeSize(sizeof(int32_t)));
 		}
 	};
 
@@ -28,7 +28,7 @@ namespace FN::Types {
 		FloatVectorType()
 		{
 			this->m_name = "FloatVector" + std::to_string(N) + "D";
-			this->m_size = sizeof(float) * N;
+			this->extend(new FN::TypeSize(sizeof(float) * N));
 		}
 	};
 
diff --git a/source/blender/modifiers/intern/MOD_functiondeform.c b/source/blender/modifiers/intern/MOD_functiondeform.c
index d05162a96e1..dd39f50c02b 100644
--- a/source/blender/modifiers/intern/MOD_functiondeform.c
+++ b/source/blender/modifiers/intern/MOD_functiondeform.c
@@ -55,6 +55,7 @@ static void do_deformation(
 {
 	FnFunction fn = FN_get_deform_function(fdmd->control2);
 	FnCallable fn_call = FN_function_get_callable(fn);
+	BLI_assert(fn_call);
 	FnTuple fn_in = FN_tuple_for_input(fn);
 	FnTuple fn_out = FN_tuple_for_output(fn);



More information about the Bf-blender-cvs mailing list