Chromium Code Reviews| 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" |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 | 227 |
| 228 // TODO(weiliangc): front(), back() and elementAt() function should return | 228 // TODO(weiliangc): front(), back() and elementAt() function should return |
| 229 // reference, consistent with container-of-object. | 229 // reference, consistent with container-of-object. |
| 230 BaseElementType* front() { return *begin(); } | 230 BaseElementType* front() { return *begin(); } |
| 231 BaseElementType* back() { return *rbegin(); } | 231 BaseElementType* back() { return *rbegin(); } |
| 232 const BaseElementType* front() const { return *begin(); } | 232 const BaseElementType* front() const { return *begin(); } |
| 233 const BaseElementType* back() const { return *rbegin(); } | 233 const BaseElementType* back() const { return *rbegin(); } |
| 234 | 234 |
| 235 BaseElementType* elementAt(size_t index) | 235 BaseElementType* elementAt(size_t index) |
| 236 { | 236 { |
| 237 return *Iterator(iteratorAt(index)); | 237 return *iteratorAt(index); |
|
danakj
2015/06/29 17:43:51
Is this a change we could/should make in cc/ too?
pdr.
2015/06/29 22:20:03
Done in https://codereview.chromium.org/1219753003
| |
| 238 } | 238 } |
| 239 | |
| 239 const BaseElementType* elementAt(size_t index) const | 240 const BaseElementType* elementAt(size_t index) const |
| 240 { | 241 { |
| 241 return *ConstIterator(iteratorAt(index)); | 242 return *iteratorAt(index); |
| 243 } | |
| 244 | |
| 245 Iterator iteratorAt(size_t index) | |
| 246 { | |
| 247 return Iterator(ListContainerBase::iteratorAt(index)); | |
| 248 } | |
| 249 | |
| 250 ConstIterator iteratorAt(size_t index) const | |
| 251 { | |
| 252 return ConstIterator(ListContainerBase::iteratorAt(index)); | |
| 253 } | |
| 254 | |
| 255 BaseElementType& operator[](size_t index) | |
|
danakj
2015/06/29 17:43:51
I recommend not adding these. It looks like it sho
pdr.
2015/06/29 22:20:03
I added these for similar reasons. These are heavi
danakj
2015/06/29 22:29:47
ah OK. iteratorat sounds OK to me. operator[] less
| |
| 256 { | |
| 257 return *elementAt(index); | |
| 258 } | |
| 259 | |
| 260 const BaseElementType& operator[](size_t index) const | |
| 261 { | |
| 262 return *elementAt(index); | |
| 263 } | |
| 264 | |
| 265 // Allocate a new, uninitialized DerivedElementType. Use with caution! | |
|
danakj
2015/06/29 17:43:51
Why do we need this?
pdr.
2015/06/29 22:20:03
We no longer do because I've made allocateAndConst
| |
| 266 template <typename DerivedElementType> | |
| 267 DerivedElementType* allocateWithoutConstruction() | |
| 268 { | |
| 269 return static_cast<DerivedElementType*>(allocate(sizeof(DerivedElementTy pe))); | |
| 242 } | 270 } |
| 243 | 271 |
| 244 // Take in derived element type and construct it at location generated by Al locate(). | 272 // Take in derived element type and construct it at location generated by Al locate(). |
| 245 template <typename DerivedElementType> | 273 template <typename DerivedElementType> |
| 246 DerivedElementType* allocateAndConstruct() | 274 DerivedElementType* allocateAndConstruct() |
| 247 { | 275 { |
| 248 return new (allocate(sizeof(DerivedElementType))) DerivedElementType; | 276 return new (allocate(sizeof(DerivedElementType))) DerivedElementType; |
| 249 } | 277 } |
| 250 | 278 |
| 251 // Take in derived element type and copy construct it at location generated by | 279 // Take in derived element type and copy construct it at location generated by |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 266 | 294 |
| 267 template <typename DerivedElementType> | 295 template <typename DerivedElementType> |
| 268 void swap(ListContainer<DerivedElementType>& other) | 296 void swap(ListContainer<DerivedElementType>& other) |
| 269 { | 297 { |
| 270 m_data.swap(other.m_data); | 298 m_data.swap(other.m_data); |
| 271 } | 299 } |
| 272 | 300 |
| 273 // Appends a new item without copying. The original item will not be | 301 // Appends a new item without copying. The original item will not be |
| 274 // destructed and will be replaced with a new DerivedElementType. The | 302 // destructed and will be replaced with a new DerivedElementType. The |
| 275 // DerivedElementType does not have to match the moved type as a full block | 303 // DerivedElementType does not have to match the moved type as a full block |
| 276 // of memory will be moved (up to maxSizeForDerivedClass()). | 304 // of memory will be moved (up to maxSizeForDerivedClass()). A pointer |
| 305 // to the moved element is returned. | |
| 277 template <typename DerivedElementType> | 306 template <typename DerivedElementType> |
| 278 void appendByMoving(DerivedElementType* item) | 307 DerivedElementType* appendByMoving(DerivedElementType* item) |
|
danakj
2015/06/29 17:43:51
Can we do this to the cc/ class too?
pdr.
2015/06/29 22:20:03
Done in https://codereview.chromium.org/1219753003
| |
| 279 { | 308 { |
| 280 size_t maxSize = maxSizeForDerivedClass(); | 309 size_t maxSize = maxSizeForDerivedClass(); |
| 281 void* newItem = allocate(maxSize); | 310 void* newItem = allocate(maxSize); |
| 282 memcpy(newItem, static_cast<void*>(item), maxSize); | 311 memcpy(newItem, static_cast<void*>(item), maxSize); |
| 283 // Construct a new element in-place so it can be destructed safely. | 312 // Construct a new element in-place so it can be destructed safely. |
| 284 new (item) DerivedElementType; | 313 new (item) DerivedElementType; |
| 314 return static_cast<DerivedElementType*>(newItem); | |
| 285 } | 315 } |
| 286 | 316 |
| 287 using ListContainerBase::size; | 317 using ListContainerBase::size; |
| 288 using ListContainerBase::empty; | 318 using ListContainerBase::empty; |
| 289 | 319 |
| 290 void clear() | 320 void clear() |
| 291 { | 321 { |
| 292 for (Iterator i = begin(); i != end(); ++i) | 322 for (Iterator i = begin(); i != end(); ++i) |
| 293 i->~BaseElementType(); | 323 i->~BaseElementType(); |
| 294 ListContainerBase::clear(); | 324 ListContainerBase::clear(); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 319 ++m_index; | 349 ++m_index; |
| 320 return *this; | 350 return *this; |
| 321 } | 351 } |
| 322 | 352 |
| 323 private: | 353 private: |
| 324 explicit Iterator(const ListContainerBase::Iterator& baseIterator) | 354 explicit Iterator(const ListContainerBase::Iterator& baseIterator) |
| 325 : ListContainerBase::Iterator(baseIterator) {} | 355 : ListContainerBase::Iterator(baseIterator) {} |
| 326 friend Iterator ListContainer<BaseElementType>::begin(); | 356 friend Iterator ListContainer<BaseElementType>::begin(); |
| 327 friend Iterator ListContainer<BaseElementType>::end(); | 357 friend Iterator ListContainer<BaseElementType>::end(); |
| 328 friend BaseElementType* ListContainer<BaseElementType>::elementAt(size_t index); | 358 friend BaseElementType* ListContainer<BaseElementType>::elementAt(size_t index); |
| 359 friend Iterator ListContainer<BaseElementType>::iteratorAt(size_t index) ; | |
| 329 }; | 360 }; |
| 330 | 361 |
| 331 class ConstIterator : public ListContainerBase::ConstIterator { | 362 class ConstIterator : public ListContainerBase::ConstIterator { |
| 332 // This class is only defined to forward iterate through | 363 // This class is only defined to forward iterate through |
| 333 // ListContainerCharAllocator. | 364 // ListContainerCharAllocator. |
| 334 public: | 365 public: |
| 335 ConstIterator(ListContainerCharAllocator* container, size_t vectorIndex, char* itemIter, size_t index) | 366 ConstIterator(ListContainerCharAllocator* container, size_t vectorIndex, char* itemIter, size_t index) |
| 336 : ListContainerBase::ConstIterator(container, vectorIndex, itemIter, index) {} | 367 : ListContainerBase::ConstIterator(container, vectorIndex, itemIter, index) {} |
| 337 ConstIterator(const Iterator& other) | 368 ConstIterator(const Iterator& other) |
| 338 : ListContainerBase::ConstIterator(other) {} | 369 : ListContainerBase::ConstIterator(other) {} |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 357 ++m_index; | 388 ++m_index; |
| 358 return *this; | 389 return *this; |
| 359 } | 390 } |
| 360 | 391 |
| 361 private: | 392 private: |
| 362 explicit ConstIterator(const ListContainerBase::ConstIterator& baseItera tor) | 393 explicit ConstIterator(const ListContainerBase::ConstIterator& baseItera tor) |
| 363 : ListContainerBase::ConstIterator(baseIterator) {} | 394 : ListContainerBase::ConstIterator(baseIterator) {} |
| 364 friend ConstIterator ListContainer<BaseElementType>::cbegin() const; | 395 friend ConstIterator ListContainer<BaseElementType>::cbegin() const; |
| 365 friend ConstIterator ListContainer<BaseElementType>::cend() const; | 396 friend ConstIterator ListContainer<BaseElementType>::cend() const; |
| 366 friend const BaseElementType* ListContainer<BaseElementType>::elementAt( size_t index) const; | 397 friend const BaseElementType* ListContainer<BaseElementType>::elementAt( size_t index) const; |
| 398 friend ConstIterator ListContainer<BaseElementType>::iteratorAt(size_t i ndex) const; | |
| 367 }; | 399 }; |
| 368 | 400 |
| 369 class ReverseIterator : public ListContainerBase::ReverseIterator { | 401 class ReverseIterator : public ListContainerBase::ReverseIterator { |
| 370 // This class is only defined to reverse iterate through | 402 // This class is only defined to reverse iterate through |
| 371 // ListContainerCharAllocator. | 403 // ListContainerCharAllocator. |
| 372 public: | 404 public: |
| 373 ReverseIterator(ListContainerCharAllocator* container, size_t vectorInde x, char* itemIter, size_t index) | 405 ReverseIterator(ListContainerCharAllocator* container, size_t vectorInde x, char* itemIter, size_t index) |
| 374 : ListContainerBase::ReverseIterator(container, vectorIndex, itemIte r, index) {} | 406 : ListContainerBase::ReverseIterator(container, vectorIndex, itemIte r, index) {} |
| 375 | 407 |
| 376 BaseElementType* operator->() const | 408 BaseElementType* operator->() const |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 explicit ConstReverseIterator(ListContainerBase::ConstReverseIterator ba seIterator) | 467 explicit ConstReverseIterator(ListContainerBase::ConstReverseIterator ba seIterator) |
| 436 : ListContainerBase::ConstReverseIterator(baseIterator) {} | 468 : ListContainerBase::ConstReverseIterator(baseIterator) {} |
| 437 friend ConstReverseIterator ListContainer<BaseElementType>::crbegin() co nst; | 469 friend ConstReverseIterator ListContainer<BaseElementType>::crbegin() co nst; |
| 438 friend ConstReverseIterator ListContainer<BaseElementType>::crend() cons t; | 470 friend ConstReverseIterator ListContainer<BaseElementType>::crend() cons t; |
| 439 }; | 471 }; |
| 440 }; | 472 }; |
| 441 | 473 |
| 442 } // namespace blink | 474 } // namespace blink |
| 443 | 475 |
| 444 #endif // ListContainer_h | 476 #endif // ListContainer_h |
| OLD | NEW |