cros: Implement default app ordering.
[chromium-blink-merge.git] / base / stl_util.h
blobb3ddfa3b4fe8f85bb9568768d1a2af9fd7c75d19
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.
5 // Derived from google3/util/gtl/stl_util.h
7 #ifndef BASE_STL_UTIL_H_
8 #define BASE_STL_UTIL_H_
10 #include <string>
11 #include <vector>
13 // Clears internal memory of an STL object.
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.
16 template<class T>
17 void STLClearObject(T* obj) {
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);
25 // For a range within a container of pointers, calls delete (non-array version)
26 // on these pointers.
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;
43 // For a range within a container of pairs, calls delete (non-array version) on
44 // BOTH items in the pairs.
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;
61 // For a range within a container of pairs, calls delete (non-array version) on
62 // the FIRST item in the pairs.
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;
74 // For a range within a container of pairs, calls delete.
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.
78 template <class ForwardIterator>
79 void STLDeleteContainerPairSecondPointers(ForwardIterator begin,
80 ForwardIterator end) {
81 while (begin != end) {
82 ForwardIterator temp = begin;
83 ++begin;
84 delete temp->second;
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()
90 // directly, but that is undefined behaviour if |v| is empty.
91 template<typename T>
92 inline T* vector_as_array(std::vector<T>* v) {
93 return v->empty() ? NULL : &*v->begin();
96 template<typename T>
97 inline const T* vector_as_array(const std::vector<T>* v) {
98 return v->empty() ? NULL : &*v->begin();
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.
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.
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();
118 // The following functions are useful for cleaning up STL containers whose
119 // elements point to allocated memory.
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.
126 // If container is NULL, this function is a no-op.
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>
132 void STLDeleteElements(T* container) {
133 if (!container)
134 return;
135 STLDeleteContainerPointers(container->begin(), container->end());
136 container->clear();
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.
142 template <class T>
143 void STLDeleteValues(T* container) {
144 if (!container)
145 return;
146 for (typename T::iterator i(container->begin()); i != container->end(); ++i)
147 delete i->second;
148 container->clear();
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:
157 // vector<MyProto *> tmp_proto;
158 // STLElementDeleter<vector<MyProto *> > d(&tmp_proto);
159 // if (...) return false;
160 // ...
161 // return success;
163 // Given a pointer to an STL container this class will delete all the element
164 // pointers when it goes out of scope.
165 template<class T>
166 class STLElementDeleter {
167 public:
168 STLElementDeleter<T>(T* container) : container_(container) {}
169 ~STLElementDeleter<T>() { STLDeleteElements(container_); }
171 private:
172 T* container_;
175 // Given a pointer to an STL container this class will delete all the value
176 // pointers when it goes out of scope.
177 template<class T>
178 class STLValueDeleter {
179 public:
180 STLValueDeleter<T>(T* container) : container_(container) {}
181 ~STLValueDeleter<T>() { STLDeleteValues(container_); }
183 private:
184 T* container_;
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();
194 #endif // BASE_STL_UTIL_H_