1 /****************************************************************************
2 ** libebml : parse EBML files, see http://embl.sourceforge.net/
4 ** <file/class description>
6 ** Copyright (C) 2002-2005 Steve Lhomme. All rights reserved.
8 ** This file is part of libebml.
10 ** This library is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU Lesser General Public
12 ** License as published by the Free Software Foundation; either
13 ** version 2.1 of the License, or (at your option) any later version.
15 ** This library is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** Lesser General Public License for more details.
20 ** You should have received a copy of the GNU Lesser General Public
21 ** License along with this library; if not, write to the Free Software
22 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
26 ** Contact license@matroska.org if any conditions of this licensing are
29 **********************************************************************/
33 \version \$Id: EbmlMaster.h 1232 2005-10-15 15:56:52Z robux4 $
34 \author Steve Lhomme <robux4 @ users.sf.net>
36 #ifndef LIBEBML_MASTER_H
37 #define LIBEBML_MASTER_H
42 #include "EbmlTypes.h"
43 #include "EbmlElement.h"
44 #include "EbmlCrc32.h"
46 START_LIBEBML_NAMESPACE
48 const bool bChecksumUsedByDefault
= false;
52 \brief Handle all operations on an EBML element that contains other EBML elements
54 class EBML_DLL_API EbmlMaster
: public EbmlElement
{
56 EbmlMaster(const EbmlSemanticContext
& aContext
, bool bSizeIsKnown
= true);
57 EbmlMaster(const EbmlMaster
& ElementToClone
);
58 bool ValidateSize() const {return true;}
60 \warning be carefull to clear the memory allocated in the ElementList elsewhere
62 virtual ~EbmlMaster();
64 uint32
RenderData(IOCallback
& output
, bool bForceRender
, bool bKeepIntact
= false);
65 uint64
ReadData(IOCallback
& input
, ScopeMode ReadFully
);
66 uint64
UpdateSize(bool bKeepIntact
= false, bool bForceRender
= false);
69 \brief Set whether the size is finite (size is known in advance when writing, or infinite size is not known on writing)
71 bool SetSizeInfinite(bool aIsInfinite
= true) {bSizeIsFinite
= !aIsInfinite
; return true;}
73 bool PushElement(EbmlElement
& element
);
74 uint64
GetSize() const {
81 uint64
GetDataStart() const {
82 return ElementPosition
+ EbmlId(*this).Length
+ CodedSizeLength(Size
, SizeLength
, bSizeIsFinite
);
86 \brief find the element corresponding to the ID of the element, NULL if not found
88 EbmlElement
*FindElt(const EbmlCallbacks
& Callbacks
) const;
90 \brief find the first element corresponding to the ID of the element
92 EbmlElement
*FindFirstElt(const EbmlCallbacks
& Callbacks
, const bool bCreateIfNull
);
93 EbmlElement
*FindFirstElt(const EbmlCallbacks
& Callbacks
) const;
96 \brief find the element of the same type of PasElt following in the list of elements
98 EbmlElement
*FindNextElt(const EbmlElement
& PastElt
, const bool bCreateIfNull
);
99 EbmlElement
*FindNextElt(const EbmlElement
& PastElt
) const;
100 EbmlElement
*AddNewElt(const EbmlCallbacks
& Callbacks
);
103 \brief add an element at a specified location
105 bool InsertElement(EbmlElement
& element
, size_t position
= 0);
106 bool InsertElement(EbmlElement
& element
, const EbmlElement
& before
);
109 \brief Read the data and keep the known children
111 void Read(EbmlStream
& inDataStream
, const EbmlSemanticContext
& Context
, int & UpperEltFound
, EbmlElement
* & FoundElt
, bool AllowDummyElt
, ScopeMode ReadFully
= SCOPE_ALL_DATA
);
114 \brief sort Data when they can
118 size_t ListSize() const {return ElementList
.size();}
120 EbmlElement
* operator[](unsigned int position
) {return ElementList
[position
];}
121 const EbmlElement
* operator[](unsigned int position
) const {return ElementList
[position
];}
123 bool IsDefaultValue() const {
124 return (ElementList
.size() == 0);
126 virtual bool IsMaster() const {return true;}
129 \brief verify that all mandatory elements are present
130 \note usefull after reading or before writing
132 bool CheckMandatory() const;
135 \brief Remove an element from the list of the master
137 void Remove(size_t Index
);
140 \brief remove all elements, even the mandatory ones
142 void RemoveAll() {ElementList
.clear();}
145 \brief facility for Master elements to write only the head and force the size later
148 uint32
WriteHead(IOCallback
& output
, int SizeLength
, bool bKeepIntact
= false);
150 void EnableChecksum(bool bIsEnabled
= true) { bChecksumUsed
= bIsEnabled
; }
151 bool HasChecksum() const {return bChecksumUsed
;}
152 bool VerifyChecksum() const;
153 uint32
GetCrc32() const {return Checksum
.GetCrc32();}
154 void ForceChecksum(uint32 NewChecksum
) {
155 Checksum
.ForceCrc32(NewChecksum
);
156 bChecksumUsed
= true;
160 \brief drill down all sub-elements, finding any missing elements
162 std::vector
<std::string
> FindAllMissingElements();
165 std::vector
<EbmlElement
*> ElementList
;
167 const EbmlSemanticContext
& Context
;
174 \brief Add all the mandatory elements to the list
176 bool ProcessMandatory();
179 ///< \todo add a restriction to only elements legal in the context
180 template <typename Type
>
181 Type
& GetChild(EbmlMaster
& Master
)
183 return *(static_cast<Type
*>(Master
.FindFirstElt(Type::ClassInfos
, true)));
186 // MyDocType = GetChild<EDocType>(TestHead);
188 template <typename Type
>
189 Type
* FindChild(EbmlMaster
& Master
)
191 return static_cast<Type
*>(Master
.FindFirstElt(Type::ClassInfos
, false));
194 template <typename Type
>
195 Type
& GetNextChild(EbmlMaster
& Master
, const Type
& PastElt
)
197 return *(static_cast<Type
*>(Master
.FindNextElt(PastElt
, true)));
200 template <typename Type
>
201 Type
& AddNewChild(EbmlMaster
& Master
)
203 return *(static_cast<Type
*>(Master
.AddNewElt(Type::ClassInfos
)));
206 END_LIBEBML_NAMESPACE
208 #endif // LIBEBML_MASTER_H