[Bf-blender-cvs] [a9c17964703] functions: use shared ownership for functions and types

Jacques Lucke noreply at git.blender.org
Sun Feb 10 20:26:15 CET 2019


Commit: a9c17964703b4217ad69e7487f7b0d59eb752aee
Author: Jacques Lucke
Date:   Wed Feb 6 18:19:31 2019 +0100
Branches: functions
https://developer.blender.org/rBa9c17964703b4217ad69e7487f7b0d59eb752aee

use shared ownership for functions and types

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

M	source/blender/blenlib/BLI_small_map.hpp
M	source/blender/functions/c_wrapper.cpp
M	source/blender/functions/core/core.hpp
M	source/blender/functions/core/cpu.hpp

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

diff --git a/source/blender/blenlib/BLI_small_map.hpp b/source/blender/blenlib/BLI_small_map.hpp
index 573d1eb4619..1af9692b100 100644
--- a/source/blender/blenlib/BLI_small_map.hpp
+++ b/source/blender/blenlib/BLI_small_map.hpp
@@ -7,6 +7,8 @@ namespace BLI {
 	template<typename K, typename V, uint N = 4>
 	class SmallMap {
 	private:
+	public:
+		/* only public until there are proper iterators */
 		struct Entry {
 			K key;
 			V value;
@@ -18,7 +20,6 @@ namespace BLI {
 
 		SmallVector<Entry> m_entries;
 
-	public:
 		SmallMap() = default;
 
 		void add(K key, V value)
diff --git a/source/blender/functions/c_wrapper.cpp b/source/blender/functions/c_wrapper.cpp
index eff4d985817..5dc84941f75 100644
--- a/source/blender/functions/c_wrapper.cpp
+++ b/source/blender/functions/c_wrapper.cpp
@@ -132,45 +132,23 @@ public:
 		Vector result;
 
 		result.x = vec.x;
-		result.y = vec.y * control;// / std::max(control, 0.1f);
+		result.y = vec.y * control;
 		result.z = vec.z;
 
 		fn_out.set<Vector>(0, result);
 	}
 };
 
-template<typename T>
-class PassThroughBody : public FN::TupleCallBody {
-public:
-	virtual void call(const FN::Tuple &fn_in, FN::Tuple &fn_out) const override
-	{
-		fn_out.set<T>(0, fn_in.get<T>(0));
-	}
-};
-
-static FN::SharedFunction get_pass_through_float_function()
-{
-	FN::InputParameters inputs;
-	inputs.append(FN::InputParameter("In", FN::Types::float_ty));
-
-	FN::OutputParameters outputs;
-	outputs.append(FN::OutputParameter("Out", FN::Types::float_ty));
-
-	auto fn = FN::SharedFunction::New(FN::Signature(inputs, outputs), "Pass Through");
-	fn->add_body(new PassThroughBody<float>());
-	return fn;
-}
-
 FnFunction FN_get_deform_function(int type)
 {
 	FN::InputParameters inputs;
-	inputs.append(FN::InputParameter("Position", FN::Types::floatvec3d_ty));
-	inputs.append(FN::InputParameter("Control", FN::Types::float_ty));
+	inputs.append(FN::InputParameter("Position", FN::Types::floatvec3d_ty)); // +1
+	inputs.append(FN::InputParameter("Control", FN::Types::float_ty)); // +1
 
 	FN::OutputParameters outputs;
-	outputs.append(FN::OutputParameter("Position", FN::Types::floatvec3d_ty));
+	outputs.append(FN::OutputParameter("Position", FN::Types::floatvec3d_ty)); // +1
 
-	auto fn = FN::SharedFunction::New(FN::Signature(inputs, outputs), "Deform");
+	auto fn = FN::SharedFunction::New(FN::Signature(inputs, outputs), "Deform"); // +3
 	if (type == 0) {
 		fn->add_body(new Deform1());
 	}
@@ -178,23 +156,6 @@ FnFunction FN_get_deform_function(int type)
 		fn->add_body(new Deform2());
 	}
 
-	FN::DataFlowGraph graph;
-	const FN::Node *n1 = graph.insert(fn);
-	const FN::Node *n2 = graph.insert(fn);
-	const FN::Node *n3 = graph.insert(fn);
-	const FN::Node *n4 = graph.insert(fn);
-	const FN::Node *p = graph.insert(get_pass_through_float_function());
-	graph.link(n1->output(0), n2->input(0));
-	graph.link(n2->output(0), n3->input(0));
-	graph.link(n2->output(0), n4->input(0));
-	graph.link(p->output(0), n1->input(1));
-	graph.link(p->output(0), n2->input(1));
-	graph.link(p->output(0), n3->input(1));
-	graph.link(p->output(0), n4->input(1));
-
-	std::string dot = graph.to_dot();
-	//std::cout << dot << std::endl;
-
 	BLI::RefCounted<FN::Function> *fn_ref = fn.refcounter();
 	fn_ref->incref();
 	return wrap(fn_ref);
diff --git a/source/blender/functions/core/core.hpp b/source/blender/functions/core/core.hpp
index 9cb2e5884ba..990b9013e49 100644
--- a/source/blender/functions/core/core.hpp
+++ b/source/blender/functions/core/core.hpp
@@ -21,32 +21,61 @@ namespace FN {
 
 	class Composition {
 	public:
+		typedef void (*FreeFunction)(void *value);
+
 		template<typename T>
-		void add(const T *value)
+		void add(T *value)
 		{
-			this->m_elements.add(this->get_key<T>(), (void *)value);
+			this->m_elements.add(this->get_key<T>(), this->create_entry(value));
 		}
 
 		template<typename T>
-		inline const T *get() const
+		inline T *get() const
 		{
 			uint64_t key = this->get_key<T>();
 			if (this->m_elements.contains(key)) {
-				return (T *)this->m_elements.lookup(key);
+				return (T *)this->m_elements.lookup(key).value;
 			}
 			else {
 				return nullptr;
 			}
 		}
 
+		~Composition()
+		{
+			for (SmallMap<uint64_t, Entry>::Entry &entry : this->m_elements.m_entries) {
+				entry.value.free(entry.value.value);
+			}
+		}
+
 	private:
+		struct Entry {
+			void *value;
+			FreeFunction free;
+		};
+
 		template<typename T>
 		static uint64_t get_key()
 		{
 			return (uint64_t)T::identifier;
 		}
 
-		BLI::SmallMap<uint64_t, void *> m_elements;
+		template<typename T>
+		static FreeFunction get_free_function()
+		{
+			return T::free;
+		}
+
+		template<typename T>
+		Entry create_entry(T *value)
+		{
+			Entry entry;
+			entry.value = (void *)value;
+			entry.free = this->get_free_function<T>();
+			return entry;
+		}
+
+		BLI::SmallMap<uint64_t, Entry> m_elements;
 	};
 
 	class Type {
@@ -78,7 +107,7 @@ namespace FN {
 
 	class Parameter {
 	public:
-		Parameter(const std::string &name, SharedType type)
+		Parameter(const std::string &name, SharedType &type)
 			: m_type(type), m_name(name) {}
 
 		const SharedType &type() const
@@ -98,13 +127,13 @@ namespace FN {
 
 	class InputParameter : public Parameter {
 	public:
-		InputParameter(const std::string &name, SharedType type)
+		InputParameter(const std::string &name, SharedType &type)
 			: Parameter(name, type) {}
 	};
 
 	class OutputParameter : public Parameter {
 	public:
-		OutputParameter(const std::string &name, SharedType type)
+		OutputParameter(const std::string &name, SharedType &type)
 			: Parameter(name, type) {}
 	};
 
diff --git a/source/blender/functions/core/cpu.hpp b/source/blender/functions/core/cpu.hpp
index 668427b04d8..f4331318000 100644
--- a/source/blender/functions/core/cpu.hpp
+++ b/source/blender/functions/core/cpu.hpp
@@ -12,13 +12,26 @@ namespace FN {
 	public:
 		static constexpr const char *identifier = "Tuple Call Body";
 
+		static void free(void *value)
+		{
+			TupleCallBody *v = (TupleCallBody *)value;
+			delete v;
+		}
+
 		virtual void call(const Tuple &fn_in, Tuple &fn_out) const = 0;
+		virtual ~TupleCallBody() {};
 	};
 
-	class TypeSize {
+	class TypeSize final {
 	public:
 		static constexpr const char *identifier = "Type Size";
 
+		static void free(void *value)
+		{
+			TypeSize *v = (TypeSize *)value;
+			delete v;
+		}
+
 		TypeSize(uint size)
 			: m_size(size) {}



More information about the Bf-blender-cvs mailing list