[Bf-blender-cvs] [d7fca3e4683] functions: initial lazy init macro

Jacques Lucke noreply at git.blender.org
Sun Feb 17 21:57:23 CET 2019


Commit: d7fca3e46835977659cd1839810e7926483e8d07
Author: Jacques Lucke
Date:   Sun Feb 17 14:46:54 2019 +0100
Branches: functions
https://developer.blender.org/rBd7fca3e46835977659cd1839810e7926483e8d07

initial lazy init macro

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

A	source/blender/blenlib/BLI_lazy_init.hpp
M	source/blender/blenlib/CMakeLists.txt
M	source/blender/functions/c_wrapper.cpp
M	source/blender/functions/types/numeric.cpp
M	source/blender/functions/types/numeric.hpp
A	tests/gtests/blenlib/BLI_lazy_init_test.cc
M	tests/gtests/blenlib/CMakeLists.txt

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

diff --git a/source/blender/blenlib/BLI_lazy_init.hpp b/source/blender/blenlib/BLI_lazy_init.hpp
new file mode 100644
index 00000000000..66c5ba2a13c
--- /dev/null
+++ b/source/blender/blenlib/BLI_lazy_init.hpp
@@ -0,0 +1,8 @@
+#define LAZY_INIT_NO_ARG(final_ret_type, builder_ret_type, func_name) \
+	builder_ret_type func_name##_impl(); \
+	final_ret_type func_name() \
+	{ \
+		static builder_ret_type value = func_name##_impl(); \
+		return value; \
+	} \
+	builder_ret_type func_name##_impl()
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 4ea5bfb2163..ccd17c30d19 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -226,6 +226,7 @@ set(SRC
 	PIL_time_utildefines.h
 
 	BLI_composition.hpp
+	BLI_lazy_init.hpp
 	BLI_mempool.hpp
 	BLI_shared.hpp
 	BLI_small_vector.hpp
diff --git a/source/blender/functions/c_wrapper.cpp b/source/blender/functions/c_wrapper.cpp
index 940f82cb62c..d036028f6ae 100644
--- a/source/blender/functions/c_wrapper.cpp
+++ b/source/blender/functions/c_wrapper.cpp
@@ -17,7 +17,6 @@ WRAPPERS(const FN::TupleCallBody *, FnCallable);
 
 void FN_initialize()
 {
-	FN::Types::init_numeric_types();
 }
 
 void FN_function_call(FnCallable fn_call, FnTuple fn_in, FnTuple fn_out)
diff --git a/source/blender/functions/types/numeric.cpp b/source/blender/functions/types/numeric.cpp
index 77558bf268c..3ab99babc34 100644
--- a/source/blender/functions/types/numeric.cpp
+++ b/source/blender/functions/types/numeric.cpp
@@ -1,35 +1,31 @@
 #include "numeric.hpp"
+#include "BLI_lazy_init.hpp"
 
 namespace FN::Types {
 
-	SharedType float_type = SharedType::New("Float");
-	SharedType int32_type = SharedType::New("Int32");
-	SharedType fvec3_type = SharedType::New("FloatVector3D");
-
 	struct Vector {
 		float x, y, z;
 	};
 
-	void init_numeric_types()
-	{
-		float_type->extend(new CPPTypeInfoForType<float>());
-		int32_type->extend(new CPPTypeInfoForType<int32_t>());
-		fvec3_type->extend(new CPPTypeInfoForType<Vector>());
-	}
-
-	SharedType &get_float_type()
+	LAZY_INIT_NO_ARG(SharedType&, SharedType, get_float_type)
 	{
-		return float_type;
+		SharedType type = SharedType::New("Float");
+		type->extend(new CPPTypeInfoForType<float>());
+		return type;
 	}
 
-	SharedType &get_int32_type()
+	LAZY_INIT_NO_ARG(SharedType&, SharedType, get_int32_type)
 	{
-		return int32_type;
+		SharedType type = SharedType::New("Int32");
+		type->extend(new CPPTypeInfoForType<int32_t>());
+		return type;
 	}
 
-	SharedType &get_fvec3_type()
+	LAZY_INIT_NO_ARG(SharedType&, SharedType, get_fvec3_type)
 	{
-		return fvec3_type;
+		SharedType type = SharedType::New("FloatVector3D");
+		type->extend(new CPPTypeInfoForType<Vector>());
+		return type;
 	}
 
 } /* namespace FN::Types */
\ No newline at end of file
diff --git a/source/blender/functions/types/numeric.hpp b/source/blender/functions/types/numeric.hpp
index 98a6f809ec9..1ecc1a810dc 100644
--- a/source/blender/functions/types/numeric.hpp
+++ b/source/blender/functions/types/numeric.hpp
@@ -4,8 +4,6 @@
 
 namespace FN::Types {
 
-	void init_numeric_types();
-
 	SharedType &get_float_type();
 	SharedType &get_int32_type();
 	SharedType &get_fvec3_type();
diff --git a/tests/gtests/blenlib/BLI_lazy_init_test.cc b/tests/gtests/blenlib/BLI_lazy_init_test.cc
new file mode 100644
index 00000000000..bcb73dea949
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_lazy_init_test.cc
@@ -0,0 +1,14 @@
+#include "testing/testing.h"
+#include "BLI_lazy_init.hpp"
+
+LAZY_INIT_NO_ARG(void *, void *, get_single_pointer)
+{
+	return std::malloc(42);
+}
+
+TEST(lazy_init, NoArg_ReturnSame)
+{
+	void *ptr1 = get_single_pointer();
+	void *ptr2 = get_single_pointer();
+	EXPECT_EQ(ptr1, ptr2);
+}
\ No newline at end of file
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index 3c0ff77de20..be8afd5ff26 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -47,6 +47,7 @@ BLENDER_TEST(BLI_hash_mm2a "bf_blenlib")
 BLENDER_TEST(BLI_heap "bf_blenlib")
 BLENDER_TEST(BLI_heap_simple "bf_blenlib")
 BLENDER_TEST(BLI_kdopbvh "bf_blenlib;bf_intern_numaapi")
+BLENDER_TEST(BLI_lazy_init "bf_blenlib")
 BLENDER_TEST(BLI_linklist_lockfree "bf_blenlib;bf_intern_numaapi")
 BLENDER_TEST(BLI_listbase "bf_blenlib")
 BLENDER_TEST(BLI_math_base "bf_blenlib")



More information about the Bf-blender-cvs mailing list