Skip to content

Commit

Permalink
Merge remote-tracking branch 'robux4/typed_children3'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbunkus committed Mar 2, 2024
2 parents d4808b9 + 0076f76 commit a3fd627
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 42 deletions.
15 changes: 7 additions & 8 deletions ebml/EbmlMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ class EBML_DLL_API EbmlMaster : public EbmlElement {
}

/*!
\brief find the element corresponding to the ID of the element, NULL if not found
\brief find the first element corresponding to the EBML class of the element, NULL if not found
*/
EbmlElement *FindElt(const EbmlCallbacks & Callbacks) const;
EbmlElement *FindFirstElt(const EbmlCallbacks & Callbacks) const;
/*!
\brief find the first element corresponding to the ID of the element
\brief find the first element corresponding to the EBML class of the element
*/
EbmlElement *FindFirstElt(const EbmlCallbacks & Callbacks, bool bCreateIfNull);
EbmlElement *FindFirstElt(const EbmlCallbacks & Callbacks) const;

/*!
\brief find the element of the same type of PasElt following in the list of elements
Expand Down Expand Up @@ -157,9 +156,9 @@ Type & GetChild(EbmlMaster & Master)
// MyDocType = GetChild<EDocType>(TestHead);

template <typename Type>
Type * FindChild(EbmlMaster & Master)
Type * FindChild(const EbmlMaster & Master)
{
return static_cast<Type *>(Master.FindFirstElt(EBML_INFO(Type), false));
return static_cast<Type *>(Master.FindFirstElt(EBML_INFO(Type)));
}

template <typename Type>
Expand All @@ -169,9 +168,9 @@ Type & GetNextChild(EbmlMaster & Master, const Type & PastElt)
}

template <typename Type>
Type * FindNextChild(EbmlMaster & Master, const Type & PastElt)
Type * FindNextChild(const EbmlMaster & Master, const Type & PastElt)
{
return static_cast<Type *>(Master.FindNextElt(PastElt, false));
return static_cast<Type *>(Master.FindNextElt(PastElt));
}

template <typename Type>
Expand Down
6 changes: 6 additions & 0 deletions ebml/EbmlStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class EBML_DLL_API EbmlStream {
*/
EbmlElement * FindNextID(const EbmlCallbacks & ClassInfos, std::uint64_t MaxDataSize) const;

template <typename Type>
Type * FindNextID(std::uint64_t MaxDataSize)
{
return static_cast<Type *>(FindNextID(EBML_INFO(Type), MaxDataSize));
}

EbmlElement * FindNextElement(const EbmlSemanticContext & Context, int & UpperLevel, std::uint64_t MaxDataSize, bool AllowDummyElt, unsigned int MaxLowerLevel = 1) const;

inline IOCallback & I_O() {return Stream;}
Expand Down
41 changes: 8 additions & 33 deletions src/EbmlMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ bool EbmlMaster::CheckMandatory() const
for (unsigned int EltIdx = 0; EltIdx < EBML_CTX_SIZE(MasterContext); EltIdx++) {
if (EBML_CTX_IDX(MasterContext,EltIdx).IsMandatory()) {
const auto & semcb = EBML_CTX_IDX_INFO(MasterContext,EltIdx);
if (FindElt(semcb) == nullptr) {
if (FindFirstElt(semcb) == nullptr) {
const bool hasDefaultValue = semcb.HasDefault();

#if !defined(NDEBUG)
Expand All @@ -200,7 +200,7 @@ bool EbmlMaster::CheckMandatory() const
return true;
}

EbmlElement *EbmlMaster::FindElt(const EbmlCallbacks & Callbacks) const
EbmlElement *EbmlMaster::FindFirstElt(const EbmlCallbacks & Callbacks) const
{
auto it = std::find_if(ElementList.begin(), ElementList.end(), [&](const EbmlElement *Element)
{ return EbmlId(*Element) == EBML_INFO_ID(Callbacks); });
Expand All @@ -210,56 +210,31 @@ EbmlElement *EbmlMaster::FindElt(const EbmlCallbacks & Callbacks) const

EbmlElement *EbmlMaster::FindFirstElt(const EbmlCallbacks & Callbacks, bool bCreateIfNull)
{
auto e = FindElt(Callbacks);
auto e = FindFirstElt(Callbacks);
if (e)
return e;

if (bCreateIfNull) {
// add the element
EbmlElement *NewElt = &EBML_INFO_CREATE(Callbacks);
if (NewElt == nullptr)
return nullptr;

if (!PushElement(*NewElt)) {
delete NewElt;
NewElt = nullptr;
}
return NewElt;
return AddNewElt(Callbacks);
}

return nullptr;
}

EbmlElement *EbmlMaster::FindFirstElt(const EbmlCallbacks & Callbacks) const
{
return FindElt(Callbacks);
}

/*!
\todo only return elements that are from the same type !
\todo the element might be the unique in the context !
*/
EbmlElement *EbmlMaster::FindNextElt(const EbmlElement & PastElt, bool bCreateIfNull)
{
auto it = std::find(ElementList.begin(), ElementList.end(), &PastElt);
if (it != ElementList.end()) {
it = std::find_if(it + 1, ElementList.end(), [&](auto &&element) {
return EbmlId(PastElt) == EbmlId(*element);
});

if (it != ElementList.end())
return *it;
}
EbmlElement *e = FindNextElt(PastElt);
if (e)
return e;

if (bCreateIfNull) {
// add the element
EbmlElement *NewElt = &(PastElt.CreateElement());

if (!PushElement(*NewElt)) {
delete NewElt;
NewElt = nullptr;
}
return NewElt;
return AddNewElt(PastElt.ElementSpec());
}

return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion test/test_missing.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void FindAllMissingElements(const EbmlMaster *pThis, std::vector<std::str
unsigned int EltIdx;
for (EltIdx = 0; EltIdx < EBML_CTX_SIZE(MasterContext); EltIdx++) {
if (EBML_CTX_IDX(MasterContext,EltIdx).IsMandatory()) {
if (pThis->FindElt(EBML_CTX_IDX_INFO(MasterContext,EltIdx)) == nullptr) {
if (pThis->FindFirstElt(EBML_CTX_IDX_INFO(MasterContext,EltIdx)) == nullptr) {
std::string missingElement;
missingElement = "Missing element \"";
missingElement.append(EBML_INFO_NAME(EBML_CTX_IDX_INFO(MasterContext,EltIdx)));
Expand Down

0 comments on commit a3fd627

Please sign in to comment.