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