Skip to content

Commit

Permalink
store the info about having a default values in EbmlCallbacks
Browse files Browse the repository at this point in the history
So it doesn't have to be a virtual class anymore and can be a real static constexp.
  • Loading branch information
robUx4 committed Jan 28, 2024
1 parent 4bca462 commit 45793cf
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions ebml/EbmlElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,38 @@ class EbmlElement;
#define DEFINE_xxx_MASTER(x,id,idl,parent,infinite,name,versions,global) \
static constexpr const libebml::EbmlId Id_##x (id, idl); \
const libebml::EbmlSemanticContext Context_##x = libebml::EbmlSemanticContext(countof(ContextList_##x), ContextList_##x, &Context_##parent, global, &EBML_INFO(x)); \
const libebml::EbmlCallbacks x::ClassInfos(x::Create, Id_##x, infinite, name, Context_##x, versions); \
constexpr const libebml::EbmlCallbacks x::ClassInfos(x::Create, Id_##x, infinite, false, name, Context_##x, versions); \
x::x() :libebml::EbmlMaster(x::ClassInfos) {}

// define a master class with a custom constructor
#define DEFINE_xxx_MASTER_CONS(x,id,idl,parent,infinite,name,versions,global) \
static constexpr const libebml::EbmlId Id_##x (id, idl); \
const libebml::EbmlSemanticContext Context_##x = libebml::EbmlSemanticContext(countof(ContextList_##x), ContextList_##x, &Context_##parent, global, &EBML_INFO(x)); \
const libebml::EbmlCallbacks x::ClassInfos(x::Create, Id_##x, infinite, name, Context_##x, versions);
constexpr const libebml::EbmlCallbacks x::ClassInfos(x::Create, Id_##x, infinite, false, name, Context_##x, versions);

// define a master class with no parent class
#define DEFINE_xxx_MASTER_ORPHAN(x,id,idl,infinite,name,versions,global) \
static constexpr const libebml::EbmlId Id_##x (id, idl); \
const libebml::EbmlSemanticContext Context_##x = libebml::EbmlSemanticContext(countof(ContextList_##x), ContextList_##x, nullptr, global, &EBML_INFO(x)); \
const libebml::EbmlCallbacks x::ClassInfos(x::Create, Id_##x, infinite, name, Context_##x, versions); \
constexpr const libebml::EbmlCallbacks x::ClassInfos(x::Create, Id_##x, infinite, false, name, Context_##x, versions); \

#define DEFINE_xxx_CLASS_CONS(x,id,idl,parent,name,global) \
static constexpr const libebml::EbmlId Id_##x (id, idl); \
const libebml::EbmlSemanticContext Context_##x = libebml::EbmlSemanticContext(0, nullptr, &Context_##parent, global, &EBML_INFO(x));

#define DEFINE_xxx_CLASS_BASE(x,BaseClass,id,idl,parent,name,versions,global) \
DEFINE_xxx_CLASS_CONS(x,id,idl,parent,name,global) \
const libebml::EbmlCallbacks x::ClassInfos(x::Create, Id_##x, false, name, Context_##x, versions); \
constexpr const libebml::EbmlCallbacks x::ClassInfos(x::Create, Id_##x, false, false, name, Context_##x, versions); \
x::x() :libebml::BaseClass(x::ClassInfos) {}

#define DEFINE_xxx_CLASS_BASE_DEFAULT(x,BaseClass,id,idl,parent,name,global,StorageType,defval,versions) \
DEFINE_xxx_CLASS_CONS(x,id,idl,parent,name,global) \
const libebml::EbmlCallbacksWithDefault<StorageType> x::ClassInfos(x::Create, Id_##x, defval, name, Context_##x, versions); \
constexpr const libebml::EbmlCallbacksWithDefault<StorageType> x::ClassInfos(x::Create, Id_##x, defval, name, Context_##x, versions); \
x::x() :libebml::BaseClass(x::ClassInfos) {}

#define DEFINE_xxx_CLASS_BASE_NODEFAULT(x,BaseClass,id,idl,parent,name,global,StorageType,versions) \
DEFINE_xxx_CLASS_CONS(x,id,idl,parent,name,global) \
const libebml::EbmlCallbacksDefault<StorageType> x::ClassInfos(x::Create, Id_##x, name, Context_##x, versions); \
constexpr const libebml::EbmlCallbacksDefault<StorageType> x::ClassInfos(x::Create, Id_##x, name, Context_##x, versions); \
x::x() :libebml::BaseClass(x::ClassInfos) {}

#define DEFINE_xxx_UINTEGER(x,id,idl,parent,name,versions,global) \
Expand Down Expand Up @@ -134,7 +134,7 @@ class EbmlElement;
#define DEFINE_xxx_CLASS_ORPHAN(x,id,idl,name,versions,global) \
static constexpr const libebml::EbmlId Id_##x (id, idl); \
const libebml::EbmlSemanticContext Context_##x = libebml::EbmlSemanticContext(0, nullptr, nullptr, global, nullptr); \
const libebml::EbmlCallbacks x::ClassInfos(x::Create, Id_##x, false, name, Context_##x, versions); \
constexpr const libebml::EbmlCallbacks x::ClassInfos(x::Create, Id_##x, false, false, name, Context_##x, versions); \

#define DEFINE_EBML_CONTEXT(x) DEFINE_xxx_CONTEXT(x,GetEbmlGlobal_Context)

Expand Down Expand Up @@ -318,34 +318,34 @@ class EBML_DLL_API EbmlDocVersion {
*/
class EBML_DLL_API EbmlCallbacks {
public:
constexpr EbmlCallbacks(EbmlElement & (*Creator)(), const EbmlId & aGlobalId, bool aCanInfinite, const char * aDebugName, const EbmlSemanticContext & aContext,
constexpr EbmlCallbacks(EbmlElement & (*Creator)(), const EbmlId & aGlobalId, bool aCanInfinite, bool aHasDefault, const char * aDebugName, const EbmlSemanticContext & aContext,
const EbmlDocVersion & aVersion)
:Create(Creator)
,GlobalId(aGlobalId)
,CanInfinite(aCanInfinite)
,hasDefault(aHasDefault)
,Version(aVersion)
,DebugName(aDebugName)
,Context(aContext)
{
assert(Creator!=nullptr);
}

virtual ~EbmlCallbacks() = default;

inline const EbmlId & ClassId() const { return GlobalId; }
inline constexpr const EbmlSemanticContext & GetContext() const { return Context; }
inline const char * GetName() const { return DebugName; }
inline EbmlElement & NewElement() const { return Create(); }
/// is infinite/unknown size allowed
inline bool CanHaveInfiniteSize() const { return CanInfinite; }
virtual bool HasDefault() const { return false; }
bool HasDefault() const { return hasDefault; }
// get information about supported version for this element
inline const EbmlDocVersion & GetVersions() const { return Version; }

private:
EbmlElement & (* const Create)();
const EbmlId & GlobalId;
const bool CanInfinite;
const bool hasDefault;
const EbmlDocVersion &Version;
const char * DebugName;
const EbmlSemanticContext & Context;
Expand All @@ -355,28 +355,23 @@ template<typename T>
class EBML_DLL_API EbmlCallbacksDefault : public EbmlCallbacks {
public:
constexpr EbmlCallbacksDefault(EbmlElement & (*Creator)(), const EbmlId & aGlobalId, const char * aDebugName, const EbmlSemanticContext & aContext,
const EbmlDocVersion & aVersion)
:EbmlCallbacks(Creator, aGlobalId, false, aDebugName, aContext, aVersion)
const EbmlDocVersion & aVersion, bool hasDefault = false)
:EbmlCallbacks(Creator, aGlobalId, false, hasDefault, aDebugName, aContext, aVersion)
{
}

virtual ~EbmlCallbacksDefault() = default;
};

template<typename T>
class EBML_DLL_API EbmlCallbacksWithDefault : public EbmlCallbacksDefault<T> {
public:
constexpr EbmlCallbacksWithDefault(EbmlElement & (*Creator)(), const EbmlId & aGlobalId, const T &def, const char * aDebugName, const EbmlSemanticContext & aContext,
const EbmlDocVersion & aVersion)
:EbmlCallbacksDefault<T>(Creator, aGlobalId, aDebugName, aContext, aVersion)
:EbmlCallbacksDefault<T>(Creator, aGlobalId, aDebugName, aContext, aVersion, true)
,defaultValue(def)
{
}

virtual ~EbmlCallbacksWithDefault() = default;

inline const T & DefaultValue() const { return defaultValue; }
bool HasDefault() const override { return true; }

private:
const T defaultValue;
Expand Down

0 comments on commit 45793cf

Please sign in to comment.