From 2907ca3a4160efd9e8afe1f636f87df5e6964134 Mon Sep 17 00:00:00 2001 From: Rainer Deyke Date: Fri, 1 Apr 2022 11:04:25 +0200 Subject: [PATCH 1/3] Made Key operators constexpr Made operators on Key and KeyModifierMask constexpr, allowing them to be used in switch cases. --- core/os/keyboard.h | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/core/os/keyboard.h b/core/os/keyboard.h index a21d81b2e7d..9464f4305aa 100644 --- a/core/os/keyboard.h +++ b/core/os/keyboard.h @@ -310,67 +310,67 @@ enum class KeyModifierMask { // To avoid having unnecessary operators, only define the ones that are needed. -inline Key operator-(uint32_t a, Key b) { +constexpr Key operator-(uint32_t a, Key b) { return (Key)(a - (uint32_t)b); } -inline Key &operator-=(Key &a, int b) { +constexpr Key &operator-=(Key &a, int b) { return (Key &)((int &)a -= b); } -inline Key operator+(Key a, int b) { +constexpr Key operator+(Key a, int b) { return (Key)((int)a + (int)b); } -inline Key operator+(Key a, Key b) { +constexpr Key operator+(Key a, Key b) { return (Key)((int)a + (int)b); } -inline Key operator-(Key a, Key b) { +constexpr Key operator-(Key a, Key b) { return (Key)((int)a - (int)b); } -inline Key operator&(Key a, Key b) { +constexpr Key operator&(Key a, Key b) { return (Key)((int)a & (int)b); } -inline Key operator|(Key a, Key b) { +constexpr Key operator|(Key a, Key b) { return (Key)((int)a | (int)b); } -inline Key &operator|=(Key &a, Key b) { +constexpr Key &operator|=(Key &a, Key b) { return (Key &)((int &)a |= (int)b); } -inline Key &operator|=(Key &a, KeyModifierMask b) { +constexpr Key &operator|=(Key &a, KeyModifierMask b) { return (Key &)((int &)a |= (int)b); } -inline Key &operator&=(Key &a, KeyModifierMask b) { +constexpr Key &operator&=(Key &a, KeyModifierMask b) { return (Key &)((int &)a &= (int)b); } -inline Key operator|(Key a, KeyModifierMask b) { +constexpr Key operator|(Key a, KeyModifierMask b) { return (Key)((int)a | (int)b); } -inline Key operator&(Key a, KeyModifierMask b) { +constexpr Key operator&(Key a, KeyModifierMask b) { return (Key)((int)a & (int)b); } -inline Key operator+(KeyModifierMask a, Key b) { +constexpr Key operator+(KeyModifierMask a, Key b) { return (Key)((int)a + (int)b); } -inline Key operator|(KeyModifierMask a, Key b) { +constexpr Key operator|(KeyModifierMask a, Key b) { return (Key)((int)a | (int)b); } -inline KeyModifierMask operator+(KeyModifierMask a, KeyModifierMask b) { +constexpr KeyModifierMask operator+(KeyModifierMask a, KeyModifierMask b) { return (KeyModifierMask)((int)a + (int)b); } -inline KeyModifierMask operator|(KeyModifierMask a, KeyModifierMask b) { +constexpr KeyModifierMask operator|(KeyModifierMask a, KeyModifierMask b) { return (KeyModifierMask)((int)a | (int)b); } From 56ae5bbafbbb19dd8cecfec1e5f52b3511ee4fc2 Mon Sep 17 00:00:00 2001 From: Rainer Deyke Date: Mon, 4 Apr 2022 14:55:41 +0200 Subject: [PATCH 2/3] Fixed unsafe casts --- core/os/keyboard.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core/os/keyboard.h b/core/os/keyboard.h index 9464f4305aa..556ba3638e2 100644 --- a/core/os/keyboard.h +++ b/core/os/keyboard.h @@ -315,7 +315,8 @@ constexpr Key operator-(uint32_t a, Key b) { } constexpr Key &operator-=(Key &a, int b) { - return (Key &)((int &)a -= b); + a = static_cast(static_cast(a) - static_cast(b)); + return a; } constexpr Key operator+(Key a, int b) { @@ -339,15 +340,18 @@ constexpr Key operator|(Key a, Key b) { } constexpr Key &operator|=(Key &a, Key b) { - return (Key &)((int &)a |= (int)b); + a = static_cast(static_cast(a) | static_cast(b)); + return a; } constexpr Key &operator|=(Key &a, KeyModifierMask b) { - return (Key &)((int &)a |= (int)b); + a = static_cast(static_cast(a) | static_cast(b)); + return a; } constexpr Key &operator&=(Key &a, KeyModifierMask b) { - return (Key &)((int &)a &= (int)b); + a = static_cast(static_cast(a) & static_cast(b)); + return a; } constexpr Key operator|(Key a, KeyModifierMask b) { From 5583bab9d495eaf328605aa40906a503e215760f Mon Sep 17 00:00:00 2001 From: Rainer Deyke Date: Mon, 4 Apr 2022 15:07:29 +0200 Subject: [PATCH 3/3] Fixed indents --- core/os/keyboard.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/os/keyboard.h b/core/os/keyboard.h index 556ba3638e2..86b86fb970b 100644 --- a/core/os/keyboard.h +++ b/core/os/keyboard.h @@ -315,7 +315,7 @@ constexpr Key operator-(uint32_t a, Key b) { } constexpr Key &operator-=(Key &a, int b) { - a = static_cast(static_cast(a) - static_cast(b)); + a = static_cast(static_cast(a) - static_cast(b)); return a; } @@ -340,17 +340,17 @@ constexpr Key operator|(Key a, Key b) { } constexpr Key &operator|=(Key &a, Key b) { - a = static_cast(static_cast(a) | static_cast(b)); + a = static_cast(static_cast(a) | static_cast(b)); return a; } constexpr Key &operator|=(Key &a, KeyModifierMask b) { - a = static_cast(static_cast(a) | static_cast(b)); + a = static_cast(static_cast(a) | static_cast(b)); return a; } constexpr Key &operator&=(Key &a, KeyModifierMask b) { - a = static_cast(static_cast(a) & static_cast(b)); + a = static_cast(static_cast(a) & static_cast(b)); return a; }