diff options
author | mgiuca <mgiuca@chromium.org> | 2015-06-15 20:30:10 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-16 03:30:37 +0000 |
commit | 24486e2517f5bbb84bc79aa908d81ec6eb34d676 (patch) | |
tree | 2003f64c12b7efef8a05a21e9b12463f2cfac675 /base | |
parent | 31cda26970725dbe9e7450e908887c828cd49735 (diff) | |
download | chromium_src-24486e2517f5bbb84bc79aa908d81ec6eb34d676.zip chromium_src-24486e2517f5bbb84bc79aa908d81ec6eb34d676.tar.gz chromium_src-24486e2517f5bbb84bc79aa908d81ec6eb34d676.tar.bz2 |
ScopedPtrMap: Added Compare template parameter.
Allows clients to specify a custom comparison function (as in std::map).
Also added |mapped_type| and |key_compare| typedefs in ScopedPtrMap, for
compatibility with std::map.
BUG=478594
Review URL: https://codereview.chromium.org/1179083006
Cr-Commit-Position: refs/heads/master@{#334549}
Diffstat (limited to 'base')
-rw-r--r-- | base/containers/scoped_ptr_map.h | 7 | ||||
-rw-r--r-- | base/containers/scoped_ptr_map_unittest.cc | 14 |
2 files changed, 19 insertions, 2 deletions
diff --git a/base/containers/scoped_ptr_map.h b/base/containers/scoped_ptr_map.h index 19a1153..259076f 100644 --- a/base/containers/scoped_ptr_map.h +++ b/base/containers/scoped_ptr_map.h @@ -5,6 +5,7 @@ #ifndef BASE_CONTAINERS_SCOPED_PTR_MAP_H_ #define BASE_CONTAINERS_SCOPED_PTR_MAP_H_ +#include <functional> #include <map> #include <utility> @@ -19,11 +20,11 @@ // // |ScopedPtr| must be a type scoped_ptr<T>. This is for compatibility with // std::map in C++11. -template <class Key, class ScopedPtr> +template <class Key, class ScopedPtr, class Compare = std::less<Key>> class ScopedPtrMap { MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(ScopedPtrMap) - using Container = std::map<Key, typename ScopedPtr::element_type*>; + using Container = std::map<Key, typename ScopedPtr::element_type*, Compare>; public: using allocator_type = typename Container::allocator_type; @@ -32,6 +33,8 @@ class ScopedPtrMap { using reference = typename Container::reference; using const_reference = typename Container::const_reference; using key_type = typename Container::key_type; + using mapped_type = ScopedPtr; + using key_compare = typename Container::key_compare; using const_iterator = typename Container::const_iterator; using const_reverse_iterator = typename Container::const_reverse_iterator; diff --git a/base/containers/scoped_ptr_map_unittest.cc b/base/containers/scoped_ptr_map_unittest.cc index ef70440..1d8f179 100644 --- a/base/containers/scoped_ptr_map_unittest.cc +++ b/base/containers/scoped_ptr_map_unittest.cc @@ -4,6 +4,7 @@ #include "base/containers/scoped_ptr_map.h" +#include <functional> #include <map> #include <utility> @@ -143,6 +144,19 @@ TEST(ScopedPtrMapTest, Clear) { EXPECT_TRUE(scoped_map.empty()); } +TEST(ScopedPtrMapTest, Compare) { + // Construct a ScopedPtrMap with a custom comparison function. + bool destroyed = false; + ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>, std::greater<int>> scoped_map; + scoped_map.insert(0, make_scoped_ptr(new ScopedDestroyer(&destroyed))); + scoped_map.insert(1, make_scoped_ptr(new ScopedDestroyer(&destroyed))); + + auto it = scoped_map.begin(); + EXPECT_EQ(1, it->first); + ++it; + EXPECT_EQ(0, it->first); +} + TEST(ScopedPtrMapTest, Scope) { bool destroyed = false; { |