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