Chromium Code Reviews| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "cc/base/cc_export.h" | 14 #include "cc/base/cc_export.h" |
| 15 #include "cc/base/list_container_helper.h" | 15 #include "cc/base/list_container_helper.h" |
| 16 | 16 |
| 17 namespace cc { | 17 namespace cc { |
| 18 | 18 |
| 19 // ListContainer is a container type that handles allocating contiguous memory | 19 // ListContainer is a container type that handles allocating contiguous memory |
| 20 // for new elements and traversing through elements with either iterator or | 20 // for new elements and traversing through elements with either iterator or |
| 21 // reverse iterator. Since this container hands out raw pointers of its | 21 // reverse iterator. Since this container hands out raw pointers of its |
| 22 // elements, it is very important that this container never reallocate its | 22 // elements, it is very important that this container never reallocate its |
| 23 // memory so those raw pointer will continue to be valid. This class is used to | 23 // memory so those raw pointer will continue to be valid. This class is used to |
| 24 // contain SharedQuadState or DrawQuad. Since the size of each DrawQuad varies, | 24 // contain SharedQuadState or DrawQuad. Since the size of each DrawQuad varies, |
| 25 // to hold DrawQuads, the allocations size of each element in this class is | 25 // to hold DrawQuads, the allocations size of each element in this class is |
| 26 // LargestDrawQuadSize while BaseElementType is DrawQuad. | 26 // LargestDrawQuadSize while BaseElementType is DrawQuad. |
| 27 template <class BaseElementType> | 27 template <class BaseElementType> |
| 28 class ListContainer { | 28 class ListContainer { |
| 29 public: | 29 public: |
| 30 ListContainer(ListContainer<BaseElementType>&& other) { | |
| 31 helper_.data_.swap(other.helper_.data_); | |
| 32 } | |
| 33 | |
| 30 // BaseElementType is the type of raw pointers this class hands out; however, | 34 // BaseElementType is the type of raw pointers this class hands out; however, |
| 31 // its derived classes might require different memory sizes. | 35 // its derived classes might require different memory sizes. |
| 32 // max_size_for_derived_class the largest memory size required for all the | 36 // max_size_for_derived_class the largest memory size required for all the |
| 33 // derived classes to use for allocation. | 37 // derived classes to use for allocation. |
| 34 explicit ListContainer(size_t max_size_for_derived_class) | 38 explicit ListContainer(size_t max_size_for_derived_class) |
| 35 : helper_(max_size_for_derived_class) {} | 39 : helper_(max_size_for_derived_class) {} |
| 36 | 40 |
| 37 // This constructor omits input variable for max_size_for_derived_class. This | 41 // This constructor omits input variable for max_size_for_derived_class. This |
| 38 // is used when there is no derived classes from BaseElementType we need to | 42 // is used when there is no derived classes from BaseElementType we need to |
| 39 // worry about, and allocation size is just sizeof(BaseElementType). | 43 // worry about, and allocation size is just sizeof(BaseElementType). |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 Iterator InsertBeforeAndInvalidateAllPointers(Iterator at, size_t count) { | 141 Iterator InsertBeforeAndInvalidateAllPointers(Iterator at, size_t count) { |
| 138 helper_.InsertBeforeAndInvalidateAllPointers(&at, count); | 142 helper_.InsertBeforeAndInvalidateAllPointers(&at, count); |
| 139 Iterator result = at; | 143 Iterator result = at; |
| 140 for (size_t i = 0; i < count; ++i) { | 144 for (size_t i = 0; i < count; ++i) { |
| 141 new (at.item_iterator) DerivedElementType(); | 145 new (at.item_iterator) DerivedElementType(); |
| 142 ++at; | 146 ++at; |
| 143 } | 147 } |
| 144 return result; | 148 return result; |
| 145 } | 149 } |
| 146 | 150 |
| 151 ListContainer<BaseElementType>& operator=( | |
|
dcheng
2016/06/15 12:27:19
<BaseElementType> shouldn't be needed here.
Fady Samuel
2016/06/15 14:46:25
Done. Also turns out I don't need the move constru
| |
| 152 ListContainer<BaseElementType>&& other) { | |
| 153 helper_.data_.swap(other.helper_.data_); | |
| 154 return *this; | |
| 155 } | |
| 156 | |
| 147 template <typename DerivedElementType> | 157 template <typename DerivedElementType> |
| 148 void swap(ListContainer<DerivedElementType>& other) { | 158 void swap(ListContainer<DerivedElementType>& other) { |
| 149 helper_.data_.swap(other.helper_.data_); | 159 helper_.data_.swap(other.helper_.data_); |
| 150 } | 160 } |
| 151 | 161 |
| 152 // Appends a new item without copying. The original item will not be | 162 // Appends a new item without copying. The original item will not be |
| 153 // destructed and will be replaced with a new DerivedElementType. The | 163 // destructed and will be replaced with a new DerivedElementType. The |
| 154 // DerivedElementType does not have to match the moved type as a full block | 164 // DerivedElementType does not have to match the moved type as a full block |
| 155 // of memory will be moved (up to MaxSizeForDerivedClass()). A pointer to | 165 // of memory will be moved (up to MaxSizeForDerivedClass()). A pointer to |
| 156 // the moved element is returned. | 166 // the moved element is returned. |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 338 | 348 |
| 339 private: | 349 private: |
| 340 ListContainerHelper helper_; | 350 ListContainerHelper helper_; |
| 341 | 351 |
| 342 DISALLOW_COPY_AND_ASSIGN(ListContainer); | 352 DISALLOW_COPY_AND_ASSIGN(ListContainer); |
| 343 }; | 353 }; |
| 344 | 354 |
| 345 } // namespace cc | 355 } // namespace cc |
| 346 | 356 |
| 347 #endif // CC_BASE_LIST_CONTAINER_H_ | 357 #endif // CC_BASE_LIST_CONTAINER_H_ |
| OLD | NEW |