[Bf-blender-cvs] [26792360475] master: Cleanup: avoid static initialization order issues when accessing CPPTypes

Jacques Lucke noreply at git.blender.org
Fri Jul 17 14:15:27 CEST 2020


Commit: 267923604754a50064c04a925a841a6714253d72
Author: Jacques Lucke
Date:   Fri Jul 17 14:15:06 2020 +0200
Branches: master
https://developer.blender.org/rB267923604754a50064c04a925a841a6714253d72

Cleanup: avoid static initialization order issues when accessing CPPTypes

Instead of depending on static initialization order of globals use
static variables within functions. Those are initialized on first use.
This is every so slighly less efficient, but avoids a full class of problems.

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

M	source/blender/blenkernel/intern/simulation.cc
M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/FN_cpp_type.hh
D	source/blender/functions/FN_cpp_types.hh
M	source/blender/functions/intern/cpp_types.cc
M	source/blender/simulation/intern/simulation_update.cc
M	tests/gtests/functions/FN_array_spans_test.cc
M	tests/gtests/functions/FN_attributes_ref_test.cc
M	tests/gtests/functions/FN_cpp_type_test.cc
M	tests/gtests/functions/FN_generic_vector_array_test.cc
M	tests/gtests/functions/FN_multi_function_network_test.cc
M	tests/gtests/functions/FN_multi_function_test.cc
M	tests/gtests/functions/FN_spans_test.cc

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

diff --git a/source/blender/blenkernel/intern/simulation.cc b/source/blender/blenkernel/intern/simulation.cc
index cec3547a3f3..1ac987d130d 100644
--- a/source/blender/blenkernel/intern/simulation.cc
+++ b/source/blender/blenkernel/intern/simulation.cc
@@ -54,7 +54,6 @@
 #include "BLT_translation.h"
 
 #include "FN_attributes_ref.hh"
-#include "FN_cpp_types.hh"
 #include "FN_multi_function_network_evaluation.hh"
 #include "FN_multi_function_network_optimization.hh"
 
diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index 0ec531dea0d..2686275e898 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -38,7 +38,6 @@ set(SRC
   FN_array_spans.hh
   FN_attributes_ref.hh
   FN_cpp_type.hh
-  FN_cpp_types.hh
   FN_generic_vector_array.hh
   FN_multi_function.hh
   FN_multi_function_builder.hh
diff --git a/source/blender/functions/FN_cpp_type.hh b/source/blender/functions/FN_cpp_type.hh
index 7ec60809194..ce02bfd05be 100644
--- a/source/blender/functions/FN_cpp_type.hh
+++ b/source/blender/functions/FN_cpp_type.hh
@@ -788,15 +788,12 @@ inline std::unique_ptr<const CPPType> create_cpp_type(StringRef name, const T &d
 }  // namespace blender::fn
 
 #define MAKE_CPP_TYPE(IDENTIFIER, TYPE_NAME) \
-  static TYPE_NAME default_value_##IDENTIFIER; \
-  static std::unique_ptr<const blender::fn::CPPType> CPPTYPE_##IDENTIFIER##_owner = \
-      blender::fn::create_cpp_type<TYPE_NAME>(STRINGIFY(IDENTIFIER), default_value_##IDENTIFIER); \
-  const blender::fn::CPPType &CPPType_##IDENTIFIER = *CPPTYPE_##IDENTIFIER##_owner; \
   template<> const blender::fn::CPPType &blender::fn::CPPType::get<TYPE_NAME>() \
   { \
-    /* This can happen when trying to access a CPPType during static storage initialization. */ \
-    BLI_assert(CPPTYPE_##IDENTIFIER##_owner.get() != nullptr); \
-    return CPPType_##IDENTIFIER; \
+    static TYPE_NAME default_value; \
+    static std::unique_ptr<const CPPType> cpp_type = blender::fn::create_cpp_type<TYPE_NAME>( \
+        STRINGIFY(IDENTIFIER), default_value); \
+    return *cpp_type; \
   }
 
 #endif /* __FN_CPP_TYPE_HH__ */
diff --git a/source/blender/functions/FN_cpp_types.hh b/source/blender/functions/FN_cpp_types.hh
deleted file mode 100644
index 63f6b49885f..00000000000
--- a/source/blender/functions/FN_cpp_types.hh
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
-#ifndef __FN_CPP_TYPES_HH__
-#define __FN_CPP_TYPES_HH__
-
-/** \file
- * \ingroup fn
- *
- * This header provides convenient access to CPPType instances for some core types like integer
- * types.
- */
-
-#include "FN_cpp_type.hh"
-
-namespace blender::fn {
-
-extern const CPPType &CPPType_bool;
-
-extern const CPPType &CPPType_float;
-extern const CPPType &CPPType_float3;
-extern const CPPType &CPPType_float4x4;
-
-extern const CPPType &CPPType_int32;
-extern const CPPType &CPPType_uint32;
-extern const CPPType &CPPType_uint8;
-
-extern const CPPType &CPPType_Color4f;
-extern const CPPType &CPPType_Color4b;
-
-extern const CPPType &CPPType_string;
-
-}  // namespace blender::fn
-
-#endif /* __FN_CPP_TYPES_HH__ */
diff --git a/source/blender/functions/intern/cpp_types.cc b/source/blender/functions/intern/cpp_types.cc
index 052278afd65..3bf4683d815 100644
--- a/source/blender/functions/intern/cpp_types.cc
+++ b/source/blender/functions/intern/cpp_types.cc
@@ -14,7 +14,7 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#include "FN_cpp_types.hh"
+#include "FN_cpp_type.hh"
 
 #include "BLI_color.hh"
 #include "BLI_float2.hh"
diff --git a/source/blender/simulation/intern/simulation_update.cc b/source/blender/simulation/intern/simulation_update.cc
index 918d433cb3d..95c7b122e7c 100644
--- a/source/blender/simulation/intern/simulation_update.cc
+++ b/source/blender/simulation/intern/simulation_update.cc
@@ -34,7 +34,6 @@
 #include "NOD_node_tree_multi_function.hh"
 
 #include "FN_attributes_ref.hh"
-#include "FN_cpp_types.hh"
 #include "FN_multi_function_network_evaluation.hh"
 #include "FN_multi_function_network_optimization.hh"
 
@@ -108,7 +107,7 @@ static Map<const fn::MFOutputSocket *, std::string> deduplicate_attribute_nodes(
   Array<std::string> attribute_names{amount, NoInitialization()};
   for (uint i : IndexRange(amount)) {
     params.add_uninitialized_single_output(
-        fn::GMutableSpan(fn::CPPType_string, attribute_names.data() + i, 1));
+        fn::GMutableSpan(fn::CPPType::get<std::string>(), attribute_names.data() + i, 1));
   }
 
   fn::MFContextBuilder context;
diff --git a/tests/gtests/functions/FN_array_spans_test.cc b/tests/gtests/functions/FN_array_spans_test.cc
index 988d48fa452..6912a62ff17 100644
--- a/tests/gtests/functions/FN_array_spans_test.cc
+++ b/tests/gtests/functions/FN_array_spans_test.cc
@@ -3,7 +3,6 @@
 #include "testing/testing.h"
 
 #include "FN_array_spans.hh"
-#include "FN_cpp_types.hh"
 #include "FN_generic_vector_array.hh"
 
 #include "BLI_array.hh"
@@ -77,7 +76,7 @@ TEST(virtual_array_span, MultipleArrayConstructor)
 
 TEST(generic_virtual_array_span, TypeConstructor)
 {
-  GVArraySpan span{CPPType_int32};
+  GVArraySpan span{CPPType::get<int32_t>()};
   EXPECT_EQ(span.size(), 0);
   EXPECT_TRUE(span.is_empty());
 
@@ -88,7 +87,7 @@ TEST(generic_virtual_array_span, TypeConstructor)
 TEST(generic_virtual_array_span, GSpanConstructor)
 {
   std::array<std::string, 3> values = {"hello", "world", "test"};
-  GVArraySpan span{GSpan(CPPType_string, values.data(), 3), 5};
+  GVArraySpan span{GSpan(CPPType::get<std::string>(), values.data(), 3), 5};
   EXPECT_EQ(span.size(), 5);
   EXPECT_FALSE(span.is_empty());
   EXPECT_EQ(span[0][0], values.data());
@@ -119,7 +118,7 @@ TEST(generic_virtual_array_span, IsSingleArray1)
 
 TEST(generic_virtual_array_span, IsSingleArray2)
 {
-  GVectorArray vectors{CPPType_int32, 3};
+  GVectorArray vectors{CPPType::get<int32_t>(), 3};
   GVectorArrayRef<int> vectors_ref = vectors;
   vectors_ref.append(1, 4);
 
diff --git a/tests/gtests/functions/FN_attributes_ref_test.cc b/tests/gtests/functions/FN_attributes_ref_test.cc
index 1c05bb930db..0b0afdc210f 100644
--- a/tests/gtests/functions/FN_attributes_ref_test.cc
+++ b/tests/gtests/functions/FN_attributes_ref_test.cc
@@ -2,7 +2,6 @@
 
 #include "BLI_float3.hh"
 #include "FN_attributes_ref.hh"
-#include "FN_cpp_types.hh"
 
 #include "testing/testing.h"
 
diff --git a/tests/gtests/functions/FN_cpp_type_test.cc b/tests/gtests/functions/FN_cpp_type_test.cc
index da5ce3416ce..78731f9c987 100644
--- a/tests/gtests/functions/FN_cpp_type_test.cc
+++ b/tests/gtests/functions/FN_cpp_type_test.cc
@@ -3,7 +3,6 @@
 #include "testing/testing.h"
 
 #include "FN_cpp_type.hh"
-#include "FN_cpp_types.hh"
 
 namespace blender::fn {
 
@@ -76,6 +75,8 @@ struct TestType {
 
 MAKE_CPP_TYPE(TestType, TestType)
 
+const CPPType &CPPType_TestType = CPPType::get<TestType>();
+
 TEST(cpp_type, Size)
 {
   EXPECT_EQ(CPPType_TestType.size(), sizeof(TestType));
@@ -312,7 +313,7 @@ TEST(cpp_type, DebugPrint)
 {
   int value = 42;
   std::stringstream ss;
-  CPPType_int32.debug_print((void *)&value, ss);
+  CPPType::get<int32_t>().debug_print((void *)&value, ss);
   std::string text = ss.str();
   EXPECT_EQ(text, "42");
 }
diff --git a/tests/gtests/functions/FN_generic_vector_array_test.cc b/tests/gtests/functions/FN_generic_vector_array_test.cc
index 7ce7b543218..cfca678ad27 100644
--- a/tests/gtests/functions/FN_generic_vector_array_test.cc
+++ b/tests/gtests/functions/FN_generic_vector_array_test.cc
@@ -1,6 +1,5 @@
 /* Apache License, Version 2.0 */
 
-#include "FN_cpp_types.hh"
 #include "FN_generic_vector_array.hh"
 
 #include "testing/testing.h"
@@ -9,19 +8,19 @@ namespace blender::fn {
 
 TEST(generic_vector_array, Constructor)
 {
-  GVectorArray vectors{CPPType_int32, 3};
+  GVectorArray vectors{CPPType::get<int32_t>(), 3};
   EXPECT_EQ(vectors.size(), 3);
   EXPECT_EQ(vectors.lengths().size(), 3);
   EXPECT_EQ(vectors.starts().size(), 3);
   EXPECT_EQ(vectors.lengths()[0], 0);
   EXPECT_EQ(vectors.lengths()[1], 0);
   EXPECT_EQ(vectors.lengths()[2], 0);
-  EXPECT_EQ(vectors.type(), CPPType_int32);
+  EXPECT_EQ(vectors.type(), CPPType::get<int32_t>());
 }
 
 TEST(generic_vector_array, Append)
 {
-  GVectorArray vectors{CPPType_string, 3};
+  GVectorArray vectors{CPPType::get<std::string>(), 3};
   std::string value = "hello";
   vectors.append(0, &value);
   value = "world";
@@ -39,7 +38,7 @@ TEST(generic_vector_array, Append)
 
 TEST(generic_vector_array, AsArraySpan)
 {
-  GVectorArray vectors{CPPType_int32, 3};
+  GVectorArray vectors{CPPType::get<int32_t>(), 3};
   int value = 3;
   vectors.append(0, &value);
   vectors.append(0, &value);
@@ -49,7 +48,7 @@ TEST(generic_vector_array, AsArraySpan)
   vectors.append(2, &value);
 
   GVArraySpan span = vectors;
-  EXPECT_EQ(span.type(), CPPType_int32);
+  EXPECT_EQ(span.type(), CPPType::get<int32_t>());
   EXPECT_EQ(span.size(), 3);
   EXPECT_EQ(span[0].size(), 2);
   EXPECT_EQ(span[1].size(), 0);
@@ -60,7 +59,7 @@ TEST(generic_vector_array, AsArraySpan)
 
 TEST(generic_vector_array, TypedRef)
 {
-  GVectorArray vectors{CPPType_int32, 4};
+  GVectorArray vectors{CPPType::get<int32_t>(), 4};
   GVectorArrayRef<int> ref = vectors.typed<int>();
   ref.append(0, 2);
   ref.append(0, 6);
@@ -84,7 +83,7 @@ TEST(generic_vector_array, TypedRef)
 
 TEST(generic_vector_array, Extend)
 {
-  GVectorArray vectors{CPPType_int32, 3};
+  GVectorArray vectors{CPPType::get<int32_t>(), 3};
   GVectorArrayRe

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list