1 // Copyright 2015 The PDFium Authors 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 CORE_FXCRT_STL_UTIL_H_ 6 #define CORE_FXCRT_STL_UTIL_H_ 7 8 #include <memory> 9 10 #include "third_party/base/numerics/safe_conversions.h" 11 12 namespace fxcrt { 13 14 // Means of generating a key for searching STL collections of std::unique_ptr 15 // that avoids the side effect of deleting the pointer. 16 template <class T> 17 class FakeUniquePtr : public std::unique_ptr<T> { 18 public: 19 using std::unique_ptr<T>::unique_ptr; ~FakeUniquePtr()20 ~FakeUniquePtr() { std::unique_ptr<T>::release(); } 21 }; 22 23 // Type-deducing wrapper for FakeUniquePtr<T>. 24 template <class T> MakeFakeUniquePtr(T * arg)25FakeUniquePtr<T> MakeFakeUniquePtr(T* arg) { 26 return FakeUniquePtr<T>(arg); 27 } 28 29 // Convenience routine for "int-fected" code, so that the stl collection 30 // size_t size() method return values will be checked. 31 template <typename ResultType, typename Collection> CollectionSize(const Collection & collection)32ResultType CollectionSize(const Collection& collection) { 33 return pdfium::base::checked_cast<ResultType>(collection.size()); 34 } 35 36 // Convenience routine for "int-fected" code, to handle signed indicies. The 37 // compiler can deduce the type, making this more convenient than the above. 38 template <typename IndexType, typename Collection> IndexInBounds(const Collection & collection,IndexType index)39bool IndexInBounds(const Collection& collection, IndexType index) { 40 return index >= 0 && index < CollectionSize<IndexType>(collection); 41 } 42 43 } // namespace fxcrt 44 45 #endif // CORE_FXCRT_STL_UTIL_H_ 46