Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Unified Diff: cc/base/list_container.h

Issue 1202153002: Add ListContainer::AppendByMoving (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address reviewer comments Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | cc/base/list_container.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/base/list_container.h
diff --git a/cc/base/list_container.h b/cc/base/list_container.h
index 0c3772b82fdb9acbf27e731f42679025835f29ac..5463ca2066873bef445acb6a66bcbf084db2bb37 100644
--- a/cc/base/list_container.h
+++ b/cc/base/list_container.h
@@ -161,6 +161,8 @@ class CC_EXPORT ListContainerBase {
size_t size() const;
bool empty() const;
+ size_t MaxSizeForDerivedClass() const;
+
// Unlike the ListContainer method, this one does not invoke element
// destructors.
void clear();
@@ -270,6 +272,7 @@ class ListContainer : public ListContainerBase {
DerivedElementType* AllocateAndConstruct() {
return new (Allocate(sizeof(DerivedElementType))) DerivedElementType;
}
+
// Take in derived element type and copy construct it at location generated by
// Allocate().
template <typename DerivedElementType>
@@ -277,6 +280,7 @@ class ListContainer : public ListContainerBase {
return new (Allocate(sizeof(DerivedElementType)))
DerivedElementType(*source);
}
+
// Construct a new element on top of an existing one.
template <typename DerivedElementType>
DerivedElementType* ReplaceExistingElement(Iterator at) {
@@ -284,6 +288,19 @@ class ListContainer : public ListContainerBase {
return new (*at) DerivedElementType();
}
+ // Appends a new item without copying. The original item will not be
+ // destructed and will be replaced with a new DerivedElementType. The
+ // DerivedElementType does not have to match the moved type as a full block
+ // of memory will be moved (up to MaxSizeForDerivedClass()).
+ template <typename DerivedElementType>
+ void AppendByMoving(DerivedElementType* item) {
+ size_t max_size_for_derived_class = MaxSizeForDerivedClass();
+ void* new_item = Allocate(max_size_for_derived_class);
+ memcpy(new_item, static_cast<void*>(item), max_size_for_derived_class);
+ // Construct a new element in-place so it can be destructed safely.
+ new (item) DerivedElementType;
+ }
+
using ListContainerBase::size;
using ListContainerBase::empty;
« no previous file with comments | « no previous file | cc/base/list_container.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698