Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_STL_UTIL_H_ |
| 4 | #define ART_SRC_STL_UTIL_H_ |
| 5 | |
Elliott Hughes | 14134a1 | 2011-09-30 16:55:51 -0700 | [diff] [blame] | 6 | #include <iostream> |
| 7 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 8 | namespace art { |
| 9 | |
| 10 | // STLDeleteContainerPointers() |
| 11 | // For a range within a container of pointers, calls delete |
| 12 | // (non-array version) on these pointers. |
| 13 | // NOTE: for these three functions, we could just implement a DeleteObject |
| 14 | // functor and then call for_each() on the range and functor, but this |
| 15 | // requires us to pull in all of algorithm.h, which seems expensive. |
| 16 | // For hash_[multi]set, it is important that this deletes behind the iterator |
| 17 | // because the hash_set may call the hash function on the iterator when it is |
| 18 | // advanced, which could result in the hash function trying to deference a |
| 19 | // stale pointer. |
| 20 | template <class ForwardIterator> |
| 21 | void STLDeleteContainerPointers(ForwardIterator begin, |
| 22 | ForwardIterator end) { |
| 23 | while (begin != end) { |
| 24 | ForwardIterator temp = begin; |
| 25 | ++begin; |
| 26 | delete *temp; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // STLDeleteElements() deletes all the elements in an STL container and clears |
| 31 | // the container. This function is suitable for use with a vector, set, |
| 32 | // hash_set, or any other STL container which defines sensible begin(), end(), |
| 33 | // and clear() methods. |
| 34 | // |
| 35 | // If container is NULL, this function is a no-op. |
| 36 | // |
| 37 | // As an alternative to calling STLDeleteElements() directly, consider |
| 38 | // ElementDeleter (defined below), which ensures that your container's elements |
| 39 | // are deleted when the ElementDeleter goes out of scope. |
| 40 | template <class T> |
| 41 | void STLDeleteElements(T *container) { |
| 42 | if (!container) return; |
| 43 | STLDeleteContainerPointers(container->begin(), container->end()); |
| 44 | container->clear(); |
| 45 | } |
| 46 | |
Elliott Hughes | c31664f | 2011-09-29 15:58:28 -0700 | [diff] [blame] | 47 | // Given an STL container consisting of (key, value) pairs, STLDeleteValues |
| 48 | // deletes all the "value" components and clears the container. Does nothing |
| 49 | // in the case it's given a NULL pointer. |
| 50 | template <class T> |
| 51 | void STLDeleteValues(T *v) { |
| 52 | if (!v) return; |
| 53 | for (typename T::iterator i = v->begin(); i != v->end(); ++i) { |
| 54 | delete i->second; |
| 55 | } |
| 56 | v->clear(); |
| 57 | } |
| 58 | |
Elliott Hughes | 14134a1 | 2011-09-30 16:55:51 -0700 | [diff] [blame] | 59 | template <class T> |
| 60 | std::string ToString(const T& v) { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 61 | std::ostringstream os; |
Elliott Hughes | 14134a1 | 2011-09-30 16:55:51 -0700 | [diff] [blame] | 62 | os << "["; |
| 63 | for (size_t i = 0; i < v.size(); ++i) { |
| 64 | os << v[i]; |
| 65 | if (i < v.size() - 1) { |
| 66 | os << ", "; |
| 67 | } |
| 68 | } |
| 69 | os << "]"; |
| 70 | return os.str(); |
| 71 | } |
| 72 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 73 | } // namespace art |
| 74 | |
| 75 | #endif // ART_SRC_STL_UTIL_H_ |