You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by dr...@apache.org on 2016/07/11 18:25:11 UTC
[3/4] incubator-kudu git commit: Add a FindPointeeOrNull method to
map-util
Add a FindPointeeOrNull method to map-util
This adds a new FindPointeeOrNull to map-util to use with maps having smart
pointers as values (like shared_ptr or unique_ptr). The new method returns
a raw pointer to the value contained in in the smart pointer, if the key exits
or nullptr if it doesn't.
Change-Id: I9a5d26bdd39e8d12382eb460cb75e04b645e3b2d
Reviewed-on: http://gerrit.cloudera.org:8080/3594
Reviewed-by: David Ribeiro Alves <dr...@apache.org>
Reviewed-by: Todd Lipcon <to...@apache.org>
Tested-by: Kudu Jenkins
Project: http://git-wip-us.apache.org/repos/asf/incubator-kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kudu/commit/48fc399e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kudu/tree/48fc399e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kudu/diff/48fc399e
Branch: refs/heads/master
Commit: 48fc399ebc76e5b4958a809a72f2674def5cebb7
Parents: a22795a
Author: dralves <dr...@apache.org>
Authored: Thu Jul 7 17:56:02 2016 -0700
Committer: David Ribeiro Alves <dr...@apache.org>
Committed: Mon Jul 11 18:10:03 2016 +0000
----------------------------------------------------------------------
src/kudu/gutil/map-util.h | 15 +++++++++++++++
src/kudu/util/map-util-test.cc | 12 ++++++++++++
2 files changed, 27 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/48fc399e/src/kudu/gutil/map-util.h
----------------------------------------------------------------------
diff --git a/src/kudu/gutil/map-util.h b/src/kudu/gutil/map-util.h
index 8c7574e..61ffd4f 100644
--- a/src/kudu/gutil/map-util.h
+++ b/src/kudu/gutil/map-util.h
@@ -236,6 +236,21 @@ FindPtrOrNull(Collection& collection, // NOLINT
return it->second;
}
+// FindPtrOrNull like function for maps whose value is a smart pointer like shared_ptr or
+// unique_ptr.
+// Returns the raw pointer contained in the smart pointer for the first found key, if it exists,
+// or null if it doesn't.
+template <class Collection>
+typename Collection::mapped_type::element_type*
+FindPointeeOrNull(const Collection& collection, // NOLINT,
+ const typename Collection::key_type& key) {
+ auto it = collection.find(key);
+ if (it == collection.end()) {
+ return nullptr;
+ }
+ return it->second.get();
+}
+
// Finds the value associated with the given key and copies it to *value (if not
// NULL). Returns false if the key was not found, true otherwise.
template <class Collection, class Key, class Value>
http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/48fc399e/src/kudu/util/map-util-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/map-util-test.cc b/src/kudu/util/map-util-test.cc
index 270c509..ef07e58 100644
--- a/src/kudu/util/map-util-test.cc
+++ b/src/kudu/util/map-util-test.cc
@@ -65,4 +65,16 @@ TEST(ComputeIfAbsentTest, TestComputeIfAbsentAndReturnAbsense) {
ASSERT_EQ(*result2.first, "hello_world");
}
+TEST(FindPointeeOrNullTest, TestFindPointeeOrNull) {
+ map<string, unique_ptr<string>> my_map;
+ auto iter = my_map.emplace("key", unique_ptr<string>(new string("hello_world")));
+ ASSERT_TRUE(iter.second);
+ string* value = FindPointeeOrNull(my_map, "key");
+ ASSERT_TRUE(value != nullptr);
+ ASSERT_EQ(*value, "hello_world");
+ my_map.erase(iter.first);
+ value = FindPointeeOrNull(my_map, "key");
+ ASSERT_TRUE(value == nullptr);
+}
+
} // namespace kudu