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

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


Commit: 328a6e9c91234bb94f1d1e9c83945e2a32e7d99c
Author: Jacques Lucke
Date:   Wed Feb 6 16:26:19 2019 +0100
Branches: functions
https://developer.blender.org/rB328a6e9c91234bb94f1d1e9c83945e2a32e7d99c

use shared ownership for functions and types

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

M	source/blender/blenlib/BLI_shared.hpp
M	source/blender/functions/FN_functions.h
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/core/data_flow_graph.cpp
M	source/blender/functions/core/data_flow_graph.hpp
M	source/blender/functions/core/dot_export.cpp
M	source/blender/functions/types/numeric.hpp
M	source/blender/modifiers/intern/MOD_functiondeform.c

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

diff --git a/source/blender/blenlib/BLI_shared.hpp b/source/blender/blenlib/BLI_shared.hpp
index 93376ac4e26..60c599ab8ef 100644
--- a/source/blender/blenlib/BLI_shared.hpp
+++ b/source/blender/blenlib/BLI_shared.hpp
@@ -64,7 +64,12 @@ namespace BLI {
 		static Shared<T> New(Args&&... args)
 		{
 			T *actual_value = new T(std::forward<Args>(args)...);
-			RefCounted<T> *refcounted_value = new RefCounted<T>(actual_value);
+			return Shared<T>::FromPointer(actual_value);
+		}
+
+		static Shared<T> FromPointer(T *ptr)
+		{
+			RefCounted<T> *refcounted_value = new RefCounted<T>(ptr);
 			return Shared<T>(refcounted_value);
 		}
 
@@ -114,6 +119,11 @@ namespace BLI {
 		{
 			return this->m_object;
 		}
+
+		friend bool operator==(const Shared &a, const Shared &b)
+		{
+			return a.refcounter()->ptr() == b.refcounter()->ptr();
+		}
 	};
 
 } /* namespace BLI */
\ No newline at end of file
diff --git a/source/blender/functions/FN_functions.h b/source/blender/functions/FN_functions.h
index ec6191f6645..428c09f54fe 100644
--- a/source/blender/functions/FN_functions.h
+++ b/source/blender/functions/FN_functions.h
@@ -8,12 +8,13 @@ extern "C" {
 #endif
 
 typedef struct OpaqueFnFunction *FnFunction;
-typedef struct OpaqueFnType *FnTypeRef;
+typedef struct OpaqueFnType *FnType;
 typedef struct OpaqueFnTuple *FnTuple;
 typedef struct OpaqueFnCallable *FnCallable;
 
 FnCallable FN_function_get_callable(FnFunction fn);
 void FN_function_call(FnCallable call, FnTuple fn_in, FnTuple fn_out);
+void FN_function_free(FnFunction fn);
 
 FnTuple FN_tuple_for_input(FnFunction fn);
 FnTuple FN_tuple_for_output(FnFunction fn);
@@ -25,11 +26,12 @@ void FN_tuple_set_float_vector_3(FnTuple tuple, uint index, float vector[3]);
 
 void FN_tuple_get_float_vector_3(FnTuple tuple, uint index, float dst[3]);
 
-const char *FN_type_name(FnTypeRef type);
+const char *FN_type_name(FnType type);
+void FN_type_free(FnType type);
 
-FnTypeRef FN_type_get_float(void);
-FnTypeRef FN_type_get_int32(void);
-FnTypeRef FN_type_get_float_vector_3d(void);
+FnType FN_type_get_float(void);
+FnType FN_type_get_int32(void);
+FnType FN_type_get_float_vector_3d(void);
 
 FnFunction FN_get_deform_function(int type);
 
diff --git a/source/blender/functions/c_wrapper.cpp b/source/blender/functions/c_wrapper.cpp
index 56cc18b4988..eff4d985817 100644
--- a/source/blender/functions/c_wrapper.cpp
+++ b/source/blender/functions/c_wrapper.cpp
@@ -9,7 +9,10 @@
 	inline T1 unwrap(T2 value) { return (T1)value; } \
 	inline T2 wrap(T1 value) { return (T2)value; }
 
-WRAPPERS(const FN::Function *, FnFunction);
+
+WRAPPERS(BLI::RefCounted<FN::Function> *, FnFunction);
+WRAPPERS(BLI::RefCounted<const FN::Type> *, FnType);
+
 WRAPPERS(FN::Tuple *, FnTuple);
 WRAPPERS(const FN::TupleCallBody *, FnCallable);
 
@@ -20,18 +23,23 @@ void FN_function_call(FnCallable fn_call, FnTuple fn_in, FnTuple fn_out)
 
 FnCallable FN_function_get_callable(FnFunction fn)
 {
-	return wrap(unwrap(fn)->body<FN::TupleCallBody>());
+	return wrap(unwrap(fn)->ptr()->body<FN::TupleCallBody>());
+}
+
+void FN_function_free(FnFunction fn)
+{
+	unwrap(fn)->decref();
 }
 
 FnTuple FN_tuple_for_input(FnFunction fn)
 {
-	auto tuple = new FN::Tuple(unwrap(fn)->signature().input_types());
+	auto tuple = new FN::Tuple(unwrap(fn)->ptr()->signature().input_types());
 	return wrap(tuple);
 }
 
 FnTuple FN_tuple_for_output(FnFunction fn)
 {
-	auto tuple = new FN::Tuple(unwrap(fn)->signature().output_types());
+	auto tuple = new FN::Tuple(unwrap(fn)->ptr()->signature().output_types());
 	return wrap(tuple);
 }
 
@@ -59,19 +67,37 @@ void FN_tuple_get_float_vector_3(FnTuple tuple, uint index, float dst[3])
 	*(Vector *)dst = unwrap(tuple)->get<Vector>(index);
 }
 
