[Bf-blender-cvs] [09a561aa253] functions: more initial setup

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


Commit: 09a561aa253cb69023d57391a6c2a6fa9948d6ff
Author: Jacques Lucke
Date:   Mon Jan 21 18:58:23 2019 +0100
Branches: functions
https://developer.blender.org/rB09a561aa253cb69023d57391a6c2a6fa9948d6ff

more initial setup

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

A	source/blender/blenlib/BLI_small_buffer.hpp
M	source/blender/blenlib/BLI_small_vector.hpp
M	source/blender/blenlib/CMakeLists.txt
M	source/blender/functions/FN_functions.h
M	source/blender/functions/FN_functions.hpp
M	source/blender/functions/intern/c_wrapper.cpp
M	source/blender/functions/intern/function.cpp

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

diff --git a/source/blender/blenlib/BLI_small_buffer.hpp b/source/blender/blenlib/BLI_small_buffer.hpp
new file mode 100644
index 00000000000..52166fbba64
--- /dev/null
+++ b/source/blender/blenlib/BLI_small_buffer.hpp
@@ -0,0 +1,43 @@
+#pragma once
+
+#include "BLI_utildefines.h"
+#include <vector>
+#include <cstring>
+
+namespace BLI {
+
+	template<uint N = 16>
+	class SmallBuffer {
+	public:
+		SmallBuffer() {}
+
+		SmallBuffer(int size)
+		{
+			this->size = size;
+			if (size > N) {
+				this->buffer = new char[size];
+			}
+			else {
+				this->buffer = this->internal_buffer;
+			}
+		}
+
+		void copy_in(uint dst, void *src, uint amount)
+		{
+			BLI_assert(dst + amount < this->size);
+			memcpy(this->buffer + dst, src, amount);
+		}
+
+		void copy_out(void *dst, uint src, uint amount) const
+		{
+			BLI_assert(src + amount < this->size);
+			memcpy(dst, this->buffer + src, amount);
+		}
+
+	private:
+		uint size;
+		char *buffer;
+		char internal_buffer[N];
+	};
+
+} /* namespace BLI */
\ No newline at end of file
diff --git a/source/blender/blenlib/BLI_small_vector.hpp b/source/blender/blenlib/BLI_small_vector.hpp
index ec933d5bb08..28fb0b4c27a 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -7,11 +7,54 @@ namespace BLI {
 
 	template<typename T, uint N = 4>
 	class SmallVector {
+	private:
+		using elements_t = std::vector<T>;
+		elements_t elements;
+
 	public:
+		using iterator = typename elements_t::iterator;
+		using const_iterator = typename elements_t::const_iterator;
+
 		SmallVector() {}
 
-	private:
-		std::vector<T> elements;
+		SmallVector(std::initializer_list<T> values)
+		{
+			for (T value : values) {
+				this->append(value);
+			}
+		}
+
+		void append(T value)
+		{
+			this->elements.push_back(value);
+		}
+
+		uint size() const
+		{
+			return this->elements.size();
+		}
+
+		T &operator[](const int index)
+		{
+			BLI_assert(index >= 0 && index < this->size());
+			return this->elements[index];
+		}
+
+		T operator[](const int index) const
+		{
+			BLI_assert(index >= 0 && index < this->size());
+			return this->elements[index];
+		}
+
+		const_iterator begin() const
+		{ return this->elements.begin(); }
+		const_iterator end() const
+		{ return this->elements.end(); }
+
+		const_iterator cbegin() const
+		{ return this->elements.cbegin(); }
+		const_iterator cend() const
+		{ return this->elements.cend(); }
 	};
 
 } /* namespace BLI */
\ No newline at end of file
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 3f89a1c6603..046cbe1a988 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -226,6 +226,7 @@ set(SRC
 	PIL_time_utildefines.h
 
 	BLI_small_vector.hpp
+	BLI_small_buffer.hpp
 )
 
 if(WITH_MEM_VALGRIND)
