OLD | NEW |
---|---|
(Empty) | |
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 | |
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 | |
11 #include <assert.h> | |
12 #include <string.h> // for memcpy | |
joth
2011/07/20 11:38:48
we don't actually call memcpy anymore.
Denis Lagno
2011/07/20 12:01:13
thanks for noticing.
And BTW we do not call asser
| |
13 | |
14 #include <string> | |
15 #include <vector> | |
16 | |
17 // Clear internal memory of an STL object. | |
18 // STL clear()/reserve(0) does not always free internal memory allocated | |
19 // This function uses swap/destructor to ensure the internal memory is freed. | |
20 template<class T> void STLClearObject(T* obj) { | |
21 T tmp; | |
22 tmp.swap(*obj); | |
23 // Sometimes "T tmp" allocates objects with memory (arena implementation?). | |
24 // Hence using additional reserve(0) even if it doesn't always work. | |
25 obj->reserve(0); | |
26 } | |
27 | |
28 // STLDeleteContainerPointers() | |
29 // For a range within a container of pointers, calls delete | |
30 // (non-array version) on these pointers. | |
31 // NOTE: for these three functions, we could just implement a DeleteObject | |
32 // functor and then call for_each() on the range and functor, but this | |
33 // requires us to pull in all of algorithm.h, which seems expensive. | |
34 // For hash_[multi]set, it is important that this deletes behind the iterator | |
35 // because the hash_set may call the hash function on the iterator when it is | |
36 // advanced, which could result in the hash function trying to deference a | |
37 // stale pointer. | |
38 template <class ForwardIterator> | |
39 void STLDeleteContainerPointers(ForwardIterator begin, ForwardIterator end) { | |
40 while (begin != end) { | |
41 ForwardIterator temp = begin; | |
42 ++begin; | |
43 delete *temp; | |
44 } | |
45 } | |
46 | |
47 // STLDeleteContainerPairPointers() | |
48 // For a range within a container of pairs, calls delete | |
49 // (non-array version) on BOTH items in the pairs. | |
50 // NOTE: Like STLDeleteContainerPointers, it is important that this deletes | |
51 // behind the iterator because if both the key and value are deleted, the | |
52 // container may call the hash function on the iterator when it is advanced, | |
53 // which could result in the hash function trying to dereference a stale | |
54 // pointer. | |
55 template <class ForwardIterator> | |
56 void STLDeleteContainerPairPointers(ForwardIterator begin, | |
57 ForwardIterator end) { | |
58 while (begin != end) { | |
59 ForwardIterator temp = begin; | |
60 ++begin; | |
61 delete temp->first; | |
62 delete temp->second; | |
63 } | |
64 } | |
65 | |
66 // STLDeleteContainerPairFirstPointers() | |
67 // For a range within a container of pairs, calls delete (non-array version) | |
68 // on the FIRST item in the pairs. | |
69 // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator. | |
70 template <class ForwardIterator> | |
71 void STLDeleteContainerPairFirstPointers(ForwardIterator begin, | |
72 ForwardIterator end) { | |
73 while (begin != end) { | |
74 ForwardIterator temp = begin; | |
75 ++begin; | |
76 delete temp->first; | |
77 } | |
78 } | |
79 | |
80 // STLDeleteContainerPairSecondPointers() | |
81 // For a range within a container of pairs, calls delete | |
82 // (non-array version) on the SECOND item in the pairs. | |
83 template <class ForwardIterator> | |
84 void STLDeleteContainerPairSecondPointers(ForwardIterator begin, | |
85 ForwardIterator end) { | |
86 while (begin != end) { | |
87 delete begin->second; | |
88 ++begin; | |
89 } | |
90 } | |
joth
2011/07/20 11:38:48
fwiw server now has this as
while (begin != end)
Denis Lagno
2011/07/20 12:01:13
yes, it looks better to sync with server.
| |
91 | |
92 // To treat a possibly-empty vector as an array, use these functions. | |
93 // If you know the array will never be empty, you can use &*v.begin() | |
94 // directly, but that is undefined behaviour if v is empty. | |
95 | |
96 template<typename T> | |
97 inline T* vector_as_array(std::vector<T>* v) { | |
98 # ifdef NDEBUG | |
99 return &*v->begin(); | |
joth
2011/07/20 11:38:48
this always seems to me to be a quite obscure non-
Denis Lagno
2011/07/20 12:54:36
agree, chromium (client) and server differs in tha
| |
100 # else | |
101 return v->empty() ? NULL : &*v->begin(); | |
102 # endif | |
103 } | |
104 | |
105 template<typename T> | |
106 inline const T* vector_as_array(const std::vector<T>* v) { | |
107 # ifdef NDEBUG | |
108 return &*v->begin(); | |
109 # else | |
110 return v->empty() ? NULL : &*v->begin(); | |
111 # endif | |
112 } | |
113 | |
114 // Return a mutable char* pointing to a string's internal buffer, | |
115 // which may not be null-terminated. Writing through this pointer will | |
116 // modify the string. | |
117 // | |
118 // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the | |
119 // next call to a string method that invalidates iterators. | |
120 // | |
121 // As of 2006-04, there is no standard-blessed way of getting a | |
122 // mutable reference to a string's internal buffer. However, issue 530 | |
123 // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530) | |
124 // proposes this as the method. According to Matt Austern, this should | |
125 // already work on all current implementations. | |
joth
2011/07/20 11:38:48
FWIW, C++ 2008 does now clarify this:
21.3.1 basi
| |
126 inline char* string_as_array(std::string* str) { | |
127 // DO NOT USE const_cast<char*>(str->data()) | |
128 return str->empty() ? NULL : &*str->begin(); | |
129 } | |
130 | |
131 // The following functions are useful for cleaning up STL containers | |
132 // whose elements point to allocated memory. | |
133 | |
134 // STLDeleteElements() deletes all the elements in an STL container and clears | |
135 // the container. This function is suitable for use with a vector, set, | |
136 // hash_set, or any other STL container which defines sensible begin(), end(), | |
137 // and clear() methods. | |
138 // | |
139 // If container is NULL, this function is a no-op. | |
140 // | |
141 // As an alternative to calling STLDeleteElements() directly, consider | |
142 // STLElementDeleter (defined below), which ensures that your container's | |
143 // elements are deleted when the STLElementDeleter goes out of scope. | |
144 template <class T> | |
145 void STLDeleteElements(T *container) { | |
146 if (!container) return; | |
147 STLDeleteContainerPointers(container->begin(), container->end()); | |
148 container->clear(); | |
149 } | |
150 | |
151 // Given an STL container consisting of (key, value) pairs, STLDeleteValues | |
152 // deletes all the "value" components and clears the container. Does nothing | |
153 // in the case it's given a NULL pointer. | |
154 | |
155 template <class T> | |
156 void STLDeleteValues(T *v) { | |
157 if (!v) return; | |
158 for (typename T::iterator i = v->begin(); i != v->end(); ++i) { | |
159 delete i->second; | |
160 } | |
161 v->clear(); | |
162 } | |
163 | |
164 | |
165 // The following classes provide a convenient way to delete all elements or | |
166 // values from STL containers when they goes out of scope. This greatly | |
167 // simplifies code that creates temporary objects and has multiple return | |
168 // statements. Example: | |
169 // | |
170 // vector<MyProto *> tmp_proto; | |
171 // STLElementDeleter<vector<MyProto *> > d(&tmp_proto); | |
172 // if (...) return false; | |
173 // ... | |
174 // return success; | |
175 | |
176 // Given a pointer to an STL container this class will delete all the element | |
177 // pointers when it goes out of scope. | |
178 | |
179 template<class STLContainer> class STLElementDeleter { | |
180 public: | |
181 STLElementDeleter<STLContainer>(STLContainer *ptr) : container_ptr_(ptr) {} | |
182 ~STLElementDeleter<STLContainer>() { STLDeleteElements(container_ptr_); } | |
183 private: | |
184 STLContainer *container_ptr_; | |
185 }; | |
186 | |
187 // Given a pointer to an STL container this class will delete all the value | |
188 // pointers when it goes out of scope. | |
189 | |
190 template<class STLContainer> class STLValueDeleter { | |
191 public: | |
192 STLValueDeleter<STLContainer>(STLContainer *ptr) : container_ptr_(ptr) {} | |
193 ~STLValueDeleter<STLContainer>() { STLDeleteValues(container_ptr_); } | |
194 private: | |
195 STLContainer *container_ptr_; | |
196 }; | |
197 | |
198 // Test to see if a set, map, hash_set or hash_map contains a particular key. | |
199 // Returns true if the key is in the collection. | |
200 template <typename Collection, typename Key> | |
201 bool ContainsKey(const Collection& collection, const Key& key) { | |
202 return collection.find(key) != collection.end(); | |
203 } | |
204 | |
205 #endif // BASE_STL_UTIL_H_ | |
OLD | NEW |