blob: 4418ab8f9f4cf126c6c42d6fe7cb6abe92b3b5b8 [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_
9#pragma once
10
[email protected]7286e3fc2011-07-19 22:13:2411#include <string>
12#include <vector>
13
[email protected]6ee951a2012-06-26 17:24:0514// Clears internal memory of an STL object.
[email protected]7286e3fc2011-07-19 22:13:2415// 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]6ee951a2012-06-26 17:24:0517template<class T>
18void STLClearObject(T* obj) {
[email protected]7286e3fc2011-07-19 22:13:2419 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]6ee951a2012-06-26 17:24:0526// For a range within a container of pointers, calls delete (non-array version)
27// on these pointers.
[email protected]7286e3fc2011-07-19 22:13:2428// 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.
35template <class ForwardIterator>
36void STLDeleteContainerPointers(ForwardIterator begin, ForwardIterator end) {
37 while (begin != end) {
38 ForwardIterator temp = begin;
39 ++begin;
40 delete *temp;
41 }
42}
43
[email protected]6ee951a2012-06-26 17:24:0544// For a range within a container of pairs, calls delete (non-array version) on
45// BOTH items in the pairs.
[email protected]7286e3fc2011-07-19 22:13:2446// 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.
51template <class ForwardIterator>
52void 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]6ee951a2012-06-26 17:24:0562// For a range within a container of pairs, calls delete (non-array version) on
63// the FIRST item in the pairs.
[email protected]7286e3fc2011-07-19 22:13:2464// NOTE: Like STLDeleteContainerPointers, deleting behind the iterator.
65template <class ForwardIterator>
66void 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]6ee951a2012-06-26 17:24:0575// For a range within a container of pairs, calls delete.
[email protected]8b7ddc72011-07-21 08:39:5176// 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]7286e3fc2011-07-19 22:13:2479template <class ForwardIterator>
80void STLDeleteContainerPairSecondPointers(ForwardIterator begin,
81 ForwardIterator end) {
82 while (begin != end) {
[email protected]8b7ddc72011-07-21 08:39:5183 ForwardIterator temp = begin;
[email protected]7286e3fc2011-07-19 22:13:2484 ++begin;
[email protected]8b7ddc72011-07-21 08:39:5185 delete temp->second;
[email protected]7286e3fc2011-07-19 22:13:2486 }
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]6ee951a2012-06-26 17:24:0591// directly, but that is undefined behaviour if |v| is empty.
[email protected]7286e3fc2011-07-19 22:13:2492template<typename T>
93inline T* vector_as_array(std::vector<T>* v) {
[email protected]7286e3fc2011-07-19 22:13:2494 return v->empty() ? NULL : &*v->begin();
[email protected]7286e3fc2011-07-19 22:13:2495}
96
97template<typename T>
98inline const T* vector_as_array(const std::vector<T>* v) {
[email protected]7286e3fc2011-07-19 22:13:2499 return v->empty() ? NULL : &*v->begin();
[email protected]7286e3fc2011-07-19 22:13:24100}
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.
114inline 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]6ee951a2012-06-26 17:24:05119// The following functions are useful for cleaning up STL containers whose
120// elements point to allocated memory.
[email protected]7286e3fc2011-07-19 22:13:24121
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.
132template <class T>
[email protected]6ee951a2012-06-26 17:24:05133void STLDeleteElements(T* container) {
134 if (!container)
135 return;
[email protected]7286e3fc2011-07-19 22:13:24136 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]7286e3fc2011-07-19 22:13:24143template <class T>
[email protected]6ee951a2012-06-26 17:24:05144void STLDeleteValues(T* container) {
145 if (!container)
146 return;
147 for (typename T::iterator i(container->begin()); i != container->end(); ++i)
[email protected]7286e3fc2011-07-19 22:13:24148 delete i->second;
[email protected]6ee951a2012-06-26 17:24:05149 container->clear();
[email protected]7286e3fc2011-07-19 22:13:24150}
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]6ee951a2012-06-26 17:24:05166template<class T>
167class STLElementDeleter {
[email protected]7286e3fc2011-07-19 22:13:24168 public:
[email protected]6ee951a2012-06-26 17:24:05169 STLElementDeleter<T>(T* container) : container_(container) {}
170 ~STLElementDeleter<T>() { STLDeleteElements(container_); }
171
[email protected]7286e3fc2011-07-19 22:13:24172 private:
[email protected]6ee951a2012-06-26 17:24:05173 T* container_;
[email protected]7286e3fc2011-07-19 22:13:24174};
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]6ee951a2012-06-26 17:24:05178template<class T>
179class STLValueDeleter {
[email protected]7286e3fc2011-07-19 22:13:24180 public:
[email protected]6ee951a2012-06-26 17:24:05181 STLValueDeleter<T>(T* container) : container_(container) {}
182 ~STLValueDeleter<T>() { STLDeleteValues(container_); }
183
[email protected]7286e3fc2011-07-19 22:13:24184 private:
[email protected]6ee951a2012-06-26 17:24:05185 T* container_;
[email protected]7286e3fc2011-07-19 22:13:24186};
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.
190template <typename Collection, typename Key>
191bool ContainsKey(const Collection& collection, const Key& key) {
192 return collection.find(key) != collection.end();
193}
194
195#endif // BASE_STL_UTIL_H_