[Matroska-devel] copying/cloning/moving ebml elements

Steve Lhomme steve.lhomme at free.fr
Sat Jul 19 12:19:21 CEST 2003

Moritz Bunkus wrote:
> Heya Steve,
> for proper tag support I need some infrastructure regarding the Ebml*
> thingies. Let's take the simple example that I read two Matroska files
> and that both contain tags. Now I want to write the merged tags to the
> new file, but I don't want two KaxTags but two KaxTag under a single
> KaxTags.
> So what I need is:
> 1. I find the KaxTags in both files and Read() them. Now I have the tags
>    in memory in two variables, e.g. tags1 and tags2.
> 2. I create a destination KaxTags object, d_tags.
> 3. I copy all KaxTag children from tags1 into d_tags. Same for tags2.

No need, you can just push the 2 KaxTag you have (tags1[0] and tags2[0]) 
in a new KaxTags.

> 4. I Render() d_tags into the output file.
> This is the case of copying subtrees around. I also need to be able to
> move a subtree from one EbmlMaster to another, e.g. move a KaxTag child
> from tags1 to d_tags. The second case is different in that when I delete
> tags1 and d_tags the subtree should of course only be deleted when I
> delete the d_tags.
> The 'moving' case seems to to be possible already if I understand
> EbmlMaster.h correctly: The Remove() function can remove the KaxTag
> child from the tags1 parent, and I can use PushElement to add the KaxTag
> child to its new parent, d_tags. Is that correct?

Yes (I should have read until here).

> But for copying we need proper copy ctors. I can easily write them for
> EbmlString, EbmlUnicodeString, EbmlBinary, but not for EbmlMaster (at
> least not easily). That's where I ask for your help ;)

Mmmm, I already thought of that. What happens for an EbmlBinary ? We 
copy the binary data or the reference, and if so who is allowed to free it ?

The thing that is easier to implement is allow to remove an element from 
an EbmlMaster without freeing it. That's exactly what Remove() does. So 
you can take an EbmlElement from a Master and move it to another Master. 
You are responsible to free it if it's not contained in a Master. Also 
if the same element is in 2 masters, it will be freed twice. Not good. 
So be careful with that.

More information about the Matroska-devel mailing list