From 56a6d75c7f27faccb3d8bd36a0db05b51b9e9d2e Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Tue, 27 Feb 2024 15:59:40 +0100 Subject: [PATCH 1/6] reuse AddNewElt() in FindFirstElt() --- src/EbmlMaster.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/EbmlMaster.cpp b/src/EbmlMaster.cpp index 31132842..23617a1a 100644 --- a/src/EbmlMaster.cpp +++ b/src/EbmlMaster.cpp @@ -216,15 +216,7 @@ EbmlElement *EbmlMaster::FindFirstElt(const EbmlCallbacks & Callbacks, bool bCre 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; From 7e457c9c9e98efadb7b990bb5a2d3eedac7c76dc Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 1 Mar 2024 10:31:37 +0100 Subject: [PATCH 2/6] reuse AddNewElt() in FindNextElt() --- src/EbmlMaster.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/EbmlMaster.cpp b/src/EbmlMaster.cpp index 23617a1a..9e497cde 100644 --- a/src/EbmlMaster.cpp +++ b/src/EbmlMaster.cpp @@ -245,13 +245,7 @@ EbmlElement *EbmlMaster::FindNextElt(const EbmlElement & PastElt, bool bCreateIf if (bCreateIfNull) { // add the element - EbmlElement *NewElt = &(PastElt.CreateElement()); - - if (!PushElement(*NewElt)) { - delete NewElt; - NewElt = nullptr; - } - return NewElt; + return AddNewElt(PastElt.ElementSpec()); } return nullptr; From 22afd2a75fbb9d566a814ec90a69cdf3c189e46d Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 1 Mar 2024 10:33:03 +0100 Subject: [PATCH 3/6] use the base FindNextElt in FindNextElt with optional creation This is exactly the same code. --- src/EbmlMaster.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/EbmlMaster.cpp b/src/EbmlMaster.cpp index 9e497cde..f8ef2529 100644 --- a/src/EbmlMaster.cpp +++ b/src/EbmlMaster.cpp @@ -233,15 +233,9 @@ EbmlElement *EbmlMaster::FindFirstElt(const EbmlCallbacks & Callbacks) const */ 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 From 71088d11008cae5cca181f53950f8df9005690e0 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 1 Mar 2024 10:39:32 +0100 Subject: [PATCH 4/6] remove EbmlMaster::FindElt It's a duplicate of EbmlMaster::FindFirstElt which is more often used. Only VLC uses it and it can be easily switched for all libebml versions. --- ebml/EbmlMaster.h | 7 +++---- src/EbmlMaster.cpp | 11 +++-------- test/test_missing.cxx | 2 +- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/ebml/EbmlMaster.h b/ebml/EbmlMaster.h index f6509646..be424c5e 100644 --- a/ebml/EbmlMaster.h +++ b/ebml/EbmlMaster.h @@ -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 diff --git a/src/EbmlMaster.cpp b/src/EbmlMaster.cpp index f8ef2529..8faf2218 100644 --- a/src/EbmlMaster.cpp +++ b/src/EbmlMaster.cpp @@ -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) @@ -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); }); @@ -210,7 +210,7 @@ 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; @@ -222,11 +222,6 @@ EbmlElement *EbmlMaster::FindFirstElt(const EbmlCallbacks & Callbacks, bool bCre 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 ! diff --git a/test/test_missing.cxx b/test/test_missing.cxx index 716b4d93..74fd2378 100644 --- a/test/test_missing.cxx +++ b/test/test_missing.cxx @@ -31,7 +31,7 @@ static void FindAllMissingElements(const EbmlMaster *pThis, std::vectorFindElt(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))); From 27b839f7cbc0e128e310ed2814a930f0b6e6f49e Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 1 Mar 2024 11:19:33 +0100 Subject: [PATCH 5/6] use the const form of FindFirstElt/FindNextElt in helpers If we don't want to create the element, we can use a const EbmlMaster --- ebml/EbmlMaster.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ebml/EbmlMaster.h b/ebml/EbmlMaster.h index be424c5e..b221b0f4 100644 --- a/ebml/EbmlMaster.h +++ b/ebml/EbmlMaster.h @@ -156,9 +156,9 @@ Type & GetChild(EbmlMaster & Master) // MyDocType = GetChild(TestHead); template -Type * FindChild(EbmlMaster & Master) +Type * FindChild(const EbmlMaster & Master) { - return static_cast(Master.FindFirstElt(EBML_INFO(Type), false)); + return static_cast(Master.FindFirstElt(EBML_INFO(Type))); } template @@ -168,9 +168,9 @@ Type & GetNextChild(EbmlMaster & Master, const Type & PastElt) } template -Type * FindNextChild(EbmlMaster & Master, const Type & PastElt) +Type * FindNextChild(const EbmlMaster & Master, const Type & PastElt) { - return static_cast(Master.FindNextElt(PastElt, false)); + return static_cast(Master.FindNextElt(PastElt)); } template From 131905c457b2b0b6ee0f9526185748e0eb7874f0 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Sat, 2 Mar 2024 07:52:50 +0100 Subject: [PATCH 6/6] provide a stronger typed version of EbmlStream::FindNextID() --- ebml/EbmlStream.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ebml/EbmlStream.h b/ebml/EbmlStream.h index 21f20e68..011050c1 100644 --- a/ebml/EbmlStream.h +++ b/ebml/EbmlStream.h @@ -27,6 +27,12 @@ class EBML_DLL_API EbmlStream { */ EbmlElement * FindNextID(const EbmlCallbacks & ClassInfos, std::uint64_t MaxDataSize) const; + template + Type * FindNextID(std::uint64_t MaxDataSize) + { + return static_cast(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;}