src/DataBuffer.cc

changeset 96
3384d7aa036a
parent 94
8915ee6a277d
child 108
6409ece8297c
--- a/src/DataBuffer.cc	Tue Feb 04 13:38:22 2014 +0200
+++ b/src/DataBuffer.cc	Tue Feb 04 14:21:06 2014 +0200
@@ -52,30 +52,23 @@
 	if (!other)
 		return;
 
-	int oldsize = GetWrittenSize();
+	// Note: We transfer the marks before the buffer is copied, so that the
+	// offset uses the proper value (which is the written size of @other, which
+	// we don't want our written size to be added to yet).
+	other->TransferMarks (this);
 	CopyBuffer (other);
-
-	// Assimilate in its marks and references
-	for (ByteMark* mark : other->GetMarks())
-	{
-		mark->pos += oldsize;
-		PushToMarks (mark);
-	}
-
-	for (MarkReference* ref : other->GetReferences())
-	{
-		ref->pos += oldsize;
-		PushToReferences (ref);
-	}
-
 	delete other;
 }
 
 // ============================================================================
 //
+// Clones this databuffer to a new one and returns it. Note that the original
+// transfers its marks and references and loses them in the process.
+//
 DataBuffer* DataBuffer::Clone()
 {
 	DataBuffer* other = new DataBuffer;
+	TransferMarks (other);
 	other->CopyBuffer (this);
 	return other;
 }
@@ -91,6 +84,28 @@
 
 // ============================================================================
 //
+void DataBuffer::TransferMarks (DataBuffer* other)
+{
+	int offset = other->GetWrittenSize();
+
+	for (ByteMark* mark : GetMarks())
+	{
+		mark->pos += offset;
+		other->PushToMarks (mark);
+	}
+
+	for (MarkReference* ref : GetReferences())
+	{
+		ref->pos += offset;
+		other->PushToReferences (ref);
+	}
+
+	ClearMarks();
+	ClearReferences();
+}
+
+// ============================================================================
+//
 ByteMark* DataBuffer::AddMark (String name)
 {
 	ByteMark* mark = new ByteMark;

mercurial