src/DataBuffer.cc

changeset 96
3384d7aa036a
parent 94
8915ee6a277d
child 108
6409ece8297c
equal deleted inserted replaced
95:4db95b92c29a 96:3384d7aa036a
50 void DataBuffer::MergeAndDestroy (DataBuffer* other) 50 void DataBuffer::MergeAndDestroy (DataBuffer* other)
51 { 51 {
52 if (!other) 52 if (!other)
53 return; 53 return;
54 54
55 int oldsize = GetWrittenSize(); 55 // Note: We transfer the marks before the buffer is copied, so that the
56 // offset uses the proper value (which is the written size of @other, which
57 // we don't want our written size to be added to yet).
58 other->TransferMarks (this);
56 CopyBuffer (other); 59 CopyBuffer (other);
57
58 // Assimilate in its marks and references
59 for (ByteMark* mark : other->GetMarks())
60 {
61 mark->pos += oldsize;
62 PushToMarks (mark);
63 }
64
65 for (MarkReference* ref : other->GetReferences())
66 {
67 ref->pos += oldsize;
68 PushToReferences (ref);
69 }
70
71 delete other; 60 delete other;
72 } 61 }
73 62
74 // ============================================================================ 63 // ============================================================================
75 // 64 //
65 // Clones this databuffer to a new one and returns it. Note that the original
66 // transfers its marks and references and loses them in the process.
67 //
76 DataBuffer* DataBuffer::Clone() 68 DataBuffer* DataBuffer::Clone()
77 { 69 {
78 DataBuffer* other = new DataBuffer; 70 DataBuffer* other = new DataBuffer;
71 TransferMarks (other);
79 other->CopyBuffer (this); 72 other->CopyBuffer (this);
80 return other; 73 return other;
81 } 74 }
82 75
83 // ============================================================================ 76 // ============================================================================
85 void DataBuffer::CopyBuffer (const DataBuffer* buf) 78 void DataBuffer::CopyBuffer (const DataBuffer* buf)
86 { 79 {
87 CheckSpace (buf->GetWrittenSize()); 80 CheckSpace (buf->GetWrittenSize());
88 memcpy (mPosition, buf->GetBuffer(), buf->GetWrittenSize()); 81 memcpy (mPosition, buf->GetBuffer(), buf->GetWrittenSize());
89 mPosition += buf->GetWrittenSize(); 82 mPosition += buf->GetWrittenSize();
83 }
84
85 // ============================================================================
86 //
87 void DataBuffer::TransferMarks (DataBuffer* other)
88 {
89 int offset = other->GetWrittenSize();
90
91 for (ByteMark* mark : GetMarks())
92 {
93 mark->pos += offset;
94 other->PushToMarks (mark);
95 }
96
97 for (MarkReference* ref : GetReferences())
98 {
99 ref->pos += offset;
100 other->PushToReferences (ref);
101 }
102
103 ClearMarks();
104 ClearReferences();
90 } 105 }
91 106
92 // ============================================================================ 107 // ============================================================================
93 // 108 //
94 ByteMark* DataBuffer::AddMark (String name) 109 ByteMark* DataBuffer::AddMark (String name)

mercurial