OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_STACK_CONTAINER_H_ | 5 #ifndef BASE_CONTAINERS_STACK_CONTAINER_H_ |
6 #define BASE_STACK_CONTAINER_H_ | 6 #define BASE_CONTAINERS_STACK_CONTAINER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "base/memory/aligned_memory.h" | 13 #include "base/memory/aligned_memory.h" |
| 14 #include "base/string16.h" |
| 15 |
| 16 namespace base { |
14 | 17 |
15 // This allocator can be used with STL containers to provide a stack buffer | 18 // This allocator can be used with STL containers to provide a stack buffer |
16 // from which to allocate memory and overflows onto the heap. This stack buffer | 19 // from which to allocate memory and overflows onto the heap. This stack buffer |
17 // would be allocated on the stack and allows us to avoid heap operations in | 20 // would be allocated on the stack and allows us to avoid heap operations in |
18 // some situations. | 21 // some situations. |
19 // | 22 // |
20 // STL likes to make copies of allocators, so the allocator itself can't hold | 23 // STL likes to make copies of allocators, so the allocator itself can't hold |
21 // the data. Instead, we make the creator responsible for creating a | 24 // the data. Instead, we make the creator responsible for creating a |
22 // StackAllocator::Source which contains the data. Copying the allocator | 25 // StackAllocator::Source which contains the data. Copying the allocator |
23 // merely copies the pointer to this shared source, so all allocators created | 26 // merely copies the pointer to this shared source, so all allocators created |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 #endif | 165 #endif |
163 | 166 |
164 protected: | 167 protected: |
165 typename Allocator::Source stack_data_; | 168 typename Allocator::Source stack_data_; |
166 Allocator allocator_; | 169 Allocator allocator_; |
167 ContainerType container_; | 170 ContainerType container_; |
168 | 171 |
169 DISALLOW_COPY_AND_ASSIGN(StackContainer); | 172 DISALLOW_COPY_AND_ASSIGN(StackContainer); |
170 }; | 173 }; |
171 | 174 |
172 // StackString | 175 // StackString ----------------------------------------------------------------- |
| 176 |
173 template<size_t stack_capacity> | 177 template<size_t stack_capacity> |
174 class StackString : public StackContainer< | 178 class StackString : public StackContainer< |
175 std::basic_string<char, | 179 std::basic_string<char, |
176 std::char_traits<char>, | 180 std::char_traits<char>, |
177 StackAllocator<char, stack_capacity> >, | 181 StackAllocator<char, stack_capacity> >, |
178 stack_capacity> { | 182 stack_capacity> { |
179 public: | 183 public: |
180 StackString() : StackContainer< | 184 StackString() : StackContainer< |
181 std::basic_string<char, | 185 std::basic_string<char, |
182 std::char_traits<char>, | 186 std::char_traits<char>, |
183 StackAllocator<char, stack_capacity> >, | 187 StackAllocator<char, stack_capacity> >, |
184 stack_capacity>() { | 188 stack_capacity>() { |
185 } | 189 } |
186 | 190 |
187 private: | 191 private: |
188 DISALLOW_COPY_AND_ASSIGN(StackString); | 192 DISALLOW_COPY_AND_ASSIGN(StackString); |
189 }; | 193 }; |
190 | 194 |
191 // StackWString | 195 // StackStrin16 ---------------------------------------------------------------- |
| 196 |
192 template<size_t stack_capacity> | 197 template<size_t stack_capacity> |
193 class StackWString : public StackContainer< | 198 class StackString16 : public StackContainer< |
194 std::basic_string<wchar_t, | 199 std::basic_string<char16, |
195 std::char_traits<wchar_t>, | 200 base::string16_char_traits, |
196 StackAllocator<wchar_t, stack_capacity> >, | 201 StackAllocator<char16, stack_capacity> >, |
197 stack_capacity> { | 202 stack_capacity> { |
198 public: | 203 public: |
199 StackWString() : StackContainer< | 204 StackString16() : StackContainer< |
200 std::basic_string<wchar_t, | 205 std::basic_string<char16, |
201 std::char_traits<wchar_t>, | 206 base::string16_char_traits, |
202 StackAllocator<wchar_t, stack_capacity> >, | 207 StackAllocator<char16, stack_capacity> >, |
203 stack_capacity>() { | 208 stack_capacity>() { |
204 } | 209 } |
205 | 210 |
206 private: | 211 private: |
207 DISALLOW_COPY_AND_ASSIGN(StackWString); | 212 DISALLOW_COPY_AND_ASSIGN(StackString16); |
208 }; | 213 }; |
209 | 214 |
210 // StackVector | 215 // StackVector ----------------------------------------------------------------- |
211 // | 216 |
212 // Example: | 217 // Example: |
213 // StackVector<int, 16> foo; | 218 // StackVector<int, 16> foo; |
214 // foo->push_back(22); // we have overloaded operator-> | 219 // foo->push_back(22); // we have overloaded operator-> |
215 // foo[0] = 10; // as well as operator[] | 220 // foo[0] = 10; // as well as operator[] |
216 template<typename T, size_t stack_capacity> | 221 template<typename T, size_t stack_capacity> |
217 class StackVector : public StackContainer< | 222 class StackVector : public StackContainer< |
218 std::vector<T, StackAllocator<T, stack_capacity> >, | 223 std::vector<T, StackAllocator<T, stack_capacity> >, |
219 stack_capacity> { | 224 stack_capacity> { |
220 public: | 225 public: |
221 StackVector() : StackContainer< | 226 StackVector() : StackContainer< |
(...skipping 19 matching lines...) Expand all Loading... |
241 } | 246 } |
242 | 247 |
243 // Vectors are commonly indexed, which isn't very convenient even with | 248 // Vectors are commonly indexed, which isn't very convenient even with |
244 // operator-> (using "->at()" does exception stuff we don't want). | 249 // operator-> (using "->at()" does exception stuff we don't want). |
245 T& operator[](size_t i) { return this->container().operator[](i); } | 250 T& operator[](size_t i) { return this->container().operator[](i); } |
246 const T& operator[](size_t i) const { | 251 const T& operator[](size_t i) const { |
247 return this->container().operator[](i); | 252 return this->container().operator[](i); |
248 } | 253 } |
249 }; | 254 }; |
250 | 255 |
251 #endif // BASE_STACK_CONTAINER_H_ | 256 } // namespace base |
| 257 |
| 258 #endif // BASE_CONTAINERS_STACK_CONTAINER_H_ |
OLD | NEW |