From 066d00d1172ea5dee467dcd60f2d070d6148e5d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20G=C3=B6rner?= Date: Tue, 15 May 2018 22:56:22 +0200 Subject: [PATCH 1/3] Add const and constexpr specifiers to Key union Making the member functions of Key `const' explicitly flags that they will not change the union. This will allow to use Key in const contexts. Adding the `constexpr' specifier to the function makes it possible to rely on the results at compile time. This puts some kind of restrictions on the function, especially when using C++11 and not a newer standard, but these restrictions were already fulfilled, so this seems to be safe. --- src/key_defs.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/key_defs.h b/src/key_defs.h index 5e02389f..76026368 100644 --- a/src/key_defs.h +++ b/src/key_defs.h @@ -26,41 +26,41 @@ union Key { }; uint16_t raw; - inline bool operator==(uint16_t rhs) { + constexpr inline bool operator==(uint16_t rhs) const { return this->raw == rhs; } - inline bool operator==(const Key rhs) { + constexpr inline bool operator==(const Key rhs) const { return this->raw == rhs.raw; } inline Key& operator=(uint16_t raw) { this->raw = raw; return *this; } - inline bool operator!=(const Key& rhs) { + constexpr inline bool operator!=(const Key& rhs) const { return !(*this == rhs); } - inline bool operator>=(uint16_t raw) { + constexpr inline bool operator>=(uint16_t raw) const { return this->raw >= raw; } - inline bool operator<=(uint16_t raw) { + constexpr inline bool operator<=(uint16_t raw) const { return this->raw <= raw; } - inline bool operator>(uint16_t raw) { + constexpr inline bool operator>(uint16_t raw) const { return this->raw > raw; } - inline bool operator<(uint16_t raw) { + constexpr inline bool operator<(uint16_t raw) const { return this->raw < raw; } - inline bool operator>=(const Key& other) { + constexpr inline bool operator>=(const Key& other) const { return this->raw >= other.raw; } - inline bool operator<=(const Key& other) { + constexpr inline bool operator<=(const Key& other) const { return this->raw <= other.raw; } - inline bool operator>(const Key& other) { + constexpr inline bool operator>(const Key& other) const { return this->raw > other.raw; } - inline bool operator<(const Key& other) { + constexpr inline bool operator<(const Key& other) const { return this->raw < other.raw; } }; From b2254e1c7fb0b25bc20ec3f99bee85c1929a7744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20G=C3=B6rner?= Date: Wed, 16 May 2018 15:31:13 +0200 Subject: [PATCH 2/3] Slightly improve function signatures The member functions of the union now take all arguments const, meaning that it is not possible to modify this value somehow. This reduces the chance of subtle bugs and widens the contexts in which these member functions can be used. Furthermore, one signature took a `Key' by value while all functions take `Key' by reference. For the sake of consistency, this was adapted to. --- src/key_defs.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/key_defs.h b/src/key_defs.h index 76026368..91db5d7b 100644 --- a/src/key_defs.h +++ b/src/key_defs.h @@ -26,29 +26,29 @@ union Key { }; uint16_t raw; - constexpr inline bool operator==(uint16_t rhs) const { + constexpr inline bool operator==(const uint16_t rhs) const { return this->raw == rhs; } - constexpr inline bool operator==(const Key rhs) const { + constexpr inline bool operator==(const Key& rhs) const { return this->raw == rhs.raw; } - inline Key& operator=(uint16_t raw) { + inline Key& operator=(const uint16_t raw) { this->raw = raw; return *this; } constexpr inline bool operator!=(const Key& rhs) const { return !(*this == rhs); } - constexpr inline bool operator>=(uint16_t raw) const { + constexpr inline bool operator>=(const uint16_t raw) const { return this->raw >= raw; } - constexpr inline bool operator<=(uint16_t raw) const { + constexpr inline bool operator<=(const uint16_t raw) const { return this->raw <= raw; } - constexpr inline bool operator>(uint16_t raw) const { + constexpr inline bool operator>(const uint16_t raw) const { return this->raw > raw; } - constexpr inline bool operator<(uint16_t raw) const { + constexpr inline bool operator<(const uint16_t raw) const { return this->raw < raw; } constexpr inline bool operator>=(const Key& other) const { From 2889f1c30189b813d9fd485e9cc5435543a5dbc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20G=C3=B6rner?= Date: Thu, 17 May 2018 19:08:11 +0200 Subject: [PATCH 3/3] Remove `inline' for improved readability --- src/key_defs.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/key_defs.h b/src/key_defs.h index 91db5d7b..8238f5cd 100644 --- a/src/key_defs.h +++ b/src/key_defs.h @@ -26,41 +26,41 @@ union Key { }; uint16_t raw; - constexpr inline bool operator==(const uint16_t rhs) const { + constexpr bool operator==(const uint16_t rhs) const { return this->raw == rhs; } - constexpr inline bool operator==(const Key& rhs) const { + constexpr bool operator==(const Key& rhs) const { return this->raw == rhs.raw; } - inline Key& operator=(const uint16_t raw) { + Key& operator=(const uint16_t raw) { this->raw = raw; return *this; } - constexpr inline bool operator!=(const Key& rhs) const { + constexpr bool operator!=(const Key& rhs) const { return !(*this == rhs); } - constexpr inline bool operator>=(const uint16_t raw) const { + constexpr bool operator>=(const uint16_t raw) const { return this->raw >= raw; } - constexpr inline bool operator<=(const uint16_t raw) const { + constexpr bool operator<=(const uint16_t raw) const { return this->raw <= raw; } - constexpr inline bool operator>(const uint16_t raw) const { + constexpr bool operator>(const uint16_t raw) const { return this->raw > raw; } - constexpr inline bool operator<(const uint16_t raw) const { + constexpr bool operator<(const uint16_t raw) const { return this->raw < raw; } - constexpr inline bool operator>=(const Key& other) const { + constexpr bool operator>=(const Key& other) const { return this->raw >= other.raw; } - constexpr inline bool operator<=(const Key& other) const { + constexpr bool operator<=(const Key& other) const { return this->raw <= other.raw; } - constexpr inline bool operator>(const Key& other) const { + constexpr bool operator>(const Key& other) const { return this->raw > other.raw; } - constexpr inline bool operator<(const Key& other) const { + constexpr bool operator<(const Key& other) const { return this->raw < other.raw; } };