From 8176bf75efc58179fe1706a4edcba823b626e9a2 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 30 Sep 2019 08:58:32 -0400 Subject: [PATCH 1/2] RetroTypes: Make functions noexcept where applicable Given these are used quite frequently, it's better to explicitly mark these as noexcept so that they may be used within noexcept contexts. --- Runtime/RetroTypes.hpp | 64 ++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/Runtime/RetroTypes.hpp b/Runtime/RetroTypes.hpp index 45bdd26ad..19c6126a3 100644 --- a/Runtime/RetroTypes.hpp +++ b/Runtime/RetroTypes.hpp @@ -30,17 +30,17 @@ class CAssetId { u64 id = UINT64_MAX; public: - constexpr CAssetId() = default; - constexpr CAssetId(u64 v) { Assign(v); } + constexpr CAssetId() noexcept = default; + constexpr CAssetId(u64 v) noexcept { Assign(v); } explicit CAssetId(CInputStream& in); - constexpr bool IsValid() const { return id != UINT64_MAX; } - constexpr u64 Value() const { return id; } - constexpr void Assign(u64 v) { id = (v == UINT32_MAX ? UINT64_MAX : (v == 0 ? UINT64_MAX : v)); } - constexpr void Reset() { id = UINT64_MAX; } + constexpr bool IsValid() const noexcept { return id != UINT64_MAX; } + constexpr u64 Value() const noexcept { return id; } + constexpr void Assign(u64 v) noexcept { id = (v == UINT32_MAX ? UINT64_MAX : (v == 0 ? UINT64_MAX : v)); } + constexpr void Reset() noexcept { id = UINT64_MAX; } void PutTo(COutputStream& out); - constexpr bool operator==(const CAssetId& other) const { return id == other.id; } - constexpr bool operator!=(const CAssetId& other) const { return !operator==(other); } - constexpr bool operator<(const CAssetId& other) const { return id < other.id; } + constexpr bool operator==(const CAssetId& other) const noexcept { return id == other.id; } + constexpr bool operator!=(const CAssetId& other) const noexcept { return !operator==(other); } + constexpr bool operator<(const CAssetId& other) const noexcept { return id < other.id; } }; //#define kInvalidAssetId CAssetId() @@ -49,12 +49,12 @@ struct SObjectTag { FourCC type; CAssetId id; - constexpr operator bool() const { return id.IsValid(); } - constexpr bool operator==(const SObjectTag& other) const { return id == other.id; } - constexpr bool operator!=(const SObjectTag& other) const { return !operator==(other); } - constexpr bool operator<(const SObjectTag& other) const { return id < other.id; } - constexpr SObjectTag() = default; - constexpr SObjectTag(FourCC tp, CAssetId rid) : type(tp), id(rid) {} + constexpr operator bool() const noexcept { return id.IsValid(); } + constexpr bool operator==(const SObjectTag& other) const noexcept { return id == other.id; } + constexpr bool operator!=(const SObjectTag& other) const noexcept { return !operator==(other); } + constexpr bool operator<(const SObjectTag& other) const noexcept { return id < other.id; } + constexpr SObjectTag() noexcept = default; + constexpr SObjectTag(FourCC tp, CAssetId rid) noexcept : type(tp), id(rid) {} SObjectTag(CInputStream& in) { in.readBytesToBuf(&type, 4); id = CAssetId(in); @@ -68,14 +68,16 @@ struct SObjectTag { struct TEditorId { u32 id = u32(-1); - constexpr TEditorId() = default; - constexpr TEditorId(u32 idin) : id(idin) {} - constexpr u8 LayerNum() const { return u8((id >> 26) & 0x3f); } - constexpr u16 AreaNum() const { return u16((id >> 16) & 0x3ff); } - 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); } + constexpr TEditorId() noexcept = default; + constexpr TEditorId(u32 idin) noexcept : id(idin) {} + constexpr u8 LayerNum() const noexcept { return u8((id >> 26) & 0x3f); } + constexpr u16 AreaNum() const noexcept { return u16((id >> 16) & 0x3ff); } + constexpr u16 Id() const noexcept { return u16(id & 0xffff); } + constexpr bool operator<(const TEditorId& other) const noexcept { return (id & 0x3ffffff) < (other.id & 0x3ffffff); } + constexpr bool operator==(const TEditorId& other) const noexcept { + return (id & 0x3ffffff) == (other.id & 0x3ffffff); + } + constexpr bool operator!=(const TEditorId& other) const noexcept { return !operator==(other); } }; #define kInvalidEditorId TEditorId() @@ -83,13 +85,13 @@ struct TEditorId { struct TUniqueId { u16 id = u16(-1); - constexpr TUniqueId() = default; - constexpr TUniqueId(u16 value, u16 version) : id(value | (version << 10)) {} - constexpr u16 Version() const { return u16((id >> 10) & 0x3f); } - 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); } + constexpr TUniqueId() noexcept = default; + constexpr TUniqueId(u16 value, u16 version) noexcept : id(value | (version << 10)) {} + constexpr u16 Version() const noexcept { return u16((id >> 10) & 0x3f); } + constexpr u16 Value() const noexcept { return u16(id & 0x3ff); } + constexpr bool operator<(const TUniqueId& other) const noexcept { return id < other.id; } + constexpr bool operator==(const TUniqueId& other) const noexcept { return id == other.id; } + constexpr bool operator!=(const TUniqueId& other) const noexcept { return !operator==(other); } }; #define kInvalidUniqueId TUniqueId() @@ -120,7 +122,7 @@ public: #endif template -T GetAverage(const T* v, s32 count) { +T GetAverage(const T* v, s32 count) noexcept { T r = v[0]; for (s32 i = 1; i < count; ++i) r += v[i]; From c77153b03c6b7df9e5fef2e43ea114029ea5ca8c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 30 Sep 2019 09:00:15 -0400 Subject: [PATCH 2/2] RetroTypes: Make SObjectTag's operator bool explicit Prevents potentially error-prone implicit conversions to bool. --- Runtime/RetroTypes.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/RetroTypes.hpp b/Runtime/RetroTypes.hpp index 19c6126a3..a3ecfe4cf 100644 --- a/Runtime/RetroTypes.hpp +++ b/Runtime/RetroTypes.hpp @@ -49,7 +49,7 @@ struct SObjectTag { FourCC type; CAssetId id; - constexpr operator bool() const noexcept { return id.IsValid(); } + constexpr explicit operator bool() const noexcept { return id.IsValid(); } constexpr bool operator==(const SObjectTag& other) const noexcept { return id == other.id; } constexpr bool operator!=(const SObjectTag& other) const noexcept { return !operator==(other); } constexpr bool operator<(const SObjectTag& other) const noexcept { return id < other.id; }