[Bf-blender-cvs] [f028760b83c] master: BLI: make Map::Item and Map::MutableItem more accessible

Jacques Lucke noreply at git.blender.org
Thu Jun 11 10:49:17 CEST 2020


Commit: f028760b83c340b802adea7bab019ca14d95bad4
Author: Jacques Lucke
Date:   Thu Jun 11 10:48:52 2020 +0200
Branches: master
https://developer.blender.org/rBf028760b83c340b802adea7bab019ca14d95bad4

BLI: make Map::Item and Map::MutableItem more accessible

This makes it easier to write range-for loops over all items
in the map without using auto.

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

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

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

diff --git a/source/blender/blenlib/BLI_map.hh b/source/blender/blenlib/BLI_map.hh
index 9737367ebca..48cbcb4e3e9 100644
--- a/source/blender/blenlib/BLI_map.hh
+++ b/source/blender/blenlib/BLI_map.hh
@@ -745,6 +745,21 @@ class Map {
     }
   };
 
+  struct Item {
+    const Key &key;
+    const Value &value;
+  };
+
+  struct MutableItem {
+    const Key &key;
+    Value &value;
+
+    operator Item() const
+    {
+      return Item{key, value};
+    }
+  };
+
   class ItemIterator final : public BaseIterator<ItemIterator> {
    public:
     ItemIterator(const Slot *slots, uint32_t total_slots, uint32_t current_slot)
@@ -752,11 +767,6 @@ class Map {
     {
     }
 
-    struct Item {
-      const Key &key;
-      const Value &value;
-    };
-
     Item operator*() const
     {
       const Slot &slot = this->current_slot();
@@ -771,12 +781,7 @@ class Map {
     {
     }
 
-    struct Item {
-      const Key &key;
-      Value &value;
-    };
-
-    Item operator*() const
+    MutableItem operator*() const
     {
       Slot &slot = this->current_slot();
       return {*slot.key(), *slot.value()};
diff --git a/tests/gtests/blenlib/BLI_map_test.cc b/tests/gtests/blenlib/BLI_map_test.cc
index b14ba40ed7c..623055d5032 100644
--- a/tests/gtests/blenlib/BLI_map_test.cc
+++ b/tests/gtests/blenlib/BLI_map_test.cc
@@ -185,7 +185,8 @@ TEST(map, ItemIterator)
   blender::Set<float> values;
 
   uint iterations = 0;
-  for (auto item : map.items()) {
+  const Map<int, float> &const_map = map;
+  for (auto item : const_map.items()) {
     keys.add(item.key);
     values.add(item.value);
     iterations++;
@@ -228,6 +229,26 @@ TEST(map, MutableItemIterator)
   EXPECT_EQ(map.lookup(2), 3.0f);
 }
 
+TEST(map, MutableItemToItemConversion)
+{
+  Map<int, int> map;
+  map.add(3, 6);
+  map.add(2, 1);
+
+  Vector<int> keys, values;
+  for (Map<int, int>::Item item : map.items()) {
+    keys.append(item.key);
+    values.append(item.value);
+  }
+
+  EXPECT_EQ(keys.size(), 2);
+  EXPECT_EQ(values.size(), 2);
+  EXPECT_TRUE(keys.contains(3));
+  EXPECT_TRUE(keys.contains(2));
+  EXPECT_TRUE(values.contains(6));
+  EXPECT_TRUE(values.contains(1));
+}
+
 static float return_42()
 {
   return 42.0f;



More information about the Bf-blender-cvs mailing list