| 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 "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 ConstIterator cend() const; | 154 ConstIterator cend() const; |
| 155 Iterator begin(); | 155 Iterator begin(); |
| 156 Iterator end(); | 156 Iterator end(); |
| 157 | 157 |
| 158 Iterator IteratorAt(size_t index); | 158 Iterator IteratorAt(size_t index); |
| 159 ConstIterator IteratorAt(size_t index) const; | 159 ConstIterator IteratorAt(size_t index) const; |
| 160 | 160 |
| 161 size_t size() const; | 161 size_t size() const; |
| 162 bool empty() const; | 162 bool empty() const; |
| 163 | 163 |
| 164 size_t MaxSizeForDerivedClass() const; |
| 165 |
| 164 // Unlike the ListContainer method, this one does not invoke element | 166 // Unlike the ListContainer method, this one does not invoke element |
| 165 // destructors. | 167 // destructors. |
| 166 void clear(); | 168 void clear(); |
| 167 | 169 |
| 168 size_t AvailableSizeWithoutAnotherAllocationForTesting() const; | 170 size_t AvailableSizeWithoutAnotherAllocationForTesting() const; |
| 169 | 171 |
| 170 // Hands out memory location for an element at the end of data structure. | 172 // Hands out memory location for an element at the end of data structure. |
| 171 void* Allocate(size_t size_of_actual_element_in_bytes); | 173 void* Allocate(size_t size_of_actual_element_in_bytes); |
| 172 | 174 |
| 173 scoped_ptr<ListContainerCharAllocator> data_; | 175 scoped_ptr<ListContainerCharAllocator> data_; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 const BaseElementType* ElementAt(size_t index) const { | 265 const BaseElementType* ElementAt(size_t index) const { |
| 264 return *ConstIterator(IteratorAt(index)); | 266 return *ConstIterator(IteratorAt(index)); |
| 265 } | 267 } |
| 266 | 268 |
| 267 // Take in derived element type and construct it at location generated by | 269 // Take in derived element type and construct it at location generated by |
| 268 // Allocate(). | 270 // Allocate(). |
| 269 template <typename DerivedElementType> | 271 template <typename DerivedElementType> |
| 270 DerivedElementType* AllocateAndConstruct() { | 272 DerivedElementType* AllocateAndConstruct() { |
| 271 return new (Allocate(sizeof(DerivedElementType))) DerivedElementType; | 273 return new (Allocate(sizeof(DerivedElementType))) DerivedElementType; |
| 272 } | 274 } |
| 275 |
| 273 // Take in derived element type and copy construct it at location generated by | 276 // Take in derived element type and copy construct it at location generated by |
| 274 // Allocate(). | 277 // Allocate(). |
| 275 template <typename DerivedElementType> | 278 template <typename DerivedElementType> |
| 276 DerivedElementType* AllocateAndCopyFrom(const DerivedElementType* source) { | 279 DerivedElementType* AllocateAndCopyFrom(const DerivedElementType* source) { |
| 277 return new (Allocate(sizeof(DerivedElementType))) | 280 return new (Allocate(sizeof(DerivedElementType))) |
| 278 DerivedElementType(*source); | 281 DerivedElementType(*source); |
| 279 } | 282 } |
| 283 |
| 280 // Construct a new element on top of an existing one. | 284 // Construct a new element on top of an existing one. |
| 281 template <typename DerivedElementType> | 285 template <typename DerivedElementType> |
| 282 DerivedElementType* ReplaceExistingElement(Iterator at) { | 286 DerivedElementType* ReplaceExistingElement(Iterator at) { |
| 283 at->~BaseElementType(); | 287 at->~BaseElementType(); |
| 284 return new (*at) DerivedElementType(); | 288 return new (*at) DerivedElementType(); |
| 285 } | 289 } |
| 286 | 290 |
| 291 // Appends a new item without copying. The original item will not be |
| 292 // destructed and will be replaced with a new DerivedElementType. The |
| 293 // DerivedElementType does not have to match the moved type as a full block |
| 294 // of memory will be moved (up to MaxSizeForDerivedClass()). |
| 295 template <typename DerivedElementType> |
| 296 void AppendByMoving(DerivedElementType* item) { |
| 297 size_t max_size_for_derived_class = MaxSizeForDerivedClass(); |
| 298 void* new_item = Allocate(max_size_for_derived_class); |
| 299 memcpy(new_item, static_cast<void*>(item), max_size_for_derived_class); |
| 300 // Construct a new element in-place so it can be destructed safely. |
| 301 new (item) DerivedElementType; |
| 302 } |
| 303 |
| 287 using ListContainerBase::size; | 304 using ListContainerBase::size; |
| 288 using ListContainerBase::empty; | 305 using ListContainerBase::empty; |
| 289 | 306 |
| 290 void clear() { | 307 void clear() { |
| 291 for (Iterator i = begin(); i != end(); ++i) { | 308 for (Iterator i = begin(); i != end(); ++i) { |
| 292 i->~BaseElementType(); | 309 i->~BaseElementType(); |
| 293 } | 310 } |
| 294 ListContainerBase::clear(); | 311 ListContainerBase::clear(); |
| 295 } | 312 } |
| 296 | 313 |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 ListContainerBase::ConstReverseIterator base_iterator) | 464 ListContainerBase::ConstReverseIterator base_iterator) |
| 448 : ListContainerBase::ConstReverseIterator(base_iterator) {} | 465 : ListContainerBase::ConstReverseIterator(base_iterator) {} |
| 449 friend ConstReverseIterator ListContainer<BaseElementType>::crbegin() const; | 466 friend ConstReverseIterator ListContainer<BaseElementType>::crbegin() const; |
| 450 friend ConstReverseIterator ListContainer<BaseElementType>::crend() const; | 467 friend ConstReverseIterator ListContainer<BaseElementType>::crend() const; |
| 451 }; | 468 }; |
| 452 }; | 469 }; |
| 453 | 470 |
| 454 } // namespace cc | 471 } // namespace cc |
| 455 | 472 |
| 456 #endif // CC_BASE_LIST_CONTAINER_H_ | 473 #endif // CC_BASE_LIST_CONTAINER_H_ |
| OLD | NEW |