Merge pull request #19742 from AspirinSJL/map_move

Make Map<> movable
pull/19390/head
Mark D. Roth 6 years ago committed by GitHub
commit 06285b3f21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      src/core/lib/gprpp/map.h
  2. 29
      test/core/gprpp/map_test.cc

@ -55,8 +55,25 @@ class Map {
typedef Compare key_compare;
class iterator;
Map() = default;
~Map() { clear(); }
// Movable.
Map(Map&& other) : root_(other.root_), size_(other.size_) {
other.root_ = nullptr;
other.size_ = 0;
}
Map& operator=(Map&& other) {
if (this != &other) {
clear();
root_ = other.root_;
size_ = other.size_;
other.root_ = nullptr;
other.size_ = 0;
}
return *this;
}
T& operator[](key_type&& key);
T& operator[](const key_type& key);
iterator find(const key_type& k);

@ -437,6 +437,35 @@ TEST_F(MapTest, LowerBound) {
EXPECT_EQ(it, test_map.end());
}
// Test move ctor
TEST_F(MapTest, MoveCtor) {
Map<const char*, Payload, StringLess> test_map;
for (int i = 0; i < 5; i++) {
test_map.emplace(kKeys[i], Payload(i));
}
Map<const char*, Payload, StringLess> test_map2 = std::move(test_map);
for (int i = 0; i < 5; i++) {
EXPECT_EQ(test_map.end(), test_map.find(kKeys[i]));
EXPECT_EQ(i, test_map2.find(kKeys[i])->second.data());
}
}
// Test move assignment
TEST_F(MapTest, MoveAssignment) {
Map<const char*, Payload, StringLess> test_map;
for (int i = 0; i < 5; i++) {
test_map.emplace(kKeys[i], Payload(i));
}
Map<const char*, Payload, StringLess> test_map2;
test_map2.emplace("xxx", Payload(123));
test_map2 = std::move(test_map);
for (int i = 0; i < 5; i++) {
EXPECT_EQ(test_map.end(), test_map.find(kKeys[i]));
EXPECT_EQ(i, test_map2.find(kKeys[i])->second.data());
}
EXPECT_EQ(test_map2.end(), test_map2.find("xxx"));
}
} // namespace testing
} // namespace grpc_core

Loading…
Cancel
Save