Skip to content

Commit

Permalink
Merge pull request grpc#19742 from AspirinSJL/map_move
Browse files Browse the repository at this point in the history
Make Map<> movable
  • Loading branch information
markdroth committed Jul 25, 2019
2 parents 5b05969 + c3896fa commit 06285b3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/core/lib/gprpp/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions test/core/gprpp/map_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 06285b3

Please sign in to comment.