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 THIRD_PARTY_BASE_STL_UTIL_H_
6 #define THIRD_PARTY_BASE_STL_UTIL_H_
7
8 #include <algorithm>
9 #include <iterator>
10 #include <memory>
11 #include <set>
12 #include <vector>
13
14 #include "third_party/base/numerics/safe_conversions.h"
15 #include "third_party/base/numerics/safe_math.h"
16
17 namespace pdfium {
18
19 // C++11 implementation of C++17's std::size():
20 // http://en.cppreference.com/w/cpp/iterator/size
21 template <typename Container>
22 constexpr auto size(const Container& c) -> decltype(c.size()) {
23 return c.size();
24 }
25
26 template <typename T, size_t N>
size(const T (& array)[N])27 constexpr size_t size(const T (&array)[N]) noexcept {
28 return N;
29 }
30
31 // Test to see if a set, map, hash_set or hash_map contains a particular key.
32 // Returns true if the key is in the collection.
33 template <typename Collection, typename Key>
ContainsKey(const Collection & collection,const Key & key)34 bool ContainsKey(const Collection& collection, const Key& key) {
35 return collection.find(key) != collection.end();
36 }
37
38 // Test to see if a collection like a vector contains a particular value.
39 // Returns true if the value is in the collection.
40 template <typename Collection, typename Value>
ContainsValue(const Collection & collection,const Value & value)41 bool ContainsValue(const Collection& collection, const Value& value) {
42 return std::find(std::begin(collection), std::end(collection), value) !=
43 std::end(collection);
44 }
45
46 // Means of generating a key for searching STL collections of std::unique_ptr
47 // that avoids the side effect of deleting the pointer.
48 template <class T>
49 class FakeUniquePtr : public std::unique_ptr<T> {
50 public:
51 using std::unique_ptr<T>::unique_ptr;
~FakeUniquePtr()52 ~FakeUniquePtr() { std::unique_ptr<T>::release(); }
53 };
54
55 // Convenience routine for "int-fected" code, so that the stl collection
56 // size_t size() method return values will be checked.
57 template <typename ResultType, typename Collection>
CollectionSize(const Collection & collection)58 ResultType CollectionSize(const Collection& collection) {
59 return pdfium::base::checked_cast<ResultType>(collection.size());
60 }
61
62 // Convenience routine for "int-fected" code, to handle signed indicies. The
63 // compiler can deduce the type, making this more convenient than the above.
64 template <typename IndexType, typename Collection>
IndexInBounds(const Collection & collection,IndexType index)65 bool IndexInBounds(const Collection& collection, IndexType index) {
66 return index >= 0 && index < CollectionSize<IndexType>(collection);
67 }
68
69 // Track the addition of an object to a set, removing it automatically when
70 // the ScopedSetInsertion goes out of scope.
71 template <typename T>
72 class ScopedSetInsertion {
73 public:
ScopedSetInsertion(std::set<T> * org_set,T elem)74 ScopedSetInsertion(std::set<T>* org_set, T elem)
75 : m_Set(org_set), m_Entry(elem) {
76 m_Set->insert(m_Entry);
77 }
~ScopedSetInsertion()78 ~ScopedSetInsertion() { m_Set->erase(m_Entry); }
79
80 private:
81 std::set<T>* const m_Set;
82 const T m_Entry;
83 };
84
85 // std::clamp(), some day.
86 template <class T>
clamp(const T & v,const T & lo,const T & hi)87 constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
88 return std::min(std::max(v, lo), hi);
89 }
90
91 // Safely allocate a 1-dim vector big enough for |w| by |h| or die.
92 template <typename T, typename A = std::allocator<T>>
Vector2D(size_t w,size_t h)93 std::vector<T, A> Vector2D(size_t w, size_t h) {
94 pdfium::base::CheckedNumeric<size_t> safe_size = w;
95 safe_size *= h;
96 return std::vector<T, A>(safe_size.ValueOrDie());
97 }
98
99 } // namespace pdfium
100
101 #endif // THIRD_PARTY_BASE_STL_UTIL_H_
102