OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 CC_BASE_LIST_CONTAINER_H_ | 5 #ifndef CC_BASE_LIST_CONTAINER_H_ |
6 #define CC_BASE_LIST_CONTAINER_H_ | 6 #define CC_BASE_LIST_CONTAINER_H_ |
7 | 7 |
8 #include <deque> | |
9 | |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/macros.h" | 11 #include "base/macros.h" |
10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
11 #include "cc/base/cc_export.h" | 13 #include "cc/base/cc_export.h" |
12 | 14 |
13 namespace cc { | 15 namespace cc { |
14 | 16 |
15 // ListContainer is a container type that handles allocating contiguous memory | 17 // ListContainer is a container type that handles allocating contiguous memory |
16 // for new elements and traversing through elements with either iterator or | 18 // for new elements and traversing through elements with either iterator or |
17 // reverse iterator. Since this container hands out raw pointers of its | 19 // reverse iterator. Since this container hands out raw pointers of its |
18 // elements, it is very important that this container never reallocate its | 20 // elements, it is very important that this container never reallocate its |
19 // memory so those raw pointer will continue to be valid. This class is used to | 21 // memory so those raw pointer will continue to be valid. This class is used to |
20 // contain SharedQuadState or DrawQuad. Since the size of each DrawQuad varies, | 22 // contain SharedQuadState or DrawQuad. Since the size of each DrawQuad varies, |
21 // to hold DrawQuads, the allocations size of each element in this class is | 23 // to hold DrawQuads, the allocations size of each element in this class is |
22 // LargestDrawQuadSize while BaseElementType is DrawQuad. | 24 // LargestDrawQuadSize while BaseElementType is DrawQuad. |
23 | 25 |
24 // Helper class for non-templated logic. All methods are private, and only | 26 // Helper class for non-templated logic. All methods are private, and only |
25 // exposed to friend classes. | 27 // exposed to friend classes. |
26 // For usage, see comments in ListContainer. | 28 // For usage, see comments in ListContainer. |
27 class CC_EXPORT ListContainerHelper { | 29 class CC_EXPORT ListContainerHelper { |
28 private: | 30 private: |
29 template <typename T> | 31 template <typename T> |
30 friend class CC_EXPORT ListContainer; | 32 friend class CC_EXPORT ListContainer; |
31 | 33 |
34 template <typename T> | |
35 friend class CC_EXPORT RandomAccessListContainer; | |
36 | |
32 explicit ListContainerHelper(size_t max_size_for_derived_class); | 37 explicit ListContainerHelper(size_t max_size_for_derived_class); |
33 ListContainerHelper(size_t max_size_for_derived_class, | 38 ListContainerHelper(size_t max_size_for_derived_class, |
34 size_t num_of_elements_to_reserve_for); | 39 size_t num_of_elements_to_reserve_for); |
35 ~ListContainerHelper(); | 40 ~ListContainerHelper(); |
36 | 41 |
37 // This class deals only with char* and void*. It does allocation and passing | 42 // This class deals only with char* and void*. It does allocation and passing |
38 // out raw pointers, as well as memory deallocation when being destroyed. | 43 // out raw pointers, as well as memory deallocation when being destroyed. |
39 class ListContainerCharAllocator; | 44 class ListContainerCharAllocator; |
40 | 45 |
41 // This class points to a certain position inside memory of | 46 // This class points to a certain position inside memory of |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
494 ListContainerHelper::ConstReverseIterator base_iterator) | 499 ListContainerHelper::ConstReverseIterator base_iterator) |
495 : ListContainerHelper::ConstReverseIterator(base_iterator) {} | 500 : ListContainerHelper::ConstReverseIterator(base_iterator) {} |
496 friend ConstReverseIterator ListContainer<BaseElementType>::crbegin() const; | 501 friend ConstReverseIterator ListContainer<BaseElementType>::crbegin() const; |
497 friend ConstReverseIterator ListContainer<BaseElementType>::crend() const; | 502 friend ConstReverseIterator ListContainer<BaseElementType>::crend() const; |
498 }; | 503 }; |
499 | 504 |
500 private: | 505 private: |
501 ListContainerHelper helper_; | 506 ListContainerHelper helper_; |
502 }; | 507 }; |
503 | 508 |
509 template <class BaseElementType> | |
510 class CC_EXPORT RandomAccessListContainer { | |
511 public: | |
512 // BaseElementType is the type of raw pointers this class hands out; however, | |
513 // its derived classes might require different memory sizes. | |
514 // max_size_for_derived_class the largest memory size required for all the | |
515 // derived classes to use for allocation. | |
516 explicit RandomAccessListContainer(size_t max_size_for_derived_class) | |
517 : helper_(max_size_for_derived_class) {} | |
518 | |
519 // This constructor omits input variable for max_size_for_derived_class. This | |
520 // is used when there is no derived classes from BaseElementType we need to | |
521 // worry about, and allocation size is just sizeof(BaseElementType). | |
522 RandomAccessListContainer() : helper_(sizeof(BaseElementType)) {} | |
523 | |
524 // This constructor reserves the requested memory up front so only single | |
525 // allocation is needed. When num_of_elements_to_reserve_for is zero, use the | |
526 // default size. | |
527 RandomAccessListContainer(size_t max_size_for_derived_class, | |
528 size_t num_of_elements_to_reserve_for) | |
529 : helper_(max_size_for_derived_class, num_of_elements_to_reserve_for) {} | |
530 | |
531 ~RandomAccessListContainer() { | |
532 for (BaseElementType* item : items_) | |
533 item->~BaseElementType(); | |
534 } | |
535 | |
536 void clear() { | |
537 for (BaseElementType* item : items_) | |
538 item->~BaseElementType(); | |
539 helper_.clear(); | |
540 items_.clear(); | |
541 } | |
542 | |
543 bool empty() const { return helper_.empty(); } | |
544 size_t size() const { return helper_.size(); } | |
545 size_t GetCapacityInBytes() const { return helper_.GetCapacityInBytes(); } | |
546 | |
547 template <typename DerivedElementType> | |
548 DerivedElementType* AllocateAndConstruct() { | |
549 auto* value = | |
550 new (helper_.Allocate(sizeof(DerivedElementType))) DerivedElementType; | |
551 items_.push_back(value); | |
552 return value; | |
553 } | |
554 | |
555 void RemoveLast() { | |
556 items_.back()->~BaseElementType(); | |
557 items_.pop_back(); | |
558 helper_.RemoveLast(); | |
559 } | |
560 | |
561 const BaseElementType* operator[](size_t index) const { | |
weiliangc
2015/09/14 20:02:47
Can you also add at()? Or explain in comment this
vmpstr
2015/09/15 00:17:48
I'm not sure why we need at() as well. I assume yo
| |
562 DCHECK_GE(index, 0u); | |
563 DCHECK_LT(index, items_.size()); | |
564 return items_[index]; | |
565 } | |
566 | |
567 using ConstIterator = typename std::deque<BaseElementType*>::const_iterator; | |
weiliangc
2015/09/14 20:02:47
Seems like only have const access, is this intende
vmpstr
2015/09/15 00:17:48
Hmm. The const access is to the iterator whose val
| |
568 ConstIterator begin() const { return items_.begin(); } | |
569 ConstIterator end() const { return items_.end(); } | |
570 | |
571 private: | |
572 ListContainerHelper helper_; | |
573 std::deque<BaseElementType*> items_; | |
574 }; | |
575 | |
504 } // namespace cc | 576 } // namespace cc |
505 | 577 |
506 #endif // CC_BASE_LIST_CONTAINER_H_ | 578 #endif // CC_BASE_LIST_CONTAINER_H_ |
OLD | NEW |