[Bf-blender-cvs] [09be2a83580] master: BLI: use forwarding reference in Map

Jacques Lucke noreply at git.blender.org
Thu Oct 29 15:22:30 CET 2020


Commit: 09be2a83580faaf4fa9b87da2632ab51d0934235
Author: Jacques Lucke
Date:   Thu Oct 29 15:19:32 2020 +0100
Branches: master
https://developer.blender.org/rB09be2a83580faaf4fa9b87da2632ab51d0934235

BLI: use forwarding reference in Map

The is necessary when Map.add_or_modify is called with callbacks that
return a reference.

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

M	source/blender/blenlib/BLI_map.hh
M	source/blender/blenlib/tests/BLI_map_test.cc

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

diff --git a/source/blender/blenlib/BLI_map.hh b/source/blender/blenlib/BLI_map.hh
index ee20cb40ade..9480af89107 100644
--- a/source/blender/blenlib/BLI_map.hh
+++ b/source/blender/blenlib/BLI_map.hh
@@ -1016,7 +1016,7 @@ class Map {
           return;
         }
         else {
-          auto return_value = create_value(value_ptr);
+          auto &&return_value = create_value(value_ptr);
           slot.occupy_no_value(std::forward<ForwardKey>(key), hash);
           occupied_and_removed_slots_++;
           return return_value;
diff --git a/source/blender/blenlib/tests/BLI_map_test.cc b/source/blender/blenlib/tests/BLI_map_test.cc
index 91c6335b949..e61d638c681 100644
--- a/source/blender/blenlib/tests/BLI_map_test.cc
+++ b/source/blender/blenlib/tests/BLI_map_test.cc
@@ -295,6 +295,24 @@ TEST(map, AddOrModify)
   EXPECT_EQ(map.lookup(1), 15.0f);
 }
 
+TEST(map, AddOrModifyReference)
+{
+  Map<int, std::unique_ptr<int>> map;
+  auto create_func = [](std::unique_ptr<int> *value) -> int & {
+    new (value) std::unique_ptr<int>(new int{10});
+    return **value;
+  };
+  auto modify_func = [](std::unique_ptr<int> *value) -> int & {
+    **value += 5;
+    return **value;
+  };
+  EXPECT_EQ(map.add_or_modify(1, create_func, modify_func), 10);
+  int &a = map.add_or_modify(1, create_func, modify_func);
+  EXPECT_EQ(a, 15);
+  a = 100;
+  EXPECT_EQ(*map.lookup(1), 100);
+}
+
 TEST(map, AddOverwrite)
 {
   Map<int, float> map;



More information about the Bf-blender-cvs mailing list