[Bf-blender-cvs] [9f850e5e4e6] functions: get float list element function

Jacques Lucke noreply at git.blender.org
Sun Mar 10 16:28:40 CET 2019


Commit: 9f850e5e4e6b9d539b65234fdfe335f181ba7d3d
Author: Jacques Lucke
Date:   Sun Mar 10 14:56:05 2019 +0100
Branches: functions
https://developer.blender.org/rB9f850e5e4e6b9d539b65234fdfe335f181ba7d3d

get float list element function

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

M	source/blender/functions/backends/tuple_call/tuple.hpp
M	source/blender/functions/functions/lists.cpp
M	source/blender/functions/functions/lists.hpp

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

diff --git a/source/blender/functions/backends/tuple_call/tuple.hpp b/source/blender/functions/backends/tuple_call/tuple.hpp
index d8563d9f7b4..1f0271f55c4 100644
--- a/source/blender/functions/backends/tuple_call/tuple.hpp
+++ b/source/blender/functions/backends/tuple_call/tuple.hpp
@@ -185,6 +185,14 @@ namespace FN {
 			return this->copy_out<T>(index);
 		}
 
+		template<typename T>
+		inline T &get_ref(uint index) const
+		{
+			BLI_assert(index < m_meta->element_amount());
+			BLI_assert(m_initialized[index]);
+			return this->element_ref<T>(index);
+		}
+
 		static inline void copy_element(
 			const Tuple &from, uint from_index,
 			Tuple &to, uint to_index)
diff --git a/source/blender/functions/functions/lists.cpp b/source/blender/functions/functions/lists.cpp
index 590bfb50858..aff411ad4ca 100644
--- a/source/blender/functions/functions/lists.cpp
+++ b/source/blender/functions/functions/lists.cpp
@@ -8,11 +8,12 @@ namespace FN { namespace Functions {
 
 	using namespace Types;
 
-	class AppendFloat : public TupleCallBody {
+	template<typename T>
+	class AppendToList : public TupleCallBody {
 		void call(Tuple &fn_in, Tuple &fn_out) const override
 		{
-			auto list = fn_in.relocate_out<SharedFloatList>(0);
-			float value = fn_in.get<float>(1);
+			auto list = fn_in.relocate_out<SharedList<T>>(0);
+			T value = fn_in.relocate_out<T>(1);
 
 			list = list->get_mutable();
 			list->append(value);
@@ -21,6 +22,25 @@ namespace FN { namespace Functions {
 		}
 	};
 
+	template<typename T>
+	class GetListElement : public TupleCallBody {
+		void call(Tuple &fn_in, Tuple &fn_out) const override
+		{
+			auto list = fn_in.get_ref<SharedList<T>>(0);
+			int32_t index = fn_in.get<int32_t>(1);
+
+			if (index >= 0 && index < list->size()) {
+				List<T> *list_ = list.ptr();
+				T value = (*list_)[index];
+				fn_out.move_in(0, value);
+			}
+			else {
+				T fallback = fn_in.relocate_out<T>(0);
+				fn_out.move_in(0, fallback);
+			}
+		}
+	};
+
 	LAZY_INIT_REF__NO_ARG(SharedFunction, append_float)
 	{
 		auto fn = SharedFunction::New("Append Float", Signature({
@@ -29,7 +49,20 @@ namespace FN { namespace Functions {
 		}, {
 			OutputParameter("List", get_float_list_type()),
 		}));
-		fn->add_body(new AppendFloat());
+		fn->add_body(new AppendToList<float>());
+		return fn;
+	}
+
+	LAZY_INIT_REF__NO_ARG(SharedFunction, get_float_list_element)
+	{
+		auto fn = SharedFunction::New("Get Float List Element", Signature({
+			InputParameter("List", get_float_list_type()),
+			InputParameter("Index", get_int32_type()),
+			InputParameter("Fallback", get_float_type()),
+		}, {
+			OutputParameter("Element", get_float_type()),
+		}));
+		fn->add_body(new GetListElement<float>());
 		return fn;
 	}
 
diff --git a/source/blender/functions/functions/lists.hpp b/source/blender/functions/functions/lists.hpp
index d72a292cece..347ff5152b2 100644
--- a/source/blender/functions/functions/lists.hpp
+++ b/source/blender/functions/functions/lists.hpp
@@ -5,5 +5,6 @@
 namespace FN { namespace Functions {
 
 	SharedFunction &append_float();
+	SharedFunction &get_float_list_element();
 
 } } /* namespace FN::Functions */
\ No newline at end of file



More information about the Bf-blender-cvs mailing list