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 | 10 |
10 #include "third_party/base/numerics/safe_conversions.h" | 11 #include "third_party/base/numerics/safe_conversions.h" |
11 | 12 |
12 namespace pdfium { | 13 namespace pdfium { |
13 | 14 |
14 // 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. |
15 // Returns true if the key is in the collection. | 16 // Returns true if the key is in the collection. |
16 template <typename Collection, typename Key> | 17 template <typename Collection, typename Key> |
17 bool ContainsKey(const Collection& collection, const Key& key) { | 18 bool ContainsKey(const Collection& collection, const Key& key) { |
18 return collection.find(key) != collection.end(); | 19 return collection.find(key) != collection.end(); |
19 } | 20 } |
20 | 21 |
21 // 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. |
22 // Returns true if the value is in the collection. | 23 // Returns true if the value is in the collection. |
23 template <typename Collection, typename Value> | 24 template <typename Collection, typename Value> |
24 bool ContainsValue(const Collection& collection, const Value& value) { | 25 bool ContainsValue(const Collection& collection, const Value& value) { |
25 return std::find(collection.begin(), collection.end(), value) != | 26 return std::find(collection.begin(), collection.end(), value) != |
26 collection.end(); | 27 collection.end(); |
27 } | 28 } |
28 | 29 |
29 // Convenience routine for "int-fected" code, so that the stl collection | 30 // Convenience routine for "int-fected" code, so that the stl collection |
30 // size_t size() method return values will be checked. | 31 // size_t size() method return values will be checked. |
31 template <typename ResultType, typename Collection> | 32 template <typename ResultType, typename Collection> |
32 ResultType CollectionSize(const Collection& collection) { | 33 ResultType CollectionSize(const Collection& collection) { |
33 return pdfium::base::checked_cast<ResultType, size_t>(collection.size()); | 34 return pdfium::base::checked_cast<ResultType, size_t>(collection.size()); |
34 } | 35 } |
35 | 36 |
| 37 // Track the addition of an object to a set, removing it automatically when |
| 38 // the ScopedSetInsertion goes out of scope. |
| 39 template <typename T> |
| 40 class ScopedSetInsertion { |
| 41 public: |
| 42 ScopedSetInsertion(std::set<T>* org_set, T elem) |
| 43 : m_Set(org_set), m_Entry(elem) { |
| 44 m_Set->insert(m_Entry); |
| 45 } |
| 46 ~ScopedSetInsertion() { m_Set->erase(m_Entry); } |
| 47 |
| 48 private: |
| 49 std::set<T>* const m_Set; |
| 50 const T m_Entry; |
| 51 }; |
| 52 |
36 } // namespace pdfium | 53 } // namespace pdfium |
37 | 54 |
38 #endif // PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ | 55 #endif // PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_ |
OLD | NEW |