OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ | 5 #ifndef PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ |
6 #define PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ | 6 #define PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "third_party/base/numerics/safe_conversions.h" | 11 #include "third_party/base/numerics/safe_conversions.h" |
12 | 12 |
13 namespace pdfium { | 13 namespace pdfium { |
14 | 14 |
15 // Test to see if a set, map, hash_set or hash_map contains a particular key. | 15 // Test to see if a set, map, hash_set or hash_map contains a particular key. |
16 // Returns true if the key is in the collection. | 16 // Returns true if the key is in the collection. |
17 template <typename Collection, typename Key> | 17 template <typename Collection, typename Key> |
18 bool ContainsKey(const Collection& collection, const Key& key) { | 18 bool ContainsKey(const Collection& collection, const Key& key) { |
19 return collection.find(key) != collection.end(); | 19 return collection.find(key) != collection.end(); |
20 } | 20 } |
21 | 21 |
22 // Test to see if a collection like a vector contains a particular value. | 22 // Test to see if a collection like a vector contains a particular value. |
23 // Returns true if the value is in the collection. | 23 // Returns true if the value is in the collection. |
24 template <typename Collection, typename Value> | 24 template <typename Collection, typename Value> |
25 bool ContainsValue(const Collection& collection, const Value& value) { | 25 bool ContainsValue(const Collection& collection, const Value& value) { |
26 return std::find(collection.begin(), collection.end(), value) != | 26 return std::find(collection.begin(), collection.end(), value) != |
27 collection.end(); | 27 collection.end(); |
28 } | 28 } |
29 | 29 |
| 30 // Means of generating a key for searching STL collections of std::unique_ptr |
| 31 // that avoids the side effect of deleting the pointer. |
| 32 template <class T> |
| 33 class FakeUniquePtr : public std::unique_ptr<T> { |
| 34 public: |
| 35 using std::unique_ptr<T>::unique_ptr; |
| 36 ~FakeUniquePtr() { std::unique_ptr<T>::release(); } |
| 37 }; |
| 38 |
30 // Convenience routine for "int-fected" code, so that the stl collection | 39 // Convenience routine for "int-fected" code, so that the stl collection |
31 // size_t size() method return values will be checked. | 40 // size_t size() method return values will be checked. |
32 template <typename ResultType, typename Collection> | 41 template <typename ResultType, typename Collection> |
33 ResultType CollectionSize(const Collection& collection) { | 42 ResultType CollectionSize(const Collection& collection) { |
34 return pdfium::base::checked_cast<ResultType, size_t>(collection.size()); | 43 return pdfium::base::checked_cast<ResultType, size_t>(collection.size()); |
35 } | 44 } |
36 | 45 |
37 // Track the addition of an object to a set, removing it automatically when | 46 // Track the addition of an object to a set, removing it automatically when |
38 // the ScopedSetInsertion goes out of scope. | 47 // the ScopedSetInsertion goes out of scope. |
39 template <typename T> | 48 template <typename T> |
40 class ScopedSetInsertion { | 49 class ScopedSetInsertion { |
41 public: | 50 public: |
42 ScopedSetInsertion(std::set<T>* org_set, T elem) | 51 ScopedSetInsertion(std::set<T>* org_set, T elem) |
43 : m_Set(org_set), m_Entry(elem) { | 52 : m_Set(org_set), m_Entry(elem) { |
44 m_Set->insert(m_Entry); | 53 m_Set->insert(m_Entry); |
45 } | 54 } |
46 ~ScopedSetInsertion() { m_Set->erase(m_Entry); } | 55 ~ScopedSetInsertion() { m_Set->erase(m_Entry); } |
47 | 56 |
48 private: | 57 private: |
49 std::set<T>* const m_Set; | 58 std::set<T>* const m_Set; |
50 const T m_Entry; | 59 const T m_Entry; |
51 }; | 60 }; |
52 | 61 |
53 } // namespace pdfium | 62 } // namespace pdfium |
54 | 63 |
55 #endif // PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ | 64 #endif // PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ |
OLD | NEW |