-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Base] Introduce FindOrNull() and FindPtrOrNull()
These helpers simplify the process of finding the stored value for a given key in STL-like maps. Bug: None Change-Id: I0976e6d66ec98269910757990414008ff95a9031 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4926655 Reviewed-by: Daniel Cheng <dcheng@chromium.org> Commit-Queue: Andrew Rayskiy <greengrape@google.com> Owners-Override: Daniel Cheng <dcheng@chromium.org> Cr-Commit-Position: refs/heads/main@{#1209315}
- Loading branch information
Andrew Rayskiy
authored and
Chromium LUCI CQ
committed
Oct 13, 2023
1 parent
9b8c074
commit 3dfec41
Showing
11 changed files
with
173 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef BASE_CONTAINERS_MAP_UTIL_H_ | ||
#define BASE_CONTAINERS_MAP_UTIL_H_ | ||
|
||
#include <memory> | ||
|
||
namespace base { | ||
|
||
namespace internal { | ||
|
||
template <typename Map> | ||
using MappedType = typename Map::mapped_type; | ||
|
||
} // namespace internal | ||
|
||
// Returns a pointer to the const value associated with the given key if it | ||
// exists, or null otherwise. | ||
template <typename Map, typename Key> | ||
constexpr const internal::MappedType<Map>* FindOrNull(const Map& map, | ||
const Key& key) { | ||
auto it = map.find(key); | ||
return it != map.end() ? &it->second : nullptr; | ||
} | ||
|
||
// Returns a pointer to the value associated with the given key if it exists, or | ||
// null otherwise. | ||
template <typename Map, typename Key> | ||
constexpr internal::MappedType<Map>* FindOrNull(Map& map, const Key& key) { | ||
auto it = map.find(key); | ||
return it != map.end() ? &it->second : nullptr; | ||
} | ||
|
||
// Returns the const pointer value associated with the given key. If none is | ||
// found, null is returned. The function is designed to be used with a map of | ||
// keys to pointers or smart pointers. | ||
// | ||
// This function does not distinguish between a missing key and a key mapped | ||
// to a null value. | ||
template <typename Map, | ||
typename Key, | ||
typename MappedElementType = | ||
std::pointer_traits<internal::MappedType<Map>>::element_type> | ||
constexpr const MappedElementType* FindPtrOrNull(const Map& map, | ||
const Key& key) { | ||
auto it = map.find(key); | ||
return it != map.end() ? std::to_address(it->second) : nullptr; | ||
} | ||
|
||
// Returns the pointer value associated with the given key. If none is found, | ||
// null is returned. The function is designed to be used with a map of keys to | ||
// pointers or smart pointers. | ||
// | ||
// This function does not distinguish between a missing key and a key mapped | ||
// to a null value. | ||
template <typename Map, | ||
typename Key, | ||
typename MappedElementType = | ||
std::pointer_traits<internal::MappedType<Map>>::element_type> | ||
constexpr MappedElementType* FindPtrOrNull(Map& map, const Key& key) { | ||
auto it = map.find(key); | ||
return it != map.end() ? std::to_address(it->second) : nullptr; | ||
} | ||
|
||
} // namespace base | ||
|
||
#endif // BASE_CONTAINERS_MAP_UTIL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef BASE_CONTAINERS_MAP_UTIL_UNITTEST_CC_ | ||
#define BASE_CONTAINERS_MAP_UTIL_UNITTEST_CC_ | ||
|
||
#include "base/containers/map_util.h" | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "base/containers/flat_map.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace base { | ||
|
||
namespace { | ||
|
||
using testing::AllOf; | ||
using testing::Eq; | ||
using testing::Pointee; | ||
|
||
constexpr char kKey[] = "key"; | ||
constexpr char kValue[] = "value"; | ||
constexpr char kMissingKey[] = "missing_key"; | ||
|
||
using StringToStringMap = base::flat_map<std::string, std::string>; | ||
using StringToStringPtrMap = base::flat_map<std::string, std::string*>; | ||
using StringToStringUniquePtrMap = | ||
base::flat_map<std::string, std::unique_ptr<std::string>>; | ||
|
||
TEST(MapUtilTest, FindOrNull) { | ||
StringToStringMap mapping({{kKey, kValue}}); | ||
|
||
EXPECT_THAT(FindOrNull(mapping, kKey), Pointee(Eq(kValue))); | ||
EXPECT_EQ(FindOrNull(mapping, kMissingKey), nullptr); | ||
} | ||
|
||
TEST(MapUtilTest, FindPtrOrNullForPointers) { | ||
auto val = std::make_unique<std::string>(kValue); | ||
|
||
StringToStringPtrMap mapping({{kKey, val.get()}}); | ||
|
||
EXPECT_THAT(FindPtrOrNull(mapping, kKey), | ||
AllOf(Eq(val.get()), Pointee(Eq(kValue)))); | ||
EXPECT_EQ(FindPtrOrNull(mapping, kMissingKey), nullptr); | ||
} | ||
|
||
TEST(MapUtilTest, FindPtrOrNullForPointerLikeValues) { | ||
StringToStringUniquePtrMap mapping; | ||
mapping.insert({kKey, std::make_unique<std::string>(kValue)}); | ||
|
||
EXPECT_THAT(FindPtrOrNull(mapping, kKey), Pointee(Eq(kValue))); | ||
EXPECT_EQ(FindPtrOrNull(mapping, kMissingKey), nullptr); | ||
} | ||
|
||
} // namespace | ||
|
||
} // namespace base | ||
|
||
#endif // BASE_CONTAINERS_MAP_UTIL_UNITTEST_CC_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.