diff options
-rw-r--r-- | source/blender/blenlib/BLI_map.hh | 2 | ||||
-rw-r--r-- | source/blender/blenlib/tests/BLI_map_test.cc | 18 |
2 files changed, 19 insertions, 1 deletions
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; |