-const char *FN_type_name(FnTypeRef type)
+const char *FN_type_name(FnType type)
 {
 	return ((FN::Type *)type)->name().c_str();
 }
 
-FnTypeRef FN_type_get_float()
-{ return (FnTypeRef)FN::Types::float_ty; }
+void FN_type_free(FnType type)
+{
+	unwrap(type)->decref();
+}
 
-FnTypeRef FN_type_get_int32()
-{ return (FnTypeRef)FN::Types::int32_ty; }
+static FnType get_type_with_increased_refcount(const FN::SharedType &type)
+{
+	BLI::RefCounted<const FN::Type> *typeref = type.refcounter();
+	typeref->incref();
+	return wrap(typeref);
+}
 
-FnTypeRef FN_type_get_float_vector_3d()
-{ return (FnTypeRef)FN::Types::floatvec3d_ty; }
+FnType FN_type_get_float()
+{
+	return get_type_with_increased_refcount(FN::Types::float_ty);
+}
+
+FnType FN_type_get_int32()
+{
+	return get_type_with_increased_refcount(FN::Types::int32_ty);
+}
+
+FnType FN_type_get_float_vector_3d()
+{
+	return get_type_with_increased_refcount(FN::Types::floatvec3d_ty);
+}
 
 
 #include <cmath>
@@ -122,7 +148,7 @@ public:
 	}
 };
 
-static FN::Function *get_pass_through_float_function()
+static FN::SharedFunction get_pass_through_float_function()
 {
 	FN::InputParameters inputs;
 	inputs.append(FN::InputParameter("In", FN::Types::float_ty));
@@ -130,7 +156,7 @@ static FN::Function *get_pass_through_float_function()
 	FN::OutputParameters outputs;
 	outputs.append(FN::OutputParameter("Out", FN::Types::float_ty));
 
-	auto fn = new FN::Function(FN::Signature(inputs, outputs), "Pass Through");
+	auto fn = FN::SharedFunction::New(FN::Signature(inputs, outputs), "Pass Through");
 	fn->add_body(new PassThroughBody<float>());
 	return fn;
 }
@@ -144,7 +170,7 @@ FnFunction FN_get_deform_function(int type)
 	FN::OutputParameters outputs;
 	outputs.append(FN::OutputParameter("Position", FN::Types::floatvec3d_ty));
 
-	auto fn = new FN::Function(FN::Signature(inputs, outputs), "Deform");
+	auto fn = FN::SharedFunction::New(FN::Signature(inputs, outputs), "Deform");
 	if (type == 0) {
 		fn->add_body(new Deform1());
 	}
@@ -153,11 +179,11 @@ FnFunction FN_get_deform_function(int type)
 	}
 
 	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());
