blob: c0fe6b133003d84ee69d597893e11b0626d1fbac [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro58551df2011-07-24 03:09:51 -070016
17#ifndef ART_SRC_STL_UTIL_H_
18#define ART_SRC_STL_UTIL_H_
19
Elliott Hughes14134a12011-09-30 16:55:51 -070020#include <iostream>
21
Carl Shapiro58551df2011-07-24 03:09:51 -070022namespace art {
23
24// STLDeleteContainerPointers()
25// For a range within a container of pointers, calls delete
26// (non-array version) 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.
34template <class ForwardIterator>
35void STLDeleteContainerPointers(ForwardIterator begin,
36 ForwardIterator end) {
37 while (begin != end) {
38 ForwardIterator temp = begin;
39 ++begin;
40 delete *temp;
41 }
42}
43
44// STLDeleteElements() deletes all the elements in an STL container and clears
45// the container. This function is suitable for use with a vector, set,
46// hash_set, or any other STL container which defines sensible begin(), end(),
47// and clear() methods.
48//
49// If container is NULL, this function is a no-op.
50//
51// As an alternative to calling STLDeleteElements() directly, consider
52// ElementDeleter (defined below), which ensures that your container's elements
53// are deleted when the ElementDeleter goes out of scope.
54template <class T>
55void STLDeleteElements(T *container) {
56 if (!container) return;
57 STLDeleteContainerPointers(container->begin(), container->end());
58 container->clear();
59}
60
Elliott Hughesc31664f2011-09-29 15:58:28 -070061// Given an STL container consisting of (key, value) pairs, STLDeleteValues
62// deletes all the "value" components and clears the container. Does nothing
63// in the case it's given a NULL pointer.
64template <class T>
65void STLDeleteValues(T *v) {
66 if (!v) return;
67 for (typename T::iterator i = v->begin(); i != v->end(); ++i) {
68 delete i->second;
69 }
70 v->clear();
71}
72
Elliott Hughes14134a12011-09-30 16:55:51 -070073template <class T>
74std::string ToString(const T& v) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070075 std::ostringstream os;
Elliott Hughes14134a12011-09-30 16:55:51 -070076 os << "[";
77 for (size_t i = 0; i < v.size(); ++i) {
78 os << v[i];
79 if (i < v.size() - 1) {
80 os << ", ";
81 }
82 }
83 os << "]";
84 return os.str();
85}
86
Carl Shapiro58551df2011-07-24 03:09:51 -070087} // namespace art
88
89#endif // ART_SRC_STL_UTIL_H_