diff --git a/source/blender/functions/FN_functions.h b/source/blender/functions/FN_functions.h
index dd6f88b7f52..8d5db4c5e00 100644
--- a/source/blender/functions/FN_functions.h
+++ b/source/blender/functions/FN_functions.h
@@ -41,7 +41,7 @@ void FN_inputs_free(FnInputsRef fn_in);
 bool FN_inputs_set_name(FnInputsRef fn_in, const char *name, void *value);
 
 /* Set a function input by index. Returns true on success. */
-bool FN_inputs_set_index(FnInputsRef fn_in, uint index, void *value);
+void FN_inputs_set_index(FnInputsRef fn_in, uint index, void *value);
 
 
 /* Create a container to store function outputs. */
@@ -62,6 +62,8 @@ FnTypeRef FN_type_get_float(void);
 FnTypeRef FN_type_get_int32(void);
 FnTypeRef FN_type_get_float_vector_3d(void);
 
+FunctionRef FN_get_add_const_function(int value);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/functions/FN_functions.hpp b/source/blender/functions/FN_functions.hpp
index f9f17ade2fe..b3481502ce8 100644
--- a/source/blender/functions/FN_functions.hpp
+++ b/source/blender/functions/FN_functions.hpp
@@ -4,6 +4,7 @@
 
 #include "BLI_utildefines.h"
 #include "BLI_small_vector.hpp"
