Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use templates to factorize generic code #200

Merged
merged 11 commits into from
Jan 1, 2024
14 changes: 3 additions & 11 deletions ebml/EbmlDate.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace libebml {
\class EbmlDate
\brief Handle all operations related to an EBML date
*/
class EBML_DLL_API EbmlDate : public EbmlElementDefault<std::int64_t> {
class EBML_DLL_API EbmlDate : public EbmlElementDefaultSameStorage<std::int64_t> {
public:
EbmlDate(const EbmlCallbacksDefault<std::int64_t> & classInfo) :EbmlElementDefault<std::int64_t>(classInfo, 8)
EbmlDate(const EbmlCallbacksDefault<std::int64_t> & classInfo) :EbmlElementDefaultSameStorage<std::int64_t>(classInfo, 8)
{
if (classInfo.HasDefault())
{
Expand All @@ -33,7 +33,7 @@ class EBML_DLL_API EbmlDate : public EbmlElementDefault<std::int64_t> {
\param NewDate UNIX/C date in UTC (no timezone)
*/
void SetEpochDate(std::int64_t NewDate) {Value = (NewDate - UnixEpochDelay) * 1'000'000'000; SetValueIsSet();}
EbmlDate &SetValue(std::int64_t NewValue) {SetEpochDate(NewValue); return *this;}
EbmlElementDefaultSameStorage<std::int64_t> &SetValue(const std::int64_t & NewValue) override {SetEpochDate(NewValue); return *this;}

/*!
\brief get the date with a UNIX/C/EPOCH form
Expand All @@ -55,19 +55,11 @@ class EBML_DLL_API EbmlDate : public EbmlElementDefault<std::int64_t> {
return GetSize();
}

bool IsSmallerThan(const EbmlElement *Cmp) const override;

filepos_t ReadData(IOCallback & input, ScopeMode ReadFully = SCOPE_ALL_DATA) override;

bool operator==(const std::int64_t & val) const override {
return val == Value;
}

private:
filepos_t RenderData(IOCallback & output, bool bForceRender, ShouldWrite writeFilter = WriteSkipDefault) override;

std::int64_t Value{0}; ///< internal format of the date

static constexpr std::uint64_t UnixEpochDelay = 978'307'200; // 2001/01/01 00:00:00 UTC
};

Expand Down
73 changes: 73 additions & 0 deletions ebml/EbmlElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,79 @@ class EBML_DLL_API EbmlElementDefault : public EbmlElement {
}

virtual bool operator==(const T &) const = 0;
virtual EbmlElementDefault<T> & SetValue(const T &) = 0;
};


template<typename T>
class EBML_DLL_API EbmlElementDefaultSameStorage : public EbmlElementDefault<T> {
public:
explicit EbmlElementDefaultSameStorage(const EbmlCallbacksDefault<T> &classInfo, std::uint64_t aDefaultSize)
:EbmlElementDefault<T>(classInfo, aDefaultSize)
{}

EbmlElementDefaultSameStorage<T> & SetValue(const T & NewValue) override
{
Value = NewValue;
EbmlElement::SetValueIsSet();
return *this;
}

bool operator==(const T & test) const override
{
return Value == test;
}

T GetValue() const
{
return Value;
}

bool IsSmallerThan(const EbmlElement *Cmp) const override
{
if (EbmlId(*this) != EbmlId(*Cmp))
return false;

return this->Value < static_cast<const EbmlElementDefaultSameStorage<T> *>(Cmp)->Value;
}

explicit operator T() const { return Value; }

protected:
T Value;
};


template<typename T, typename S>
class EBML_DLL_API EbmlElementDefaultStorage : public EbmlElementDefault<T> {
public:
explicit EbmlElementDefaultStorage(const EbmlCallbacksDefault<T> &classInfo, std::uint64_t aDefaultSize)
:EbmlElementDefault<T>(classInfo, aDefaultSize)
{}

EbmlElementDefaultStorage<T,S> & SetValue(const T & NewValue) override
{
Value = static_cast<S>(NewValue);
EbmlElement::SetValueIsSet();
return *this;
}

EbmlElementDefaultStorage<T,S> & SetValue(const S & NewValue)
{
Value = NewValue;
EbmlElement::SetValueIsSet();
return *this;
}

const S & GetValue() const
{
return Value;
}

explicit operator const S &() const { return Value; }

protected:
S Value;
};

} // namespace libebml
Expand Down
14 changes: 1 addition & 13 deletions ebml/EbmlFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace libebml {
\class EbmlFloat
\brief Handle all operations on a float EBML element
*/
class EBML_DLL_API EbmlFloat : public EbmlElementDefault<double> {
class EBML_DLL_API EbmlFloat : public EbmlElementDefaultSameStorage<double> {
public:
enum Precision {
FLOAT_32
Expand All @@ -44,21 +44,9 @@ class EBML_DLL_API EbmlFloat : public EbmlElementDefault<double> {
}


bool IsSmallerThan(const EbmlElement *Cmp) const override;

using EbmlElement::operator const EbmlId &;
operator float() const;
operator double() const;

EbmlFloat &SetValue(double NewValue);
double GetValue() const;

bool operator==(const double & val) const override {
return val == Value;
}

private:
double Value; /// The actual value of the element
};

} // namespace libebml
Expand Down
14 changes: 1 addition & 13 deletions ebml/EbmlSInteger.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const int DEFAULT_INT_SIZE = 1; ///< optimal size stored
\class EbmlSInteger
\brief Handle all operations on a signed integer EBML element
*/
class EBML_DLL_API EbmlSInteger : public EbmlElementDefault<std::int64_t> {
class EBML_DLL_API EbmlSInteger : public EbmlElementDefaultSameStorage<std::int64_t> {
public:
EbmlSInteger(const EbmlCallbacksDefault<std::int64_t> &);

Expand All @@ -37,23 +37,11 @@ class EBML_DLL_API EbmlSInteger : public EbmlElementDefault<std::int64_t> {
filepos_t ReadData(IOCallback & input, ScopeMode ReadFully = SCOPE_ALL_DATA) override;
filepos_t UpdateSize(ShouldWrite writeFilter = WriteSkipDefault, bool bForceRender = false) override;

bool IsSmallerThan(const EbmlElement *Cmp) const override;

using EbmlElement::operator const EbmlId &;
explicit operator std::int8_t() const;
explicit operator std::int16_t() const;
explicit operator std::int32_t() const;
explicit operator std::int64_t() const;

EbmlSInteger &SetValue(std::int64_t NewValue);
std::int64_t GetValue() const;

bool operator==(const std::int64_t & val) const override {
return val == Value;
}

private:
std::int64_t Value; /// The actual value of the element
};

} // namespace libebml
Expand Down
9 changes: 1 addition & 8 deletions ebml/EbmlString.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace libebml {
\class EbmlString
\brief Handle all operations on a printable string EBML element
*/
class EBML_DLL_API EbmlString : public EbmlElementDefault<const char *> {
class EBML_DLL_API EbmlString : public EbmlElementDefaultStorage<const char *, std::string> {
public:
EbmlString(const EbmlCallbacksDefault<const char *> &);

Expand All @@ -29,17 +29,10 @@ class EBML_DLL_API EbmlString : public EbmlElementDefault<const char *> {
filepos_t UpdateSize(ShouldWrite writeFilter = WriteSkipDefault, bool bForceRender = false) override;

using EbmlElement::operator const EbmlId &;
explicit operator const std::string &() const;

EbmlString &SetValue(std::string const &NewValue);
std::string GetValue() const;

bool operator==(const char * const & val) const override {
return val == Value;
}

private:
std::string Value; /// The actual value of the element
};

} // namespace libebml
Expand Down
14 changes: 1 addition & 13 deletions ebml/EbmlUInteger.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const int DEFAULT_UINT_SIZE = 0; ///< optimal size stored
\class EbmlUInteger
\brief Handle all operations on an unsigned integer EBML element
*/
class EBML_DLL_API EbmlUInteger : public EbmlElementDefault<std::uint64_t> {
class EBML_DLL_API EbmlUInteger : public EbmlElementDefaultSameStorage<std::uint64_t> {
public:
EbmlUInteger(const EbmlCallbacksDefault<std::uint64_t> &);

Expand All @@ -35,23 +35,11 @@ class EBML_DLL_API EbmlUInteger : public EbmlElementDefault<std::uint64_t> {
filepos_t ReadData(IOCallback & input, ScopeMode ReadFully = SCOPE_ALL_DATA) override;
filepos_t UpdateSize(ShouldWrite writeFilter = WriteSkipDefault, bool bForceRender = false) override;

bool IsSmallerThan(const EbmlElement *Cmp) const override;

using EbmlElement::operator const EbmlId &;
explicit operator std::uint8_t() const;
explicit operator std::uint16_t() const;
explicit operator std::uint32_t() const;
explicit operator std::uint64_t() const;

EbmlUInteger &SetValue(std::uint64_t NewValue);
std::uint64_t GetValue() const;

bool operator==(const std::uint64_t & val) const override {
return val == Value;
}

private:
std::uint64_t Value; /// The actual value of the element
};

} // namespace libebml
Expand Down
8 changes: 1 addition & 7 deletions ebml/EbmlUnicodeString.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class EBML_DLL_API UTFstring {
\brief Handle all operations on a Unicode string EBML element
\note internally treated as a string made of wide characters (ie UCS-2 or UCS-4 depending on the machine)
*/
class EBML_DLL_API EbmlUnicodeString : public EbmlElementDefault<const wchar_t *> {
class EBML_DLL_API EbmlUnicodeString : public EbmlElementDefaultStorage<const wchar_t *, UTFstring> {
public:
EbmlUnicodeString(const EbmlCallbacksDefault<const wchar_t *> &);

Expand All @@ -75,19 +75,13 @@ class EBML_DLL_API EbmlUnicodeString : public EbmlElementDefault<const wchar_t *
filepos_t UpdateSize(ShouldWrite writeFilter = WriteSkipDefault, bool bForceRender = false) override;

using EbmlElement::operator const EbmlId &;
explicit operator const UTFstring &() const;

EbmlUnicodeString &SetValue(UTFstring const &NewValue);
EbmlUnicodeString &SetValueUTF8(std::string const &NewValue);
UTFstring GetValue() const;
std::string GetValueUTF8() const;

bool operator==(const wchar_t * const & val) const override {
return static_cast<UTFstring>(val) == Value;
}

private:
UTFstring Value; /// The actual value of the element
};

} // namespace libebml
Expand Down
8 changes: 0 additions & 8 deletions src/EbmlDate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,4 @@ filepos_t EbmlDate::RenderData(IOCallback & output, bool /* bForceRender */, Sho
return GetSize();
}

bool EbmlDate::IsSmallerThan(const EbmlElement *Cmp) const
{
if (EbmlId(*this) == EbmlId(*Cmp))
return this->Value < static_cast<const EbmlDate *>(Cmp)->Value;

return false;
}

} // namespace libebml
18 changes: 1 addition & 17 deletions src/EbmlFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace libebml {

EbmlFloat::EbmlFloat(const EbmlCallbacksDefault<double> & classInfo, const EbmlFloat::Precision prec)
:EbmlElementDefault(classInfo, 0)
:EbmlElementDefaultSameStorage<double>(classInfo, 0)
{
SetPrecision(prec);
if (classInfo.HasDefault())
Expand All @@ -26,14 +26,6 @@ EbmlFloat::EbmlFloat(const EbmlCallbacksDefault<double> & classInfo, const EbmlF
EbmlFloat::operator float() const {return static_cast<float>(Value);}
EbmlFloat::operator double() const {return (Value);}

double EbmlFloat::GetValue() const {return Value;}

EbmlFloat & EbmlFloat::SetValue(double NewValue) {
Value = NewValue;
SetValueIsSet();
return *this;
}

/*!
\todo handle exception on errors
\todo handle 10 bits precision
Expand Down Expand Up @@ -100,12 +92,4 @@ filepos_t EbmlFloat::ReadData(IOCallback & input, ScopeMode ReadFully)
return GetSize();
}

bool EbmlFloat::IsSmallerThan(const EbmlElement *Cmp) const
{
if (EbmlId(*this) == EbmlId(*Cmp))
return this->Value < static_cast<const EbmlFloat *>(Cmp)->Value;

return false;
}

} // namespace libebml
18 changes: 1 addition & 17 deletions src/EbmlSInteger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ToSigned(std::uint64_t u) {
namespace libebml {

EbmlSInteger::EbmlSInteger(const EbmlCallbacksDefault<std::int64_t> & classInfo)
:EbmlElementDefault(classInfo, DEFAULT_INT_SIZE)
:EbmlElementDefaultSameStorage(classInfo, DEFAULT_INT_SIZE)
{
if (classInfo.HasDefault())
{
Expand All @@ -46,14 +46,6 @@ EbmlSInteger::operator std::int16_t() const {return static_cast<std::int16_t>(Va
EbmlSInteger::operator std::int32_t() const {return static_cast<std::int32_t>(Value);}
EbmlSInteger::operator std::int64_t() const {return Value;}

std::int64_t EbmlSInteger::GetValue() const {return Value;}

EbmlSInteger & EbmlSInteger::SetValue(std::int64_t NewValue) {
Value = NewValue;
SetValueIsSet();
return *this;
}

/*!
\todo handle exception on errors
*/
Expand Down Expand Up @@ -136,12 +128,4 @@ filepos_t EbmlSInteger::ReadData(IOCallback & input, ScopeMode ReadFully)
return GetSize();
}

bool EbmlSInteger::IsSmallerThan(const EbmlElement *Cmp) const
{
if (EbmlId(*this) == EbmlId(*Cmp))
return this->Value < static_cast<const EbmlSInteger *>(Cmp)->Value;

return false;
}

} // namespace libebml
14 changes: 1 addition & 13 deletions src/EbmlString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace libebml {

EbmlString::EbmlString(const EbmlCallbacksDefault<const char *> & classInfo)
:EbmlElementDefault(classInfo, 0)
:EbmlElementDefaultStorage<const char *, std::string>(classInfo, 0)
{
if (classInfo.HasDefault())
{
Expand Down Expand Up @@ -41,18 +41,6 @@ filepos_t EbmlString::RenderData(IOCallback & output, bool /* bForceRender */, S
return Result;
}

EbmlString::operator const std::string &() const {return Value;}

EbmlString &EbmlString::SetValue(std::string const &NewValue) {
Value = NewValue;
SetValueIsSet();
return *this;
}

std::string EbmlString::GetValue() const {
return Value;
}

std::uint64_t EbmlString::UpdateSize(ShouldWrite writeFilter, bool /* bForceRender */)
{
if (!writeFilter(*this))
Expand Down
Loading