From 849491426918ed570cbc8f4e5e63d4e8c46a9ce1 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Tue, 19 Dec 2023 17:17:16 +0100 Subject: [PATCH] Initialize each EbmlElement with its EbmlCallbacks So we have access to the semantic constraints in the constructor. --- ebml/EbmlBinary.h | 2 +- ebml/EbmlDate.h | 2 +- ebml/EbmlDummy.h | 4 ++-- ebml/EbmlElement.h | 15 +++++++++------ ebml/EbmlFloat.h | 4 ++-- ebml/EbmlMaster.h | 2 +- ebml/EbmlSInteger.h | 4 ++-- ebml/EbmlString.h | 4 ++-- ebml/EbmlUInteger.h | 4 ++-- ebml/EbmlUnicodeString.h | 4 ++-- src/EbmlBinary.cpp | 4 ++-- src/EbmlCrc32.cpp | 4 +++- src/EbmlElement.cpp | 5 +++-- src/EbmlFloat.cpp | 8 ++++---- src/EbmlHead.cpp | 2 +- src/EbmlMaster.cpp | 4 ++-- src/EbmlSInteger.cpp | 8 ++++---- src/EbmlString.cpp | 8 ++++---- src/EbmlUInteger.cpp | 8 ++++---- src/EbmlUnicodeString.cpp | 8 ++++---- src/EbmlVoid.cpp | 1 + 21 files changed, 56 insertions(+), 49 deletions(-) diff --git a/ebml/EbmlBinary.h b/ebml/EbmlBinary.h index 74a49a61..3e9ff3c2 100644 --- a/ebml/EbmlBinary.h +++ b/ebml/EbmlBinary.h @@ -25,7 +25,7 @@ namespace libebml { */ class EBML_DLL_API EbmlBinary : public EbmlElement { public: - EbmlBinary(); + EbmlBinary(const EbmlCallbacks &); EbmlBinary(const EbmlBinary & ElementToClone); EbmlBinary& operator=(const EbmlBinary & ElementToClone); ~EbmlBinary() override; diff --git a/ebml/EbmlDate.h b/ebml/EbmlDate.h index efcf02f7..f3c43726 100644 --- a/ebml/EbmlDate.h +++ b/ebml/EbmlDate.h @@ -19,7 +19,7 @@ namespace libebml { */ class EBML_DLL_API EbmlDate : public EbmlElement { public: - EbmlDate() :EbmlElement(8, false) {} + EbmlDate(const EbmlCallbacks & classInfo) :EbmlElement(classInfo, 8, false) {} /*! \brief set the date with a UNIX/C/EPOCH form diff --git a/ebml/EbmlDummy.h b/ebml/EbmlDummy.h index d243ebc0..bcb841a3 100644 --- a/ebml/EbmlDummy.h +++ b/ebml/EbmlDummy.h @@ -14,8 +14,8 @@ namespace libebml { class EBML_DLL_API EbmlDummy : public EbmlBinary { public: - EbmlDummy() :DummyId(DummyRawId) {} - EbmlDummy(const EbmlId & aId) : DummyId(aId) {} + EbmlDummy() : EbmlBinary(EbmlDummy::ClassInfos), DummyId(DummyRawId) {} + EbmlDummy(const EbmlId & aId) : EbmlBinary(EbmlDummy::ClassInfos), DummyId(aId) {} bool IsDummy() const override {return true;} bool IsDefaultValue() const override {return true;} diff --git a/ebml/EbmlElement.h b/ebml/EbmlElement.h index 2ea7dcb7..ca876acb 100644 --- a/ebml/EbmlElement.h +++ b/ebml/EbmlElement.h @@ -58,7 +58,7 @@ extern const EbmlSemanticContext Context_EbmlGlobal; constexpr EbmlId Id_##x (id, idl); \ const EbmlSemanticContext Context_##x = EbmlSemanticContext(countof(ContextList_##x), ContextList_##x, &Context_##parent, global, &EBML_INFO(x)); \ constexpr EbmlCallbacks x::ClassInfos(x::Create, Id_##x, name, Context_##x); \ - x::x() :EbmlMaster(Context_##x) {} + x::x() :EbmlMaster(x::ClassInfos, Context_##x) {} #define DEFINE_xxx_MASTER_CONS(x,id,idl,parent,name,global) \ constexpr EbmlId Id_##x (id, idl); \ @@ -85,25 +85,25 @@ extern const EbmlSemanticContext Context_EbmlGlobal; constexpr EbmlId Id_##x (id, idl); \ const EbmlSemanticContext Context_##x = EbmlSemanticContext(0, nullptr, &Context_##parent, global, &EBML_INFO(x)); \ constexpr EbmlCallbacks x::ClassInfos(x::Create, Id_##x, name, Context_##x); \ - x::x() :EbmlUInteger(defval) {} + x::x() :EbmlUInteger(x::ClassInfos, defval) {} #define DEFINE_xxx_SINTEGER_DEF(x,id,idl,parent,name,global,defval) \ constexpr EbmlId Id_##x (id, idl); \ const EbmlSemanticContext Context_##x = EbmlSemanticContext(0, nullptr, &Context_##parent, global, &EBML_INFO(x)); \ constexpr EbmlCallbacks x::ClassInfos(x::Create, Id_##x, name, Context_##x); \ - x::x() :EbmlSInteger(defval) {} + x::x() :EbmlSInteger(x::ClassInfos, defval) {} #define DEFINE_xxx_STRING_DEF(x,id,idl,parent,name,global,defval) \ constexpr EbmlId Id_##x (id, idl); \ const EbmlSemanticContext Context_##x = EbmlSemanticContext(0, nullptr, &Context_##parent, global, &EBML_INFO(x)); \ constexpr EbmlCallbacks x::ClassInfos(x::Create, Id_##x, name, Context_##x); \ - x::x() :EbmlString(defval) {} + x::x() :EbmlString(x::ClassInfos, defval) {} #define DEFINE_xxx_FLOAT_DEF(x,id,idl,parent,name,global,defval) \ constexpr EbmlId Id_##x (id, idl); \ const EbmlSemanticContext Context_##x = EbmlSemanticContext(0, nullptr, &Context_##parent, global, &EBML_INFO(x)); \ constexpr EbmlCallbacks x::ClassInfos(x::Create, Id_##x, name, Context_##x); \ - x::x() :EbmlFloat(defval) {} + x::x() :EbmlFloat(x::ClassInfos, defval) {} #define DEFINE_xxx_CLASS_GLOBAL(x,id,idl,name,global) \ constexpr EbmlId Id_##x (id, idl); \ @@ -288,7 +288,7 @@ class EBML_DLL_API EbmlSemanticContext { */ class EBML_DLL_API EbmlElement { public: - explicit EbmlElement(std::uint64_t aDefaultSize, bool bValueSet = false); + explicit EbmlElement(const EbmlCallbacks &, std::uint64_t aDefaultSize, bool bValueSet = false); virtual ~EbmlElement() = default; EbmlElement& operator=(const EbmlElement&) = delete; @@ -408,6 +408,9 @@ class EBML_DLL_API EbmlElement { inline void SetSizeIsFinite(bool Set = true) {bSizeIsFinite = Set;} inline std::uint64_t GetSizePosition() const {return SizePosition;} + protected: + const EbmlCallbacks & ClassInfo; + private: std::uint64_t Size; ///< the size of the data to write std::uint64_t DefaultSize; ///< Minimum data size to fill on rendering (0 = optimal) diff --git a/ebml/EbmlFloat.h b/ebml/EbmlFloat.h index 6e3013e0..25451a4c 100644 --- a/ebml/EbmlFloat.h +++ b/ebml/EbmlFloat.h @@ -24,8 +24,8 @@ class EBML_DLL_API EbmlFloat : public EbmlElement { ,FLOAT_64 }; - EbmlFloat(Precision prec = FLOAT_32); - EbmlFloat(double DefaultValue, Precision prec = FLOAT_32); + EbmlFloat(const EbmlCallbacks &, Precision prec = FLOAT_32); + EbmlFloat(const EbmlCallbacks &, double DefaultValue, Precision prec = FLOAT_32); bool ValidateSize() const override { diff --git a/ebml/EbmlMaster.h b/ebml/EbmlMaster.h index 0b29093e..051dba3b 100644 --- a/ebml/EbmlMaster.h +++ b/ebml/EbmlMaster.h @@ -30,7 +30,7 @@ const bool bChecksumUsedByDefault = false; */ class EBML_DLL_API EbmlMaster : public EbmlElement { public: - explicit EbmlMaster(const EbmlSemanticContext & aContext, bool bSizeIsKnown = true); + explicit EbmlMaster(const EbmlCallbacks &, const EbmlSemanticContext & aContext, bool bSizeIsKnown = true); EbmlMaster(const EbmlMaster & ElementToClone); EbmlMaster& operator=(const EbmlMaster&) = delete; bool ValidateSize() const override {return true;} diff --git a/ebml/EbmlSInteger.h b/ebml/EbmlSInteger.h index b3b76ff7..ea258406 100644 --- a/ebml/EbmlSInteger.h +++ b/ebml/EbmlSInteger.h @@ -25,8 +25,8 @@ const int DEFAULT_INT_SIZE = 1; ///< optimal size stored */ class EBML_DLL_API EbmlSInteger : public EbmlElement { public: - EbmlSInteger(); - explicit EbmlSInteger(std::int64_t DefaultValue); + EbmlSInteger(const EbmlCallbacks &); + explicit EbmlSInteger(const EbmlCallbacks &, std::int64_t DefaultValue); EbmlSInteger & operator = (std::int64_t NewValue) {Value = NewValue; SetValueIsSet(); return *this;} diff --git a/ebml/EbmlString.h b/ebml/EbmlString.h index 78c0518e..54b0995b 100644 --- a/ebml/EbmlString.h +++ b/ebml/EbmlString.h @@ -21,8 +21,8 @@ namespace libebml { */ class EBML_DLL_API EbmlString : public EbmlElement { public: - EbmlString(); - explicit EbmlString(const std::string & aDefaultValue); + EbmlString(const EbmlCallbacks &); + explicit EbmlString(const EbmlCallbacks &, const std::string & aDefaultValue); bool ValidateSize() const override {return IsFiniteSize() && GetSize() < 0x7FFFFFFF;} // any size is possible filepos_t RenderData(IOCallback & output, bool bForceRender, bool bWithDefault = false) override; diff --git a/ebml/EbmlUInteger.h b/ebml/EbmlUInteger.h index bdab3da4..a9c5d0b0 100644 --- a/ebml/EbmlUInteger.h +++ b/ebml/EbmlUInteger.h @@ -23,8 +23,8 @@ const int DEFAULT_UINT_SIZE = 0; ///< optimal size stored */ class EBML_DLL_API EbmlUInteger : public EbmlElement { public: - EbmlUInteger(); - explicit EbmlUInteger(std::uint64_t DefaultValue); + EbmlUInteger(const EbmlCallbacks &); + explicit EbmlUInteger(const EbmlCallbacks &, std::uint64_t DefaultValue); EbmlUInteger & operator=(std::uint64_t NewValue) {Value = NewValue; SetValueIsSet(); return *this;} diff --git a/ebml/EbmlUnicodeString.h b/ebml/EbmlUnicodeString.h index 8e601c2d..41c0c606 100644 --- a/ebml/EbmlUnicodeString.h +++ b/ebml/EbmlUnicodeString.h @@ -67,8 +67,8 @@ class EBML_DLL_API UTFstring { */ class EBML_DLL_API EbmlUnicodeString : public EbmlElement { public: - EbmlUnicodeString(); - explicit EbmlUnicodeString(const UTFstring & DefaultValue); + EbmlUnicodeString(const EbmlCallbacks &); + explicit EbmlUnicodeString(const EbmlCallbacks &, const UTFstring & DefaultValue); bool ValidateSize() const override {return IsFiniteSize();} // any size is possible filepos_t RenderData(IOCallback & output, bool bForceRender, bool bWithDefault = false) override; diff --git a/src/EbmlBinary.cpp b/src/EbmlBinary.cpp index 39134330..76272463 100644 --- a/src/EbmlBinary.cpp +++ b/src/EbmlBinary.cpp @@ -15,8 +15,8 @@ namespace libebml { -EbmlBinary::EbmlBinary() - :EbmlElement(0, false) +EbmlBinary::EbmlBinary(const EbmlCallbacks & classInfo) + :EbmlElement(classInfo, 0, false) {} EbmlBinary::EbmlBinary(const EbmlBinary & ElementToClone) diff --git a/src/EbmlCrc32.cpp b/src/EbmlCrc32.cpp index 85808289..848231a6 100644 --- a/src/EbmlCrc32.cpp +++ b/src/EbmlCrc32.cpp @@ -137,7 +137,9 @@ constexpr std::array EbmlCrc32::m_tab { #endif }; -EbmlCrc32::EbmlCrc32() { +EbmlCrc32::EbmlCrc32() + : EbmlBinary(EbmlCrc32::ClassInfos) +{ ResetCRC(); SetDefaultSize(4); SetSize_(4); diff --git a/src/EbmlElement.cpp b/src/EbmlElement.cpp index e457778b..a9b3e99a 100644 --- a/src/EbmlElement.cpp +++ b/src/EbmlElement.cpp @@ -133,8 +133,9 @@ const EbmlSemantic & EbmlSemanticContext::GetSemantic(std::size_t i) const } -EbmlElement::EbmlElement(std::uint64_t aDefaultSize, bool bValueSet) - :DefaultSize(aDefaultSize) +EbmlElement::EbmlElement(const EbmlCallbacks & classInfo, std::uint64_t aDefaultSize, bool bValueSet) + : ClassInfo(classInfo) + , DefaultSize(aDefaultSize) , bValueIsSet(bValueSet) { Size = DefaultSize; diff --git a/src/EbmlFloat.cpp b/src/EbmlFloat.cpp index f8888045..1b3235eb 100644 --- a/src/EbmlFloat.cpp +++ b/src/EbmlFloat.cpp @@ -12,14 +12,14 @@ namespace libebml { -EbmlFloat::EbmlFloat(const EbmlFloat::Precision prec) - :EbmlElement(0, false) +EbmlFloat::EbmlFloat(const EbmlCallbacks & classInfo, const EbmlFloat::Precision prec) + :EbmlElement(classInfo, 0, false) { SetPrecision(prec); } -EbmlFloat::EbmlFloat(const double aDefaultValue, const EbmlFloat::Precision prec) - :EbmlElement(0, true), Value(aDefaultValue), DefaultValue(aDefaultValue) +EbmlFloat::EbmlFloat(const EbmlCallbacks & classInfo, const double aDefaultValue, const EbmlFloat::Precision prec) + :EbmlElement(classInfo, 0, true), Value(aDefaultValue), DefaultValue(aDefaultValue) { SetDefaultIsSet(); SetPrecision(prec); diff --git a/src/EbmlHead.cpp b/src/EbmlHead.cpp index b14be5a5..6f37d85f 100644 --- a/src/EbmlHead.cpp +++ b/src/EbmlHead.cpp @@ -24,7 +24,7 @@ DEFINE_END_SEMANTIC(EbmlHead) DEFINE_EBML_MASTER_ORPHAN(EbmlHead, 0x1A45DFA3, 4, "EBMLHead\0ratamapaga") EbmlHead::EbmlHead() - :EbmlMaster(EbmlHead_Context) + :EbmlMaster(EbmlHead::ClassInfos, EbmlHead_Context) {} } // namespace libebml diff --git a/src/EbmlMaster.cpp b/src/EbmlMaster.cpp index 07974268..54175fd8 100644 --- a/src/EbmlMaster.cpp +++ b/src/EbmlMaster.cpp @@ -17,8 +17,8 @@ namespace libebml { -EbmlMaster::EbmlMaster(const EbmlSemanticContext & aContext, bool bSizeIsknown) - :EbmlElement(0), MasterContext(aContext), bChecksumUsed(bChecksumUsedByDefault) +EbmlMaster::EbmlMaster(const EbmlCallbacks & classInfo, const EbmlSemanticContext & aContext, bool bSizeIsknown) + :EbmlElement(classInfo, 0), MasterContext(aContext), bChecksumUsed(bChecksumUsedByDefault) { SetSizeIsFinite(bSizeIsknown); SetValueIsSet(); diff --git a/src/EbmlSInteger.cpp b/src/EbmlSInteger.cpp index e6757207..24c2fec0 100644 --- a/src/EbmlSInteger.cpp +++ b/src/EbmlSInteger.cpp @@ -31,12 +31,12 @@ ToSigned(std::uint64_t u) { namespace libebml { -EbmlSInteger::EbmlSInteger() - :EbmlElement(DEFAULT_INT_SIZE, false) +EbmlSInteger::EbmlSInteger(const EbmlCallbacks & classInfo) + :EbmlElement(classInfo, DEFAULT_INT_SIZE, false) {} -EbmlSInteger::EbmlSInteger(std::int64_t aDefaultValue) - :EbmlElement(DEFAULT_INT_SIZE, true), Value(aDefaultValue) +EbmlSInteger::EbmlSInteger(const EbmlCallbacks & classInfo, std::int64_t aDefaultValue) + :EbmlElement(classInfo, DEFAULT_INT_SIZE, true), Value(aDefaultValue) { SetDefaultIsSet(); } diff --git a/src/EbmlString.cpp b/src/EbmlString.cpp index e29bf9d4..92761215 100644 --- a/src/EbmlString.cpp +++ b/src/EbmlString.cpp @@ -12,8 +12,8 @@ namespace libebml { -EbmlString::EbmlString() - :EbmlElement(0, false) +EbmlString::EbmlString(const EbmlCallbacks & classInfo) + :EbmlElement(classInfo, 0, false) { SetDefaultSize(0); /* done automatically @@ -22,8 +22,8 @@ EbmlString::EbmlString() SetSize_(GetDefaultSize());*/ } -EbmlString::EbmlString(const std::string & aDefaultValue) - :EbmlElement(0, true), Value(aDefaultValue), DefaultValue(aDefaultValue) +EbmlString::EbmlString(const EbmlCallbacks & classInfo, const std::string & aDefaultValue) + :EbmlElement(classInfo, 0, true), Value(aDefaultValue), DefaultValue(aDefaultValue) { SetDefaultSize(0); SetDefaultIsSet(); diff --git a/src/EbmlUInteger.cpp b/src/EbmlUInteger.cpp index 1d5ad951..9d4edd73 100644 --- a/src/EbmlUInteger.cpp +++ b/src/EbmlUInteger.cpp @@ -13,12 +13,12 @@ namespace libebml { -EbmlUInteger::EbmlUInteger() - :EbmlElement(DEFAULT_UINT_SIZE, false) +EbmlUInteger::EbmlUInteger(const EbmlCallbacks & classInfo) + :EbmlElement(classInfo, DEFAULT_UINT_SIZE, false) {} -EbmlUInteger::EbmlUInteger(std::uint64_t aDefaultValue) - :EbmlElement(DEFAULT_UINT_SIZE, true), Value(aDefaultValue), DefaultValue(aDefaultValue) +EbmlUInteger::EbmlUInteger(const EbmlCallbacks & classInfo, std::uint64_t aDefaultValue) + :EbmlElement(classInfo, DEFAULT_UINT_SIZE, true), Value(aDefaultValue), DefaultValue(aDefaultValue) { SetDefaultIsSet(); } diff --git a/src/EbmlUnicodeString.cpp b/src/EbmlUnicodeString.cpp index c3c3461c..00b8f50d 100644 --- a/src/EbmlUnicodeString.cpp +++ b/src/EbmlUnicodeString.cpp @@ -161,14 +161,14 @@ bool UTFstring::wcscmp_internal(const wchar_t *str1, const wchar_t *str2) // ===================== EbmlUnicodeString class =================== -EbmlUnicodeString::EbmlUnicodeString() - :EbmlElement(0, false) +EbmlUnicodeString::EbmlUnicodeString(const EbmlCallbacks & classInfo) + :EbmlElement(classInfo, 0, false) { SetDefaultSize(0); } -EbmlUnicodeString::EbmlUnicodeString(const UTFstring & aDefaultValue) - :EbmlElement(0, true), Value(aDefaultValue), DefaultValue(aDefaultValue) +EbmlUnicodeString::EbmlUnicodeString(const EbmlCallbacks & classInfo, const UTFstring & aDefaultValue) + :EbmlElement(classInfo, 0, true), Value(aDefaultValue), DefaultValue(aDefaultValue) { SetDefaultSize(0); SetDefaultIsSet(); diff --git a/src/EbmlVoid.cpp b/src/EbmlVoid.cpp index e60dc289..54dded9f 100644 --- a/src/EbmlVoid.cpp +++ b/src/EbmlVoid.cpp @@ -13,6 +13,7 @@ namespace libebml { DEFINE_EBML_CLASS_GLOBAL(EbmlVoid, 0xEC, 1, "EBMLVoid") EbmlVoid::EbmlVoid() + :EbmlBinary(EbmlVoid::ClassInfos) { SetValueIsSet(); }