+#include "BLI_small_buffer.hpp"
 
 namespace FN {
 	using namespace BLI;
@@ -14,6 +15,8 @@ namespace FN {
 	class Signature;
 	class Function;
 
+	using SmallTypeVector = SmallVector<Type *>;
+
 	class Type {
 	public:
 		const std::string &name() const;
@@ -24,41 +27,73 @@ namespace FN {
 		uint m_size;
 	};
 
+	class ValueArray {
+	public:
+		ValueArray() {};
+		ValueArray(SmallTypeVector types);
+		void set(uint index, void *src);
+		void get(uint index, void *dst) const;
+
+	private:
+		SmallTypeVector types;
+		SmallVector<int> offsets;
+		SmallBuffer<> storage;
+	};
+
 	class Inputs {
 	public:
-		static Inputs *New(Function *fn);
+		Inputs(Function &fn);
 
-		bool set(uint index, void *value);
+		inline void set(uint index, void *src)
+		{ this->values.set(index, src); }
+		inline void get(uint index, void *dst) const
+		{ this->values.get(index, dst); }
 
 	private:
-		Inputs() {}
-
-		Function *fn;
+		Function &fn;
+		ValueArray values;
 	};
 
 	class Outputs {
 	public:
-		static Outputs *New(Function *fn);
+		static Outputs *New(Function &fn);
 
-		bool get(uint index, void *value);
+		bool set(uint index, void *value);
+		bool get(uint index);
 
 	private:
 		Outputs() {}
 
 		Function *fn;
+		ValueArray values;
 	};
 
 	class Signature {
+	public:
+		Signature() {}
+		Signature(SmallTypeVector inputs, SmallTypeVector outputs)
+			: m_inputs(inputs), m_outputs(outputs) {}
+
+		inline const SmallTypeVector &inputs() const
+		{ return this->m_inputs; }
+		inline const SmallTypeVector &outputs() const
+		{ return this->m_outputs; }
+
 	private:
-		SmallVector<Type *> inputs;
-		SmallVector<Type *> outputs;
+		SmallTypeVector m_inputs;
+		SmallTypeVector m_outputs;
 	};
 
 	class Function {
 	public:
-		bool call(Inputs *fn_in, Outputs *fn_out);
+		bool call(Inputs &fn_in, Outputs &fn_out);
+
+		inline const Signature &signature() const
+		{ return this->m_signature; }
 
 	private:
-		Signature *signature;
+
+	protected:
+		Signature m_signature;
 	};
 } /* namespace FN */
diff --git a/source/blender/functions/intern/c_wrapper.cpp b/source/blender/functions/intern/c_wrapper.cpp
index 619f9551e48..553ea6671ca 100644
--- a/source/blender/functions/intern/c_wrapper.cpp
+++ b/source/blender/functions/intern/c_wrapper.cpp
@@ -5,17 +5,17 @@
 
 bool FN_function_call(FunctionRef fn, FnInputsRef fn_in, FnOutputsRef fn_out)
 {
-	return ((FN::Function *)fn)->call((FN::Inputs *)fn_in, (FN::Outputs *)fn_out);
+	return ((FN::Function *)fn)->call(*(FN::Inputs *)fn_in, *(FN::Outputs *)fn_out);
 }
 
 FnInputsRef FN_inputs_new(FunctionRef fn)
 {
-	return (FnInputsRef)FN::Inputs::New((FN::Function *)fn);
+	return (FnInputsRef)new FN::Inputs(*(FN::Function *)fn);
 }
 
-bool FN_inputs_set_index(FnInputsRef fn_in, uint index, void *value)
+void FN_inputs_set_index(FnInputsRef fn_in, uint index, void *value)
 {
-	return ((FN::Inputs *)fn_in)->set(index, value);
+	((FN::Inputs *)fn_in)->set(index, value);
 }
 
 const char *FN_type_name(FnTypeRef type)
@@ -31,3 +31,26 @@ FnTypeRef FN_type_get_int32()
 
 FnTypeRef FN_type_get_float_vector_3d()
 { return (FnTypeRef)FN::Types::floatvec3d_ty; }
+
+
+class AddConstFunction : public FN::Function {
+public:
+	AddConstFunction(int value)
+		: value(value)
+	{
+		this->m_signature = FN::Signature({FN::Types::int32_ty}, {FN::Types::int32_ty});
+	}
+
+	bool call(FN::Inputs &UNUSED(fn_in), FN::Outputs &UNUSED(fn_out))
+	{
+		return false;
+	}
+
+private:
+	int value;
+};
+
+FunctionRef FN_get_add_const_function(int value)
+{
+	return (FunctionRef)new AddConstFunction(value);
+}
\ No newline at end of file
diff --git a/source/blender/functions/intern/function.cpp b/source/blender/functions/intern/function.cpp
index fd77119c4bd..28cbd0c1e10 100644
--- a/source/blender/functions/intern/function.cpp
+++ b/source/blender/functions/intern/function.cpp
@@ -12,17 +12,40 @@ const uint Type::size() const
 	return this->m_size;
 }
 
-Inputs *Inputs::New(Function *fn)
+Inputs::Inputs(Function &fn)
+	: fn(fn), values(fn.signature().inputs())
+{ }
+
+bool Function::call(Inputs &UNUSED(fn_in), Outputs &UNUSED(fn_out))
 {
-	return nullptr;
+	return false;
 }
 
-bool Inputs::set(uint index, void *value)
+ValueArray::ValueArray(SmallTypeVector types)
+	: types(types)
 {
-	return false;
+	int total_size = 0;
+	for (Type *type : types) {
+		this->offsets.append(total_size);
+		total_size += type->size();
+	}
+	this->storage = SmallBuffer<>(total_size);
 }
 
-bool Function::call(Inputs *fn_in, Outputs *fn_out)
+void ValueArray::set(uint index, void *src)
 {
-	return false;
+	BLI_assert(index < this->offsets.size());
+	this->storage.copy_in(
+		this->offsets[index],
+		src,
+		this->types[index]->size());
+}
+
+void ValueArray::get(uint index, void *dst) const
+{
+	BLI_assert(index < this->offsets.size());
+	this->storage.copy_out(
+		dst,
+		this->offsets[index],
+		this->types[index]->size());
 }
\ No newline at end of file



More information about the Bf-blender-cvs mailing list