diff options
author | limasdf <limasdf@gmail.com> | 2015-12-01 19:23:23 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-12-02 03:24:14 +0000 |
commit | 93f52367cfe2ce652cebdbd213bc3f976d3c6c7b (patch) | |
tree | a94867fab127064079188b487b6b93c8886603e8 /base/containers | |
parent | 8fda38634cdd114540b47818f133f34e2b359910 (diff) | |
download | chromium_src-93f52367cfe2ce652cebdbd213bc3f976d3c6c7b.zip chromium_src-93f52367cfe2ce652cebdbd213bc3f976d3c6c7b.tar.gz chromium_src-93f52367cfe2ce652cebdbd213bc3f976d3c6c7b.tar.bz2 |
Kill ScopedPtrMap and friends
C++ 11 enables containers that contain move-only type, scoped_ptr.
So, Remove ScopedPtrMap and use std::map<key, scoped_ptr<Foo>> instead.
BUG=554291
Review URL: https://codereview.chromium.org/1486333002
Cr-Commit-Position: refs/heads/master@{#362615}
Diffstat (limited to 'base/containers')
-rw-r--r-- | base/containers/scoped_ptr_map.h | 146 | ||||
-rw-r--r-- | base/containers/scoped_ptr_map_unittest.cc | 244 |
2 files changed, 0 insertions, 390 deletions
diff --git a/base/containers/scoped_ptr_map.h b/base/containers/scoped_ptr_map.h deleted file mode 100644 index 25538b9..0000000 --- a/base/containers/scoped_ptr_map.h +++ /dev/null @@ -1,146 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BASE_CONTAINERS_SCOPED_PTR_MAP_H_ -#define BASE_CONTAINERS_SCOPED_PTR_MAP_H_ - -#include <functional> -#include <map> -#include <utility> - -#include "base/basictypes.h" -#include "base/macros.h" -#include "base/memory/scoped_ptr.h" -#include "base/stl_util.h" - -namespace base { - -// ScopedPtrMap provides a std::map that supports scoped_ptr values. It ensures -// that the map's values are properly deleted when removed from the map, or when -// the map is destroyed. -// -// |ScopedPtr| must be a type scoped_ptr<T>. This is for compatibility with -// std::map in C++11. -// -// TODO(http://crbug.com/554291): DEPRECATED: Use std::map instead (now that we -// have support for moveable types inside containers). -template <class Key, class ScopedPtr, class Compare = std::less<Key>> -class ScopedPtrMap { - using Container = std::map<Key, typename ScopedPtr::element_type*, Compare>; - public: - using allocator_type = typename Container::allocator_type; - using size_type = typename Container::size_type; - using difference_type = typename Container::difference_type; - 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; - - ScopedPtrMap() {} - ~ScopedPtrMap() { clear(); } - ScopedPtrMap(ScopedPtrMap&& other) { swap(other); } - - ScopedPtrMap& operator=(ScopedPtrMap&& rhs) { - swap(rhs); - return *this; - } - - const_iterator find(const Key& k) const { return data_.find(k); } - size_type count(const Key& k) const { return data_.count(k); } - - bool empty() const { return data_.empty(); } - size_t size() const { return data_.size(); } - - const_reverse_iterator rbegin() const { return data_.rbegin(); } - const_reverse_iterator rend() const { return data_.rend(); } - - const_iterator begin() const { return data_.begin(); } - const_iterator end() const { return data_.end(); } - - void swap(ScopedPtrMap& other) { data_.swap(other.data_); } - - void clear() { STLDeleteValues(&data_); } - - // Inserts |val| into the map, associated with |key|. - std::pair<const_iterator, bool> insert(const Key& key, ScopedPtr val) { - auto result = data_.insert(std::make_pair(key, val.get())); - if (result.second) - ::ignore_result(val.release()); - return result; - } - - // Inserts |val| into the map, associated with |key|. Overwrites any existing - // element at |key|. - void set(const Key& key, ScopedPtr val) { - typename ScopedPtr::element_type*& val_ref = data_[key]; - delete val_ref; - val_ref = val.release(); - } - - void erase(const_iterator position) { - DCHECK(position != end()); - delete position->second; - // Key-based lookup (cannot use const_iterator overload in C++03 library). - data_.erase(position->first); - } - - size_type erase(const Key& k) { - typename Container::iterator it = data_.find(k); - if (it == end()) - return 0; - - delete it->second; - data_.erase(it); - return 1; - } - - void erase(const_iterator first, const_iterator last) { - STLDeleteContainerPairSecondPointers(first, last); - // Need non-const iterators as required by the C++03 library. - data_.erase(ConstIteratorToIterator(first), ConstIteratorToIterator(last)); - } - - // Like |erase()|, but returns the element instead of deleting it. - ScopedPtr take_and_erase(const_iterator position) { - DCHECK(position != end()); - if (position == end()) - return ScopedPtr(); - - ScopedPtr ret(position->second); - // Key-based lookup (cannot use const_iterator overload in C++03 library). - data_.erase(position->first); - return ret; - } - - // Like |erase()|, but returns the element instead of deleting it. - ScopedPtr take_and_erase(const Key& k) { - typename Container::iterator it = data_.find(k); - if (it == end()) - return ScopedPtr(); - - ScopedPtr ret(it->second); - data_.erase(it); - return ret; - } - - private: - Container data_; - - typename Container::iterator ConstIteratorToIterator(const_iterator it) { - // This is the only way to convert a const iterator to a non-const iterator - // in C++03 (get the key and do the lookup again). - if (it == data_.end()) - return data_.end(); - return data_.find(it->first); - }; - - DISALLOW_COPY_AND_ASSIGN(ScopedPtrMap); -}; - -} // namespace base - -#endif // BASE_CONTAINERS_SCOPED_PTR_MAP_H_ diff --git a/base/containers/scoped_ptr_map_unittest.cc b/base/containers/scoped_ptr_map_unittest.cc deleted file mode 100644 index f8a9779..0000000 --- a/base/containers/scoped_ptr_map_unittest.cc +++ /dev/null @@ -1,244 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/containers/scoped_ptr_map.h" - -#include <functional> -#include <map> -#include <utility> - -#include "base/bind.h" -#include "base/callback.h" -#include "base/memory/scoped_ptr.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace base { -namespace { - -namespace namespace_with_ignore_result { - -class Value {}; - -template <typename T> -void ignore_result(const T&) {} - -} // namespace namespace_with_ignore_result - -// A ScopedDestroyer sets a Boolean to true upon destruction. -class ScopedDestroyer { - public: - ScopedDestroyer(bool* destroyed) : destroyed_(destroyed) { - *destroyed_ = false; - } - - ~ScopedDestroyer() { *destroyed_ = true; } - - private: - bool* destroyed_; -}; - -TEST(ScopedPtrMapTest, Insert) { - bool destroyed1 = false; - bool destroyed2 = false; - { - ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map; - - // Insert to new key. - ScopedDestroyer* elem1 = new ScopedDestroyer(&destroyed1); - EXPECT_FALSE(destroyed1); - EXPECT_TRUE(scoped_map.insert(0, make_scoped_ptr(elem1)).second); - EXPECT_EQ(elem1, scoped_map.find(0)->second); - EXPECT_FALSE(destroyed1); - - // Insert to existing key. - ScopedDestroyer* elem2 = new ScopedDestroyer(&destroyed2); - EXPECT_FALSE(destroyed2); - EXPECT_FALSE(scoped_map.insert(0, make_scoped_ptr(elem2)).second); - EXPECT_EQ(elem1, scoped_map.find(0)->second); - - EXPECT_FALSE(destroyed1); - EXPECT_TRUE(destroyed2); - } - EXPECT_TRUE(destroyed1); -} - -TEST(ScopedPtrMapTest, Set) { - bool destroyed1 = false; - bool destroyed2 = false; - { - ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map; - - // Set a new key. - ScopedDestroyer* elem1 = new ScopedDestroyer(&destroyed1); - EXPECT_FALSE(destroyed1); - scoped_map.set(0, make_scoped_ptr(elem1)); - EXPECT_EQ(elem1, scoped_map.find(0)->second); - EXPECT_FALSE(destroyed1); - - // Set to replace an existing key. - ScopedDestroyer* elem2 = new ScopedDestroyer(&destroyed2); - EXPECT_FALSE(destroyed2); - scoped_map.set(0, make_scoped_ptr(elem2)); - EXPECT_EQ(elem2, scoped_map.find(0)->second); - - EXPECT_TRUE(destroyed1); - EXPECT_FALSE(destroyed2); - } - EXPECT_TRUE(destroyed1); - EXPECT_TRUE(destroyed2); -} - -TEST(ScopedPtrMapTest, EraseIterator) { - bool destroyed = false; - ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map; - scoped_map.insert(0, make_scoped_ptr(new ScopedDestroyer(&destroyed))); - EXPECT_FALSE(destroyed); - scoped_map.erase(scoped_map.find(0)); - EXPECT_TRUE(destroyed); - EXPECT_TRUE(scoped_map.empty()); -} - -TEST(ScopedPtrMapTest, EraseKey) { - bool destroyed = false; - ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map; - scoped_map.insert(0, make_scoped_ptr(new ScopedDestroyer(&destroyed))); - EXPECT_FALSE(destroyed); - EXPECT_EQ(1u, scoped_map.erase(0)); - EXPECT_TRUE(destroyed); - EXPECT_TRUE(scoped_map.empty()); - - // Test erase of a non-existent key. - EXPECT_EQ(0u, scoped_map.erase(7)); -} - -TEST(ScopedPtrMapTest, EraseRange) { - bool destroyed1 = false; - bool destroyed2 = false; - ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map; - - scoped_map.insert(0, make_scoped_ptr(new ScopedDestroyer(&destroyed1))); - EXPECT_FALSE(destroyed1); - - scoped_map.insert(1, make_scoped_ptr(new ScopedDestroyer(&destroyed2))); - EXPECT_FALSE(destroyed2); - - scoped_map.erase(scoped_map.find(0), scoped_map.end()); - EXPECT_TRUE(destroyed1); - EXPECT_TRUE(destroyed2); - EXPECT_TRUE(scoped_map.empty()); -} - -TEST(ScopedPtrMapTest, TakeAndErase) { - bool destroyed = false; - ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map; - ScopedDestroyer* elem = new ScopedDestroyer(&destroyed); - scoped_map.insert(0, make_scoped_ptr(elem)); - EXPECT_EQ(elem, scoped_map.find(0)->second); - EXPECT_FALSE(destroyed); - scoped_ptr<ScopedDestroyer> object = scoped_map.take_and_erase(0); - EXPECT_EQ(elem, object.get()); - EXPECT_FALSE(destroyed); - EXPECT_TRUE(scoped_map.empty()); - object.reset(); - EXPECT_TRUE(destroyed); -} - -TEST(ScopedPtrMapTest, Clear) { - bool destroyed = false; - ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map; - scoped_map.insert(0, make_scoped_ptr(new ScopedDestroyer(&destroyed))); - EXPECT_FALSE(destroyed); - scoped_map.clear(); - EXPECT_TRUE(destroyed); - EXPECT_TRUE(scoped_map.empty()); -} - -TEST(ScopedPtrMapTest, Compare) { - // Construct a ScopedPtrMap with a custom comparison function. - ScopedPtrMap<int, scoped_ptr<int>, std::greater<int>> scoped_map1; - scoped_map1.insert(0, make_scoped_ptr(new int(0))); - scoped_map1.insert(1, make_scoped_ptr(new int(0))); - - auto it = scoped_map1.begin(); - EXPECT_EQ(1, it->first); - ++it; - EXPECT_EQ(0, it->first); - - // Test the move constructor. - ScopedPtrMap<int, scoped_ptr<int>, std::greater<int>> scoped_map2( - std::move(scoped_map1)); - EXPECT_EQ(2u, scoped_map2.size()); - EXPECT_TRUE(scoped_map1.empty()); - - // Test move assignment. - scoped_map1 = std::move(scoped_map2); - EXPECT_EQ(2u, scoped_map1.size()); - EXPECT_TRUE(scoped_map2.empty()); - - // Test swap. - scoped_map2.swap(scoped_map1); - EXPECT_EQ(2u, scoped_map2.size()); - EXPECT_TRUE(scoped_map1.empty()); -} - -TEST(ScopedPtrMapTest, Scope) { - bool destroyed = false; - { - ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map; - scoped_map.insert(0, make_scoped_ptr(new ScopedDestroyer(&destroyed))); - EXPECT_FALSE(destroyed); - } - EXPECT_TRUE(destroyed); -} - -TEST(ScopedPtrMapTest, MoveConstruct) { - bool destroyed = false; - { - ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map; - ScopedDestroyer* elem = new ScopedDestroyer(&destroyed); - scoped_map.insert(0, make_scoped_ptr(elem)); - EXPECT_EQ(elem, scoped_map.find(0)->second); - EXPECT_FALSE(destroyed); - EXPECT_FALSE(scoped_map.empty()); - - ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map_copy( - std::move(scoped_map)); - EXPECT_TRUE(scoped_map.empty()); - EXPECT_FALSE(scoped_map_copy.empty()); - EXPECT_EQ(elem, scoped_map_copy.find(0)->second); - EXPECT_FALSE(destroyed); - } - EXPECT_TRUE(destroyed); -} - -TEST(ScopedPtrMapTest, MoveAssign) { - bool destroyed = false; - { - ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map; - ScopedDestroyer* elem = new ScopedDestroyer(&destroyed); - scoped_map.insert(0, make_scoped_ptr(elem)); - EXPECT_EQ(elem, scoped_map.find(0)->second); - EXPECT_FALSE(destroyed); - EXPECT_FALSE(scoped_map.empty()); - - ScopedPtrMap<int, scoped_ptr<ScopedDestroyer>> scoped_map_assign; - scoped_map_assign = std::move(scoped_map); - EXPECT_TRUE(scoped_map.empty()); - EXPECT_FALSE(scoped_map_assign.empty()); - EXPECT_EQ(elem, scoped_map_assign.find(0)->second); - EXPECT_FALSE(destroyed); - } - EXPECT_TRUE(destroyed); -} - -// Test that using a value type from a namespace containing an ignore_result -// function compiles correctly. -TEST(ScopedPtrMapTest, IgnoreResultCompile) { - ScopedPtrMap<int, scoped_ptr<namespace_with_ignore_result::Value>> scoped_map; - scoped_map.insert(1, - make_scoped_ptr(new namespace_with_ignore_result::Value)); -} - -} // namespace -} // namespace base |