2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-05-13 19:51:22 +00:00

RetroTypes: Make types constexpr where applicable

These are generally used as basic tags and ID types, so these can have a
constexpr interface. This is particularly beneficial, given some of
these types are used in file-static lookup tables.

Without being constexpr, these type's constructors in that case are
technically runtime static constructors. While most compilers will
initialize the type at compile-time, this would be dependent on the
optimizer. By marking them constexpr, we allow it outright. It also
allows those arrays to be made constexpr as well.
This commit is contained in:
Lioncash 2019-08-14 05:15:08 -04:00
parent cf294db9eb
commit 1f8ed5af25

View File

@ -27,17 +27,17 @@ class CAssetId {
u64 id = UINT64_MAX; u64 id = UINT64_MAX;
public: public:
CAssetId() = default; constexpr CAssetId() = default;
CAssetId(u64 v) { Assign(v); } constexpr CAssetId(u64 v) { Assign(v); }
explicit CAssetId(CInputStream& in); explicit CAssetId(CInputStream& in);
bool IsValid() const { return id != UINT64_MAX; } constexpr bool IsValid() const { return id != UINT64_MAX; }
u64 Value() const { return id; } constexpr u64 Value() const { return id; }
void Assign(u64 v) { id = (v == UINT32_MAX ? UINT64_MAX : (v == 0 ? UINT64_MAX : v)); } constexpr void Assign(u64 v) { id = (v == UINT32_MAX ? UINT64_MAX : (v == 0 ? UINT64_MAX : v)); }
void Reset() { id = UINT64_MAX; } constexpr void Reset() { id = UINT64_MAX; }
void PutTo(COutputStream& out); void PutTo(COutputStream& out);
bool operator==(const CAssetId& other) const { return id == other.id; } constexpr bool operator==(const CAssetId& other) const { return id == other.id; }
bool operator!=(const CAssetId& other) const { return id != other.id; } constexpr bool operator!=(const CAssetId& other) const { return !operator==(other); }
bool operator<(const CAssetId& other) const { return id < other.id; } constexpr bool operator<(const CAssetId& other) const { return id < other.id; }
}; };
//#define kInvalidAssetId CAssetId() //#define kInvalidAssetId CAssetId()
@ -46,12 +46,12 @@ struct SObjectTag {
FourCC type; FourCC type;
CAssetId id; CAssetId id;
operator bool() const { return id.IsValid(); } constexpr operator bool() const { return id.IsValid(); }
bool operator!=(const SObjectTag& other) const { return id != other.id; } constexpr bool operator==(const SObjectTag& other) const { return id == other.id; }
bool operator==(const SObjectTag& other) const { return id == other.id; } constexpr bool operator!=(const SObjectTag& other) const { return !operator==(other); }
bool operator<(const SObjectTag& other) const { return id < other.id; } constexpr bool operator<(const SObjectTag& other) const { return id < other.id; }
SObjectTag() = default; constexpr SObjectTag() = default;
SObjectTag(FourCC tp, CAssetId rid) : type(tp), id(rid) {} constexpr SObjectTag(FourCC tp, CAssetId rid) : type(tp), id(rid) {}
SObjectTag(CInputStream& in) { SObjectTag(CInputStream& in) {
in.readBytesToBuf(&type, 4); in.readBytesToBuf(&type, 4);
id = CAssetId(in); id = CAssetId(in);
@ -63,28 +63,30 @@ struct SObjectTag {
}; };
struct TEditorId { struct TEditorId {
TEditorId() = default;
TEditorId(u32 idin) : id(idin) {}
u32 id = u32(-1); u32 id = u32(-1);
u8 LayerNum() const { return u8((id >> 26) & 0x3f); }
u16 AreaNum() const { return u16((id >> 16) & 0x3ff); } constexpr TEditorId() = default;
u16 Id() const { return u16(id & 0xffff); } constexpr TEditorId(u32 idin) : id(idin) {}
bool operator<(const TEditorId& other) const { return (id & 0x3ffffff) < (other.id & 0x3ffffff); } constexpr u8 LayerNum() const { return u8((id >> 26) & 0x3f); }
bool operator!=(const TEditorId& other) const { return (id & 0x3ffffff) != (other.id & 0x3ffffff); } constexpr u16 AreaNum() const { return u16((id >> 16) & 0x3ff); }
bool operator==(const TEditorId& other) const { return (id & 0x3ffffff) == (other.id & 0x3ffffff); } constexpr u16 Id() const { return u16(id & 0xffff); }
constexpr bool operator<(const TEditorId& other) const { return (id & 0x3ffffff) < (other.id & 0x3ffffff); }
constexpr bool operator==(const TEditorId& other) const { return (id & 0x3ffffff) == (other.id & 0x3ffffff); }
constexpr bool operator!=(const TEditorId& other) const { return !operator==(other); }
}; };
#define kInvalidEditorId TEditorId() #define kInvalidEditorId TEditorId()
struct TUniqueId { struct TUniqueId {
TUniqueId() = default;
TUniqueId(u16 value, u16 version) : id(value | (version << 10)) {}
u16 id = u16(-1); u16 id = u16(-1);
u16 Version() const { return u16((id >> 10) & 0x3f); }
u16 Value() const { return u16(id & 0x3ff); } constexpr TUniqueId() = default;
bool operator<(const TUniqueId& other) const { return (id < other.id); } constexpr TUniqueId(u16 value, u16 version) : id(value | (version << 10)) {}
bool operator!=(const TUniqueId& other) const { return (id != other.id); } constexpr u16 Version() const { return u16((id >> 10) & 0x3f); }
bool operator==(const TUniqueId& other) const { return (id == other.id); } constexpr u16 Value() const { return u16(id & 0x3ff); }
constexpr bool operator<(const TUniqueId& other) const { return id < other.id; }
constexpr bool operator==(const TUniqueId& other) const { return id == other.id; }
constexpr bool operator!=(const TUniqueId& other) const { return !operator==(other); }
}; };
#define kInvalidUniqueId TUniqueId() #define kInvalidUniqueId TUniqueId()