Skip to content

Commit

Permalink
Initialize each EbmlElement with its EbmlCallbacks
Browse files Browse the repository at this point in the history
So we have access to the semantic constraints in the constructor.
  • Loading branch information
robUx4 committed Dec 23, 2023
1 parent 9463f48 commit 8494914
Show file tree
Hide file tree
Showing 21 changed files with 56 additions and 49 deletions.
2 changes: 1 addition & 1 deletion ebml/EbmlBinary.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ebml/EbmlDate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ebml/EbmlDummy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;}
Expand Down
15 changes: 9 additions & 6 deletions ebml/EbmlElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
Expand All @@ -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); \
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions ebml/EbmlFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion ebml/EbmlMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;}
Expand Down
4 changes: 2 additions & 2 deletions ebml/EbmlSInteger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;}

Expand Down
4 changes: 2 additions & 2 deletions ebml/EbmlString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions ebml/EbmlUInteger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;}

Expand Down
4 changes: 2 additions & 2 deletions ebml/EbmlUnicodeString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/EbmlBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/EbmlCrc32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ constexpr std::array<std::uint32_t, 256> EbmlCrc32::m_tab {
#endif
};

EbmlCrc32::EbmlCrc32() {
EbmlCrc32::EbmlCrc32()
: EbmlBinary(EbmlCrc32::ClassInfos)
{
ResetCRC();
SetDefaultSize(4);
SetSize_(4);
Expand Down
5 changes: 3 additions & 2 deletions src/EbmlElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/EbmlFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/EbmlHead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/EbmlMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/EbmlSInteger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 4 additions & 4 deletions src/EbmlString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace libebml {

EbmlString::EbmlString()
:EbmlElement(0, false)
EbmlString::EbmlString(const EbmlCallbacks & classInfo)
:EbmlElement(classInfo, 0, false)
{
SetDefaultSize(0);
/* done automatically
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/EbmlUInteger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 4 additions & 4 deletions src/EbmlUnicodeString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/EbmlVoid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace libebml {
DEFINE_EBML_CLASS_GLOBAL(EbmlVoid, 0xEC, 1, "EBMLVoid")

EbmlVoid::EbmlVoid()
:EbmlBinary(EbmlVoid::ClassInfos)
{
SetValueIsSet();
}
Expand Down

0 comments on commit 8494914

Please sign in to comment.