blob: b3ddfa3b4fe8f85bb9568768d1a2af9fd7c75d19 [file] [log] [blame]
[email protected]7286e3fc2011-07-19 22:13:241// 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]7286e3fc2011-07-19 22:13:249
[email protected]7286e3fc2011-07-19 22:13:2410#include <string>
11#include <vector>
12
[email protected]6ee951a2012-06-26 17:24:0513// Clears internal memory of an STL object.
[email protected]7286e3fc2011-07-19 22:13:2414// 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]6ee951a2012-06-26 17:24:0516template<class T>
17void STLClearObject(T* obj) {
[email protected]7286e3fc2011-07-19 22:13:2418 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]6ee951a2012-06-26 17:24:0525// For a range within a container of pointers, calls delete (non-array version)
26// on these pointers.
[email protected]7286e3fc2011-07-19 22:13:2427// 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.
34template <class ForwardIterator>
35void STLDeleteContainerPointers(ForwardIterator begin, ForwardIterator end) {
36 while (begin != end) {
37 ForwardIterator temp = begin;
38 ++begin;
39 delete *temp;
40 }
41}
42
[email protected]6ee951a2012-06-26 17:24:0543// For a range within a container of pairs, calls delete (non-array version) on
44// BOTH items in the pairs.
[email protected]7286e3fc2011-07-19 22:13:2445// 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.
50template <class ForwardIterator>
51void 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]6ee951a2012-06-26 17:24:0561// For a range within a container of pairs, calls delete (non-array version) on
62// the FIRST item in the pairs.
[email protected]7286e3fc2011-07-19 22:13:2463// NOTE: Like STLDeleteContainerPointers, deleting behind the iterator.
64template <class ForwardIterator>
65void 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]6ee951a2012-06-26 17:24:0574// For a range within a container of pairs, calls delete.
[email protected]8b7ddc72011-07-21 08:39:5175// 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]7286e3fc2011-07-19 22:13:2478template <class ForwardIterator>
79void STLDeleteContainerPairSecondPointers(ForwardIterator begin,
80 ForwardIterator end) {
81 while (begin != end) {
[email protected]8b7ddc72011-07-21 08:39:5182 ForwardIterator temp = begin;
[email protected]7286e3fc2011-07-19 22:13:2483 ++begin;
[email protected]8b7ddc72011-07-21 08:39:5184 delete temp->second;
[email protected]7286e3fc2011-07-19 22:13:2485 }
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]6ee951a2012-06-26 17:24:0590// directly, but that is undefined behaviour if |v| is empty.
[email protected]7286e3fc2011-07-19 22:13:2491template<typename T>
92inline T* vector_as_array(std::vector<T>* v) {
[email protected]7286e3fc2011-07-19 22:13:2493 return v->empty() ? NULL : &*v->begin();
[email protected]7286e3fc2011-07-19 22:13:2494}
95
96template<typename T>
97inline const T* vector_as_array(const std::vector<T>* v) {
[email protected]7286e3fc2011-07-19 22:13:2498 return v->empty() ? NULL : &*v->begin();
[email protected]7286e3fc2011-07-19 22:13:2499}
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.
113inline 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]6ee951a2012-06-26 17:24:05118// The following functions are useful for cleaning up STL containers whose
119// elements point to allocated memory.
[email protected]7286e3fc2011-07-19 22:13:24120
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.
131template <class T>
[email protected]6ee951a2012-06-26 17:24:05132void STLDeleteElements(T* container) {
133 if (!container)
134 return;
[email protected]7286e3fc2011-07-19 22:13:24135 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]7286e3fc2011-07-19 22:13:24142template <class T>
[email protected]6ee951a2012-06-26 17:24:05143void STLDeleteValues(T* container) {
144 if (!container)
145 return;
146 for (typename T::iterator i(container->begin()); i != container->end(); ++i)
[email protected]7286e3fc2011-07-19 22:13:24147 delete i->second;
[email protected]6ee951a2012-06-26 17:24:05148 container->clear();
[email protected]7286e3fc2011-07-19 22:13:24149}
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]6ee951a2012-06-26 17:24:05165template<class T>
166class STLElementDeleter {
[email protected]7286e3fc2011-07-19 22:13:24167 public:
[email protected]6ee951a2012-06-26 17:24:05168 STLElementDeleter<T>(T* container) : container_(container) {}
169 ~STLElementDeleter<T>() { STLDeleteElements(container_); }
170
[email protected]7286e3fc2011-07-19 22:13:24171 private:
[email protected]6ee951a2012-06-26 17:24:05172 T* container_;
[email protected]7286e3fc2011-07-19 22:13:24173};
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]6ee951a2012-06-26 17:24:05177template<class T>
178class STLValueDeleter {
[email protected]7286e3fc2011-07-19 22:13:24179 public:
[email protected]6ee951a2012-06-26 17:24:05180 STLValueDeleter<T>(T* container) : container_(container) {}
181 ~STLValueDeleter<T>() { STLDeleteValues(container_); }
182
[email protected]7286e3fc2011-07-19 22:13:24183 private:
[email protected]6ee951a2012-06-26 17:24:05184 T* container_;
[email protected]7286e3fc2011-07-19 22:13:24185};
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.
189template <typename Collection, typename Key>
190bool ContainsKey(const Collection& collection, const Key& key) {
191 return collection.find(key) != collection.end();
192}
193
194#endif // BASE_STL_UTIL_H_