security/sandbox/chromium/base/stl_util.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 // Derived from google3/util/gtl/stl_util.h
michael@0 6
michael@0 7 #ifndef BASE_STL_UTIL_H_
michael@0 8 #define BASE_STL_UTIL_H_
michael@0 9
michael@0 10 #include <algorithm>
michael@0 11 #include <functional>
michael@0 12 #include <iterator>
michael@0 13 #include <string>
michael@0 14 #include <vector>
michael@0 15
michael@0 16 #include "base/logging.h"
michael@0 17
michael@0 18 // Clears internal memory of an STL object.
michael@0 19 // STL clear()/reserve(0) does not always free internal memory allocated
michael@0 20 // This function uses swap/destructor to ensure the internal memory is freed.
michael@0 21 template<class T>
michael@0 22 void STLClearObject(T* obj) {
michael@0 23 T tmp;
michael@0 24 tmp.swap(*obj);
michael@0 25 // Sometimes "T tmp" allocates objects with memory (arena implementation?).
michael@0 26 // Hence using additional reserve(0) even if it doesn't always work.
michael@0 27 obj->reserve(0);
michael@0 28 }
michael@0 29
michael@0 30 // For a range within a container of pointers, calls delete (non-array version)
michael@0 31 // on these pointers.
michael@0 32 // NOTE: for these three functions, we could just implement a DeleteObject
michael@0 33 // functor and then call for_each() on the range and functor, but this
michael@0 34 // requires us to pull in all of algorithm.h, which seems expensive.
michael@0 35 // For hash_[multi]set, it is important that this deletes behind the iterator
michael@0 36 // because the hash_set may call the hash function on the iterator when it is
michael@0 37 // advanced, which could result in the hash function trying to deference a
michael@0 38 // stale pointer.
michael@0 39 template <class ForwardIterator>
michael@0 40 void STLDeleteContainerPointers(ForwardIterator begin, ForwardIterator end) {
michael@0 41 while (begin != end) {
michael@0 42 ForwardIterator temp = begin;
michael@0 43 ++begin;
michael@0 44 delete *temp;
michael@0 45 }
michael@0 46 }
michael@0 47
michael@0 48 // For a range within a container of pairs, calls delete (non-array version) on
michael@0 49 // BOTH items in the pairs.
michael@0 50 // NOTE: Like STLDeleteContainerPointers, it is important that this deletes
michael@0 51 // behind the iterator because if both the key and value are deleted, the
michael@0 52 // container may call the hash function on the iterator when it is advanced,
michael@0 53 // which could result in the hash function trying to dereference a stale
michael@0 54 // pointer.
michael@0 55 template <class ForwardIterator>
michael@0 56 void STLDeleteContainerPairPointers(ForwardIterator begin,
michael@0 57 ForwardIterator end) {
michael@0 58 while (begin != end) {
michael@0 59 ForwardIterator temp = begin;
michael@0 60 ++begin;
michael@0 61 delete temp->first;
michael@0 62 delete temp->second;
michael@0 63 }
michael@0 64 }
michael@0 65
michael@0 66 // For a range within a container of pairs, calls delete (non-array version) on
michael@0 67 // the FIRST item in the pairs.
michael@0 68 // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator.
michael@0 69 template <class ForwardIterator>
michael@0 70 void STLDeleteContainerPairFirstPointers(ForwardIterator begin,
michael@0 71 ForwardIterator end) {
michael@0 72 while (begin != end) {
michael@0 73 ForwardIterator temp = begin;
michael@0 74 ++begin;
michael@0 75 delete temp->first;
michael@0 76 }
michael@0 77 }
michael@0 78
michael@0 79 // For a range within a container of pairs, calls delete.
michael@0 80 // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator.
michael@0 81 // Deleting the value does not always invalidate the iterator, but it may
michael@0 82 // do so if the key is a pointer into the value object.
michael@0 83 template <class ForwardIterator>
michael@0 84 void STLDeleteContainerPairSecondPointers(ForwardIterator begin,
michael@0 85 ForwardIterator end) {
michael@0 86 while (begin != end) {
michael@0 87 ForwardIterator temp = begin;
michael@0 88 ++begin;
michael@0 89 delete temp->second;
michael@0 90 }
michael@0 91 }
michael@0 92
michael@0 93 // To treat a possibly-empty vector as an array, use these functions.
michael@0 94 // If you know the array will never be empty, you can use &*v.begin()
michael@0 95 // directly, but that is undefined behaviour if |v| is empty.
michael@0 96 template<typename T>
michael@0 97 inline T* vector_as_array(std::vector<T>* v) {
michael@0 98 return v->empty() ? NULL : &*v->begin();
michael@0 99 }
michael@0 100
michael@0 101 template<typename T>
michael@0 102 inline const T* vector_as_array(const std::vector<T>* v) {
michael@0 103 return v->empty() ? NULL : &*v->begin();
michael@0 104 }
michael@0 105
michael@0 106 // Return a mutable char* pointing to a string's internal buffer,
michael@0 107 // which may not be null-terminated. Writing through this pointer will
michael@0 108 // modify the string.
michael@0 109 //
michael@0 110 // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
michael@0 111 // next call to a string method that invalidates iterators.
michael@0 112 //
michael@0 113 // As of 2006-04, there is no standard-blessed way of getting a
michael@0 114 // mutable reference to a string's internal buffer. However, issue 530
michael@0 115 // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530)
michael@0 116 // proposes this as the method. According to Matt Austern, this should
michael@0 117 // already work on all current implementations.
michael@0 118 inline char* string_as_array(std::string* str) {
michael@0 119 // DO NOT USE const_cast<char*>(str->data())
michael@0 120 return str->empty() ? NULL : &*str->begin();
michael@0 121 }
michael@0 122
michael@0 123 // The following functions are useful for cleaning up STL containers whose
michael@0 124 // elements point to allocated memory.
michael@0 125
michael@0 126 // STLDeleteElements() deletes all the elements in an STL container and clears
michael@0 127 // the container. This function is suitable for use with a vector, set,
michael@0 128 // hash_set, or any other STL container which defines sensible begin(), end(),
michael@0 129 // and clear() methods.
michael@0 130 //
michael@0 131 // If container is NULL, this function is a no-op.
michael@0 132 //
michael@0 133 // As an alternative to calling STLDeleteElements() directly, consider
michael@0 134 // STLElementDeleter (defined below), which ensures that your container's
michael@0 135 // elements are deleted when the STLElementDeleter goes out of scope.
michael@0 136 template <class T>
michael@0 137 void STLDeleteElements(T* container) {
michael@0 138 if (!container)
michael@0 139 return;
michael@0 140 STLDeleteContainerPointers(container->begin(), container->end());
michael@0 141 container->clear();
michael@0 142 }
michael@0 143
michael@0 144 // Given an STL container consisting of (key, value) pairs, STLDeleteValues
michael@0 145 // deletes all the "value" components and clears the container. Does nothing
michael@0 146 // in the case it's given a NULL pointer.
michael@0 147 template <class T>
michael@0 148 void STLDeleteValues(T* container) {
michael@0 149 if (!container)
michael@0 150 return;
michael@0 151 for (typename T::iterator i(container->begin()); i != container->end(); ++i)
michael@0 152 delete i->second;
michael@0 153 container->clear();
michael@0 154 }
michael@0 155
michael@0 156
michael@0 157 // The following classes provide a convenient way to delete all elements or
michael@0 158 // values from STL containers when they goes out of scope. This greatly
michael@0 159 // simplifies code that creates temporary objects and has multiple return
michael@0 160 // statements. Example:
michael@0 161 //
michael@0 162 // vector<MyProto *> tmp_proto;
michael@0 163 // STLElementDeleter<vector<MyProto *> > d(&tmp_proto);
michael@0 164 // if (...) return false;
michael@0 165 // ...
michael@0 166 // return success;
michael@0 167
michael@0 168 // Given a pointer to an STL container this class will delete all the element
michael@0 169 // pointers when it goes out of scope.
michael@0 170 template<class T>
michael@0 171 class STLElementDeleter {
michael@0 172 public:
michael@0 173 STLElementDeleter<T>(T* container) : container_(container) {}
michael@0 174 ~STLElementDeleter<T>() { STLDeleteElements(container_); }
michael@0 175
michael@0 176 private:
michael@0 177 T* container_;
michael@0 178 };
michael@0 179
michael@0 180 // Given a pointer to an STL container this class will delete all the value
michael@0 181 // pointers when it goes out of scope.
michael@0 182 template<class T>
michael@0 183 class STLValueDeleter {
michael@0 184 public:
michael@0 185 STLValueDeleter<T>(T* container) : container_(container) {}
michael@0 186 ~STLValueDeleter<T>() { STLDeleteValues(container_); }
michael@0 187
michael@0 188 private:
michael@0 189 T* container_;
michael@0 190 };
michael@0 191
michael@0 192 // Test to see if a set, map, hash_set or hash_map contains a particular key.
michael@0 193 // Returns true if the key is in the collection.
michael@0 194 template <typename Collection, typename Key>
michael@0 195 bool ContainsKey(const Collection& collection, const Key& key) {
michael@0 196 return collection.find(key) != collection.end();
michael@0 197 }
michael@0 198
michael@0 199 namespace base {
michael@0 200
michael@0 201 // Returns true if the container is sorted.
michael@0 202 template <typename Container>
michael@0 203 bool STLIsSorted(const Container& cont) {
michael@0 204 return std::adjacent_find(cont.begin(), cont.end(),
michael@0 205 std::greater<typename Container::value_type>())
michael@0 206 == cont.end();
michael@0 207 }
michael@0 208
michael@0 209 // Returns a new ResultType containing the difference of two sorted containers.
michael@0 210 template <typename ResultType, typename Arg1, typename Arg2>
michael@0 211 ResultType STLSetDifference(const Arg1& a1, const Arg2& a2) {
michael@0 212 DCHECK(STLIsSorted(a1));
michael@0 213 DCHECK(STLIsSorted(a2));
michael@0 214 ResultType difference;
michael@0 215 std::set_difference(a1.begin(), a1.end(),
michael@0 216 a2.begin(), a2.end(),
michael@0 217 std::inserter(difference, difference.end()));
michael@0 218 return difference;
michael@0 219 }
michael@0 220
michael@0 221 } // namespace base
michael@0 222
michael@0 223 #endif // BASE_STL_UTIL_H_

mercurial