+	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));
@@ -169,5 +195,7 @@ FnFunction FN_get_deform_function(int type)
 	std::string dot = graph.to_dot();
 	//std::cout << dot << std::endl;
 
-	return wrap(fn);
+	BLI::RefCounted<FN::Function> *fn_ref = fn.refcounter();
+	fn_ref->incref();
+	return wrap(fn_ref);
 }
\ No newline at end of file
diff --git a/source/blender/functions/core/core.hpp b/source/blender/functions/core/core.hpp
index ccf1a96d1c3..9cb2e5884ba 100644
--- a/source/blender/functions/core/core.hpp
+++ b/source/blender/functions/core/core.hpp
@@ -1,9 +1,11 @@
 #pragma once
 
 #include <string>
+#include <iostream>
 
 #include "BLI_small_vector.hpp"
 #include "BLI_small_map.hpp"
+#include "BLI_shared.hpp"
 
 namespace FN {
 
@@ -13,7 +15,9 @@ namespace FN {
 	class Signature;
 	class Function;
 
-	using SmallTypeVector = SmallVector<const Type *>;
+	using SharedType = Shared<const Type>;
+	using SharedFunction = Shared<Function>;
+	using SmallTypeVector = SmallVector<SharedType>;
 
 	class Composition {
 	public:
@@ -74,10 +78,10 @@ namespace FN {
 
 	class Parameter {
 	public:
-		Parameter(const std::string &name, const Type *type)
+		Parameter(const std::string &name, SharedType type)
 			: m_type(type), m_name(name) {}
 
-		const Type *type() const
+		const SharedType &type() const
 		{
 			return this->m_type;
 		}
@@ -88,19 +92,19 @@ namespace FN {
 		}
 
 	private:
-		const Type *m_type;
+		SharedType m_type;
 		const std::string m_name;
 	};
 
 	class InputParameter : public Parameter {
 	public:
-		InputParameter(const std::string &name, const Type *type)
+		InputParameter(const std::string &name, SharedType type)
 			: Parameter(name, type) {}
 	};
 
 	class OutputParameter : public Parameter {
 	public:
-		OutputParameter(const std::string &name, const Type *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 33ca9a4a144..668427b04d8 100644
--- a/source/blender/functions/core/cpu.hpp
+++ b/source/blender/functions/core/cpu.hpp
@@ -31,7 +31,7 @@ namespace FN {
 		uint m_size;
 	};
 
-	inline uint get_type_size(const Type *type)
+	inline uint get_type_size(const SharedType &type)
 	{
 		auto extension = type->extension<TypeSize>();
 		BLI_assert(extension);
@@ -46,7 +46,7 @@ namespace FN {
 			: m_types(types)
 		{
 			int total_size = 0;
-			for (const Type *type : types) {
+			for (const SharedType &type : types) {
 				this->m_offsets.append(total_size);
 				this->m_initialized.append(false);
 				total_size += get_type_size(type);
diff --git a/source/blender/functions/core/data_flow_graph.cpp b/source/blender/functions/core/data_flow_graph.cpp
index 61c52b525e8..541de0a76bb 100644
--- a/source/blender/functions/core/data_flow_graph.cpp
+++ b/source/blender/functions/core/data_flow_graph.cpp
@@ -1,7 +1,7 @@
 #include "data_flow_graph.hpp"
 
 namespace FN {
-	const Type *Socket::type() const
+	const SharedType &Socket::type() const
 	{
 		if (this->m_is_output) {
 			return this->node()->signature().outputs()[this->m_index].type();
diff --git a/source/blender/functions/core/data_flow_graph.hpp b/source/blender/functions/core/data_flow_graph.hpp
index 1ea5285c57c..24f74e6722f 100644
--- a/source/blender/functions/core/data_flow_graph.hpp
+++ b/source/blender/functions/core/data_flow_graph.hpp
@@ -36,7 +36,7 @@ namespace FN {
 			return this->m_index;
 		}
 
-		const Type *type() const;
+		const SharedType &type() const;
 		std::string name() const;
 
 		friend bool operator==(const Socket &a, const Socket &b)
@@ -58,7 +58,7 @@ namespace FN {
 
 	class Node {
 	public:
-		Node(const Function &function)
+		Node(const SharedFunction &function)
 			: m_function(function) {}
 
 		Socket input(uint index) const
@@ -71,18 +71,18 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list