| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Derived from google3/util/gtl/stl_util.h | 5 // Derived from google3/util/gtl/stl_util.h |
| 6 | 6 |
| 7 #ifndef BASE_STL_UTIL_H_ | 7 #ifndef BASE_STL_UTIL_H_ |
| 8 #define BASE_STL_UTIL_H_ | 8 #define BASE_STL_UTIL_H_ |
| 9 #pragma once | 9 #pragma once |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 // Return the address of an object as a unary functor. |
| 15 // Can be used with std::transform to make a container of pointers from a |
| 16 // container of objects. |
| 17 // NOTE: Does not work around the possibility that operator & is overloaded. |
| 18 template<typename T> |
| 19 T* address_of(T& v) { |
| 20 return &v; |
| 21 } |
| 22 template<typename T> |
| 23 const T* const_address_of(const T& v) { |
| 24 return &v; |
| 25 } |
| 26 |
| 14 // Clear internal memory of an STL object. | 27 // Clear internal memory of an STL object. |
| 15 // STL clear()/reserve(0) does not always free internal memory allocated | 28 // STL clear()/reserve(0) does not always free internal memory allocated |
| 16 // This function uses swap/destructor to ensure the internal memory is freed. | 29 // This function uses swap/destructor to ensure the internal memory is freed. |
| 17 template<class T> void STLClearObject(T* obj) { | 30 template<class T> void STLClearObject(T* obj) { |
| 18 T tmp; | 31 T tmp; |
| 19 tmp.swap(*obj); | 32 tmp.swap(*obj); |
| 20 // Sometimes "T tmp" allocates objects with memory (arena implementation?). | 33 // Sometimes "T tmp" allocates objects with memory (arena implementation?). |
| 21 // Hence using additional reserve(0) even if it doesn't always work. | 34 // Hence using additional reserve(0) even if it doesn't always work. |
| 22 obj->reserve(0); | 35 obj->reserve(0); |
| 23 } | 36 } |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 }; | 202 }; |
| 190 | 203 |
| 191 // Test to see if a set, map, hash_set or hash_map contains a particular key. | 204 // Test to see if a set, map, hash_set or hash_map contains a particular key. |
| 192 // Returns true if the key is in the collection. | 205 // Returns true if the key is in the collection. |
| 193 template <typename Collection, typename Key> | 206 template <typename Collection, typename Key> |
| 194 bool ContainsKey(const Collection& collection, const Key& key) { | 207 bool ContainsKey(const Collection& collection, const Key& key) { |
| 195 return collection.find(key) != collection.end(); | 208 return collection.find(key) != collection.end(); |
| 196 } | 209 } |
| 197 | 210 |
| 198 #endif // BASE_STL_UTIL_H_ | 211 #endif // BASE_STL_UTIL_H_ |
| OLD | NEW |