OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 ListContainer_h | 5 #ifndef ListContainer_h |
6 #define ListContainer_h | 6 #define ListContainer_h |
7 | 7 |
8 #include "platform/PlatformExport.h" | 8 #include "platform/PlatformExport.h" |
9 #include "wtf/Forward.h" | 9 #include "wtf/Forward.h" |
10 #include "wtf/Noncopyable.h" | 10 #include "wtf/Noncopyable.h" |
11 #include "wtf/OwnPtr.h" | 11 #include "wtf/OwnPtr.h" |
12 #include "wtf/Utility.h" | |
12 #include "wtf/Vector.h" | 13 #include "wtf/Vector.h" |
13 | 14 |
14 #include <iterator> | 15 #include <iterator> |
15 | 16 |
16 namespace blink { | 17 namespace blink { |
17 | 18 |
18 // ListContainer is a copy of cc/base/list_container.h and is only here | 19 // ListContainer is a copy of cc/base/list_container.h and is only here |
19 // temporarily pending the Blink merge. | 20 // temporarily pending the Blink merge. |
20 // | 21 // |
21 // Use of this class is discouraged. | 22 // Use of this class is discouraged. |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 | 228 |
228 // TODO(weiliangc): front(), back() and elementAt() function should return | 229 // TODO(weiliangc): front(), back() and elementAt() function should return |
229 // reference, consistent with container-of-object. | 230 // reference, consistent with container-of-object. |
230 BaseElementType* front() { return *begin(); } | 231 BaseElementType* front() { return *begin(); } |
231 BaseElementType* back() { return *rbegin(); } | 232 BaseElementType* back() { return *rbegin(); } |
232 const BaseElementType* front() const { return *begin(); } | 233 const BaseElementType* front() const { return *begin(); } |
233 const BaseElementType* back() const { return *rbegin(); } | 234 const BaseElementType* back() const { return *rbegin(); } |
234 | 235 |
235 BaseElementType* elementAt(size_t index) | 236 BaseElementType* elementAt(size_t index) |
236 { | 237 { |
237 return *Iterator(iteratorAt(index)); | 238 return *iteratorAt(index); |
238 } | 239 } |
240 | |
239 const BaseElementType* elementAt(size_t index) const | 241 const BaseElementType* elementAt(size_t index) const |
240 { | 242 { |
241 return *ConstIterator(iteratorAt(index)); | 243 return *iteratorAt(index); |
244 } | |
245 | |
246 Iterator iteratorAt(size_t index) | |
247 { | |
248 return Iterator(ListContainerBase::iteratorAt(index)); | |
249 } | |
250 | |
251 ConstIterator iteratorAt(size_t index) const | |
252 { | |
253 return ConstIterator(ListContainerBase::iteratorAt(index)); | |
242 } | 254 } |
243 | 255 |
244 // Take in derived element type and construct it at location generated by Al locate(). | 256 // Take in derived element type and construct it at location generated by Al locate(). |
245 template <typename DerivedElementType> | 257 template <typename DerivedElementType, typename... Args> |
246 DerivedElementType* allocateAndConstruct() | 258 DerivedElementType* allocateAndConstruct(Args&&... args) |
247 { | 259 { |
248 return new (allocate(sizeof(DerivedElementType))) DerivedElementType; | 260 return new (allocate(sizeof(DerivedElementType))) DerivedElementType(WTF ::forward<Args>(args)...); |
danakj
2015/06/30 18:49:01
Can you add a unit test with non-0 number of argum
pdr.
2015/06/30 22:16:06
Done, added ListContainerTest::AllocateAndConstruc
| |
249 } | 261 } |
250 | 262 |
251 // Take in derived element type and copy construct it at location generated by | 263 // Take in derived element type and copy construct it at location generated by |
252 // Allocate(). | 264 // Allocate(). |
253 template <typename DerivedElementType> | 265 template <typename DerivedElementType> |
254 DerivedElementType* allocateAndCopyFrom(const DerivedElementType* source) | 266 DerivedElementType* allocateAndCopyFrom(const DerivedElementType* source) |
255 { | 267 { |
256 return new (allocate(sizeof(DerivedElementType))) DerivedElementType(*so urce); | 268 return new (allocate(sizeof(DerivedElementType))) DerivedElementType(*so urce); |
257 } | 269 } |
258 | 270 |
259 // Construct a new element on top of an existing one. | 271 // Construct a new element on top of an existing one. |
260 template <typename DerivedElementType> | 272 template <typename DerivedElementType> |
261 DerivedElementType* replaceExistingElement(Iterator at) | 273 DerivedElementType* replaceExistingElement(Iterator at) |
262 { | 274 { |
263 at->~BaseElementType(); | 275 at->~BaseElementType(); |
264 return new (*at) DerivedElementType(); | 276 return new (*at) DerivedElementType(); |
265 } | 277 } |
266 | 278 |
267 template <typename DerivedElementType> | 279 template <typename DerivedElementType> |
268 void swap(ListContainer<DerivedElementType>& other) | 280 void swap(ListContainer<DerivedElementType>& other) |
269 { | 281 { |
270 m_data.swap(other.m_data); | 282 m_data.swap(other.m_data); |
271 } | 283 } |
272 | 284 |
273 // Appends a new item without copying. The original item will not be | 285 // Appends a new item without copying. The original item will not be |
274 // destructed and will be replaced with a new DerivedElementType. The | 286 // destructed and will be replaced with a new DerivedElementType. The |
275 // DerivedElementType does not have to match the moved type as a full block | 287 // DerivedElementType does not have to match the moved type as a full block |
276 // of memory will be moved (up to maxSizeForDerivedClass()). | 288 // of memory will be moved (up to maxSizeForDerivedClass()). A pointer |
289 // to the moved element is returned. | |
277 template <typename DerivedElementType> | 290 template <typename DerivedElementType> |
278 void appendByMoving(DerivedElementType* item) | 291 DerivedElementType* appendByMoving(DerivedElementType* item) |
279 { | 292 { |
280 size_t maxSize = maxSizeForDerivedClass(); | 293 size_t maxSize = maxSizeForDerivedClass(); |
281 void* newItem = allocate(maxSize); | 294 void* newItem = allocate(maxSize); |
282 memcpy(newItem, static_cast<void*>(item), maxSize); | 295 memcpy(newItem, static_cast<void*>(item), maxSize); |
283 // Construct a new element in-place so it can be destructed safely. | 296 // Construct a new element in-place so it can be destructed safely. |
284 new (item) DerivedElementType; | 297 new (item) DerivedElementType; |
298 return static_cast<DerivedElementType*>(newItem); | |
285 } | 299 } |
286 | 300 |
287 using ListContainerBase::size; | 301 using ListContainerBase::size; |
288 using ListContainerBase::empty; | 302 using ListContainerBase::empty; |
289 | 303 |
290 void clear() | 304 void clear() |
291 { | 305 { |
292 for (Iterator i = begin(); i != end(); ++i) | 306 for (Iterator i = begin(); i != end(); ++i) |
293 i->~BaseElementType(); | 307 i->~BaseElementType(); |
294 ListContainerBase::clear(); | 308 ListContainerBase::clear(); |
(...skipping 24 matching lines...) Expand all Loading... | |
319 ++m_index; | 333 ++m_index; |
320 return *this; | 334 return *this; |
321 } | 335 } |
322 | 336 |
323 private: | 337 private: |
324 explicit Iterator(const ListContainerBase::Iterator& baseIterator) | 338 explicit Iterator(const ListContainerBase::Iterator& baseIterator) |
325 : ListContainerBase::Iterator(baseIterator) {} | 339 : ListContainerBase::Iterator(baseIterator) {} |
326 friend Iterator ListContainer<BaseElementType>::begin(); | 340 friend Iterator ListContainer<BaseElementType>::begin(); |
327 friend Iterator ListContainer<BaseElementType>::end(); | 341 friend Iterator ListContainer<BaseElementType>::end(); |
328 friend BaseElementType* ListContainer<BaseElementType>::elementAt(size_t index); | 342 friend BaseElementType* ListContainer<BaseElementType>::elementAt(size_t index); |
343 friend Iterator ListContainer<BaseElementType>::iteratorAt(size_t index) ; | |
329 }; | 344 }; |
330 | 345 |
331 class ConstIterator : public ListContainerBase::ConstIterator { | 346 class ConstIterator : public ListContainerBase::ConstIterator { |
332 // This class is only defined to forward iterate through | 347 // This class is only defined to forward iterate through |
333 // ListContainerCharAllocator. | 348 // ListContainerCharAllocator. |
334 public: | 349 public: |
335 ConstIterator(ListContainerCharAllocator* container, size_t vectorIndex, char* itemIter, size_t index) | 350 ConstIterator(ListContainerCharAllocator* container, size_t vectorIndex, char* itemIter, size_t index) |
336 : ListContainerBase::ConstIterator(container, vectorIndex, itemIter, index) {} | 351 : ListContainerBase::ConstIterator(container, vectorIndex, itemIter, index) {} |
337 ConstIterator(const Iterator& other) | 352 ConstIterator(const Iterator& other) |
338 : ListContainerBase::ConstIterator(other) {} | 353 : ListContainerBase::ConstIterator(other) {} |
(...skipping 18 matching lines...) Expand all Loading... | |
357 ++m_index; | 372 ++m_index; |
358 return *this; | 373 return *this; |
359 } | 374 } |
360 | 375 |
361 private: | 376 private: |
362 explicit ConstIterator(const ListContainerBase::ConstIterator& baseItera tor) | 377 explicit ConstIterator(const ListContainerBase::ConstIterator& baseItera tor) |
363 : ListContainerBase::ConstIterator(baseIterator) {} | 378 : ListContainerBase::ConstIterator(baseIterator) {} |
364 friend ConstIterator ListContainer<BaseElementType>::cbegin() const; | 379 friend ConstIterator ListContainer<BaseElementType>::cbegin() const; |
365 friend ConstIterator ListContainer<BaseElementType>::cend() const; | 380 friend ConstIterator ListContainer<BaseElementType>::cend() const; |
366 friend const BaseElementType* ListContainer<BaseElementType>::elementAt( size_t index) const; | 381 friend const BaseElementType* ListContainer<BaseElementType>::elementAt( size_t index) const; |
382 friend ConstIterator ListContainer<BaseElementType>::iteratorAt(size_t i ndex) const; | |
367 }; | 383 }; |
368 | 384 |
369 class ReverseIterator : public ListContainerBase::ReverseIterator { | 385 class ReverseIterator : public ListContainerBase::ReverseIterator { |
370 // This class is only defined to reverse iterate through | 386 // This class is only defined to reverse iterate through |
371 // ListContainerCharAllocator. | 387 // ListContainerCharAllocator. |
372 public: | 388 public: |
373 ReverseIterator(ListContainerCharAllocator* container, size_t vectorInde x, char* itemIter, size_t index) | 389 ReverseIterator(ListContainerCharAllocator* container, size_t vectorInde x, char* itemIter, size_t index) |
374 : ListContainerBase::ReverseIterator(container, vectorIndex, itemIte r, index) {} | 390 : ListContainerBase::ReverseIterator(container, vectorIndex, itemIte r, index) {} |
375 | 391 |
376 BaseElementType* operator->() const | 392 BaseElementType* operator->() const |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 explicit ConstReverseIterator(ListContainerBase::ConstReverseIterator ba seIterator) | 451 explicit ConstReverseIterator(ListContainerBase::ConstReverseIterator ba seIterator) |
436 : ListContainerBase::ConstReverseIterator(baseIterator) {} | 452 : ListContainerBase::ConstReverseIterator(baseIterator) {} |
437 friend ConstReverseIterator ListContainer<BaseElementType>::crbegin() co nst; | 453 friend ConstReverseIterator ListContainer<BaseElementType>::crbegin() co nst; |
438 friend ConstReverseIterator ListContainer<BaseElementType>::crend() cons t; | 454 friend ConstReverseIterator ListContainer<BaseElementType>::crend() cons t; |
439 }; | 455 }; |
440 }; | 456 }; |
441 | 457 |
442 } // namespace blink | 458 } // namespace blink |
443 | 459 |
444 #endif // ListContainer_h | 460 #endif // ListContainer_h |
OLD | NEW |