• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_
6 #define PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_
7 
8 #include <algorithm>
9 #include <iterator>
10 #include <memory>
11 #include <set>
12 
13 #include "third_party/base/numerics/safe_conversions.h"
14 
15 namespace pdfium {
16 
17 // Test to see if a set, map, hash_set or hash_map contains a particular key.
18 // Returns true if the key is in the collection.
19 template <typename Collection, typename Key>
ContainsKey(const Collection & collection,const Key & key)20 bool ContainsKey(const Collection& collection, const Key& key) {
21   return collection.find(key) != collection.end();
22 }
23 
24 // Test to see if a collection like a vector contains a particular value.
25 // Returns true if the value is in the collection.
26 template <typename Collection, typename Value>
ContainsValue(const Collection & collection,const Value & value)27 bool ContainsValue(const Collection& collection, const Value& value) {
28   return std::find(std::begin(collection), std::end(collection), value) !=
29          std::end(collection);
30 }
31 
32 // Means of generating a key for searching STL collections of std::unique_ptr
33 // that avoids the side effect of deleting the pointer.
34 template <class T>
35 class FakeUniquePtr : public std::unique_ptr<T> {
36  public:
37   using std::unique_ptr<T>::unique_ptr;
~FakeUniquePtr()38   ~FakeUniquePtr() { std::unique_ptr<T>::release(); }
39 };
40 
41 // Convenience routine for "int-fected" code, so that the stl collection
42 // size_t size() method return values will be checked.
43 template <typename ResultType, typename Collection>
CollectionSize(const Collection & collection)44 ResultType CollectionSize(const Collection& collection) {
45   return pdfium::base::checked_cast<ResultType>(collection.size());
46 }
47 
48 // Convenience routine for "int-fected" code, to handle signed indicies. The
49 // compiler can deduce the type, making this more convenient than the above.
50 template <typename IndexType, typename Collection>
IndexInBounds(const Collection & collection,IndexType index)51 bool IndexInBounds(const Collection& collection, IndexType index) {
52   return index >= 0 && index < CollectionSize<IndexType>(collection);
53 }
54 
55 // Track the addition of an object to a set, removing it automatically when
56 // the ScopedSetInsertion goes out of scope.
57 template <typename T>
58 class ScopedSetInsertion {
59  public:
ScopedSetInsertion(std::set<T> * org_set,T elem)60   ScopedSetInsertion(std::set<T>* org_set, T elem)
61       : m_Set(org_set), m_Entry(elem) {
62     m_Set->insert(m_Entry);
63   }
~ScopedSetInsertion()64   ~ScopedSetInsertion() { m_Set->erase(m_Entry); }
65 
66  private:
67   std::set<T>* const m_Set;
68   const T m_Entry;
69 };
70 
71 // std::clamp(), some day.
72 template <class T>
clamp(const T & v,const T & lo,const T & hi)73 constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
74   return std::min(std::max(v, lo), hi);
75 }
76 
77 }  // namespace pdfium
78 
79 #endif  // PDFIUM_THIRD_PARTY_BASE_STL_UTIL_H_
80