From adb50a67beebcb885ebcc9526db09316feef7a3c Mon Sep 17 00:00:00 2001 From: Drew Rife Date: Tue, 12 Mar 2024 17:54:29 -0400 Subject: [PATCH 1/7] feat: make type_def constexpr --- include/etl/type_def.h | 72 +++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/include/etl/type_def.h b/include/etl/type_def.h index d16e4cd9c..80214a56a 100644 --- a/include/etl/type_def.h +++ b/include/etl/type_def.h @@ -58,38 +58,38 @@ namespace etl typedef TIdType id_type; //********************************************************************* - type_def() + ETL_CONSTEXPR type_def() : value(TValue()) { } //********************************************************************* - type_def(TValue value_) + ETL_CONSTEXPR type_def(TValue value_) : value(value_) { } //********************************************************************* - type_def(const type_def& other) + ETL_CONSTEXPR type_def(const type_def& other) : value(other.value) { } //********************************************************************* - operator TValue() const + ETL_CONSTEXPR operator TValue() const { return value; } //********************************************************************* - type_def& operator ++() + ETL_CONSTEXPR type_def& operator ++() { ++value; return *this; } //********************************************************************* - type_def operator ++(int) + ETL_CONSTEXPR type_def operator ++(int) { type_def temp(*this); type_def::operator ++(); @@ -97,14 +97,14 @@ namespace etl } //********************************************************************* - type_def& operator --() + ETL_CONSTEXPR type_def& operator --() { --value; return *this; } //********************************************************************* - type_def operator --(int) + ETL_CONSTEXPR type_def operator --(int) { type_def temp(*this); type_def::operator --(); @@ -112,189 +112,189 @@ namespace etl } //********************************************************************* - type_def& operator +=(TValue rhs) + ETL_CONSTEXPR type_def& operator +=(TValue rhs) { value += rhs; return *this; } //********************************************************************* - type_def& operator +=(const type_def& rhs) + ETL_CONSTEXPR type_def& operator +=(const type_def& rhs) { value += rhs.value; return *this; } //********************************************************************* - type_def& operator -=(TValue rhs) + ETL_CONSTEXPR type_def& operator -=(TValue rhs) { value -= rhs; return *this; } //********************************************************************* - type_def& operator -=(const type_def& rhs) + ETL_CONSTEXPR type_def& operator -=(const type_def& rhs) { value -= rhs.value; return *this; } //********************************************************************* - type_def& operator *=(TValue rhs) + ETL_CONSTEXPR type_def& operator *=(TValue rhs) { value *= rhs; return *this; } //********************************************************************* - type_def& operator *=(const type_def& rhs) + ETL_CONSTEXPR type_def& operator *=(const type_def& rhs) { value *= rhs.value; return *this; } //********************************************************************* - type_def& operator /=(TValue rhs) + ETL_CONSTEXPR type_def& operator /=(TValue rhs) { value /= rhs; return *this; } //********************************************************************* - type_def& operator /=(const type_def& rhs) + ETL_CONSTEXPR type_def& operator /=(const type_def& rhs) { value /= rhs.value; return *this; } //********************************************************************* - type_def& operator %=(TValue rhs) + ETL_CONSTEXPR type_def& operator %=(TValue rhs) { value %= rhs; return *this; } //********************************************************************* - type_def& operator %=(const type_def& rhs) + ETL_CONSTEXPR type_def& operator %=(const type_def& rhs) { value %= rhs.value; return *this; } //********************************************************************* - type_def& operator &=(TValue rhs) + ETL_CONSTEXPR type_def& operator &=(TValue rhs) { value &= rhs; return *this; } //********************************************************************* - type_def& operator &=(const type_def& rhs) + ETL_CONSTEXPR type_def& operator &=(const type_def& rhs) { value &= rhs.value; return *this; } //********************************************************************* - type_def& operator |=(TValue rhs) + ETL_CONSTEXPR type_def& operator |=(TValue rhs) { value |= rhs; return *this; } //********************************************************************* - type_def& operator |=(const type_def& rhs) + ETL_CONSTEXPR type_def& operator |=(const type_def& rhs) { value |= rhs.value; return *this; } //********************************************************************* - type_def& operator ^=(TValue rhs) + ETL_CONSTEXPR type_def& operator ^=(TValue rhs) { value ^= rhs; return *this; } //********************************************************************* - type_def& operator ^=(const type_def& rhs) + ETL_CONSTEXPR type_def& operator ^=(const type_def& rhs) { value ^= rhs.value; return *this; } //********************************************************************* - type_def& operator <<=(TValue rhs) + ETL_CONSTEXPR type_def& operator <<=(TValue rhs) { value <<= rhs; return *this; } //********************************************************************* - type_def& operator >>=(TValue rhs) + ETL_CONSTEXPR type_def& operator >>=(TValue rhs) { value >>= rhs; return *this; } //********************************************************************* - type_def& operator =(TValue rhs) + ETL_CONSTEXPR type_def& operator =(TValue rhs) { value = rhs; return *this; } //********************************************************************* - type_def& operator =(const type_def& rhs) + ETL_CONSTEXPR type_def& operator =(const type_def& rhs) { value = rhs.value; return *this; } //********************************************************************* - TValue& get() + ETL_CONSTEXPR TValue& get() { return value; } //********************************************************************* - const TValue& get() const + ETL_CONSTEXPR const TValue& get() const { return value; } //********************************************************************* - friend bool operator <(const type_def& lhs, const type_def& rhs) + friend ETL_CONSTEXPR bool operator <(const type_def& lhs, const type_def& rhs) { return lhs.value < rhs.value; } //********************************************************************* - friend bool operator <=(const type_def& lhs, const type_def& rhs) + friend ETL_CONSTEXPR bool operator <=(const type_def& lhs, const type_def& rhs) { return lhs.value <= rhs.value; } //********************************************************************* - friend bool operator >(const type_def& lhs, const type_def& rhs) + friend ETL_CONSTEXPR bool operator >(const type_def& lhs, const type_def& rhs) { return lhs.value > rhs.value; } //********************************************************************* - friend bool operator >=(const type_def& lhs, const type_def& rhs) + friend ETL_CONSTEXPR bool operator >=(const type_def& lhs, const type_def& rhs) { return lhs.value >= rhs.value; } //********************************************************************* - friend bool operator ==(const type_def& lhs, const type_def& rhs) + friend ETL_CONSTEXPR bool operator ==(const type_def& lhs, const type_def& rhs) { return lhs.value == rhs.value; } //********************************************************************* - friend bool operator !=(const type_def& lhs, const type_def& rhs) + friend ETL_CONSTEXPR bool operator !=(const type_def& lhs, const type_def& rhs) { return lhs.value != rhs.value; } From 0617696451a596af40016a0318316431697b0d38 Mon Sep 17 00:00:00 2001 From: Drew Rife Date: Tue, 12 Mar 2024 17:57:38 -0400 Subject: [PATCH 2/7] test: macro constexpr --- test/test_type_def.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/test_type_def.cpp b/test/test_type_def.cpp index 5cf9203bf..2b1cbdd49 100644 --- a/test/test_type_def.cpp +++ b/test/test_type_def.cpp @@ -51,6 +51,21 @@ namespace CHECK_EQUAL(i1, i2); } + //************************************************************************* + TEST(test_macro_constexpr) + { + ETL_TYPEDEF(uint32_t, type1_t); + ETL_TYPEDEF(uint32_t, type2_t); + + constexpr type1_t t1{1}; + constexpr type2_t t2{1}; + + uint32_t i1 = t1.get(); + uint32_t i2 = t2.get(); + + CHECK_EQUAL(i1, i2); + } + //************************************************************************* TEST(test_implicit) { From 089c1228b85e197a33b197d954b3cb32b97fb604 Mon Sep 17 00:00:00 2001 From: Drew Rife Date: Tue, 12 Mar 2024 18:01:43 -0400 Subject: [PATCH 3/7] test: implicit constexpr --- test/test_type_def.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/test_type_def.cpp b/test/test_type_def.cpp index 2b1cbdd49..df9a6bcea 100644 --- a/test/test_type_def.cpp +++ b/test/test_type_def.cpp @@ -57,8 +57,8 @@ namespace ETL_TYPEDEF(uint32_t, type1_t); ETL_TYPEDEF(uint32_t, type2_t); - constexpr type1_t t1{1}; - constexpr type2_t t2{1}; + constexpr type1_t t1 = type1_t(1); + constexpr type2_t t2 = type2_t(1); uint32_t i1 = t1.get(); uint32_t i2 = t2.get(); @@ -84,6 +84,24 @@ namespace CHECK_EQUAL(i1, i2); } + //************************************************************************* + TEST(test_implicit_constexpr) + { + class type1_t_tag; + typedef etl::type_def type1_t; + + class type2_t_tag; + typedef etl::type_def type2_t; + + constexpr type1_t t1 = type1_t(1); + constexpr type2_t t2 = type2_t(1); + + uint32_t i1 = t1.get(); + uint32_t i2 = t2.get(); + + CHECK_EQUAL(i1, i2); + } + //************************************************************************* TEST(test_get) { From f1d4ef9035122b1eddab1b7153e96118ca725320 Mon Sep 17 00:00:00 2001 From: Drew Rife Date: Tue, 12 Mar 2024 18:06:50 -0400 Subject: [PATCH 4/7] test: get constexpr --- test/test_type_def.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/test_type_def.cpp b/test/test_type_def.cpp index df9a6bcea..dea46ec15 100644 --- a/test/test_type_def.cpp +++ b/test/test_type_def.cpp @@ -117,6 +117,21 @@ namespace CHECK_EQUAL(t1.get(), t2.get()); } + //************************************************************************* + TEST(test_get_constexpr) + { + class type1_t_tag; + typedef etl::type_def type1_t; + + class type2_t_tag; + typedef etl::type_def type2_t; + + constexpr type1_t t1(1); + constexpr type2_t t2(1); + + CHECK_EQUAL(t1.get(), t2.get()); + } + //************************************************************************* TEST(test_operators) { From 35d2226439fb847641d4886280a9c24a7a6906a4 Mon Sep 17 00:00:00 2001 From: Drew Rife Date: Tue, 12 Mar 2024 18:41:31 -0400 Subject: [PATCH 5/7] refactor: remove constexpr from assignment --- include/etl/type_def.h | 44 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/include/etl/type_def.h b/include/etl/type_def.h index 80214a56a..b5f74946c 100644 --- a/include/etl/type_def.h +++ b/include/etl/type_def.h @@ -82,14 +82,14 @@ namespace etl } //********************************************************************* - ETL_CONSTEXPR type_def& operator ++() + type_def& operator ++() { ++value; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def operator ++(int) + type_def operator ++(int) { type_def temp(*this); type_def::operator ++(); @@ -97,14 +97,14 @@ namespace etl } //********************************************************************* - ETL_CONSTEXPR type_def& operator --() + type_def& operator --() { --value; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def operator --(int) + type_def operator --(int) { type_def temp(*this); type_def::operator --(); @@ -112,126 +112,126 @@ namespace etl } //********************************************************************* - ETL_CONSTEXPR type_def& operator +=(TValue rhs) + type_def& operator +=(TValue rhs) { value += rhs; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator +=(const type_def& rhs) + type_def& operator +=(const type_def& rhs) { value += rhs.value; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator -=(TValue rhs) + type_def& operator -=(TValue rhs) { value -= rhs; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator -=(const type_def& rhs) + type_def& operator -=(const type_def& rhs) { value -= rhs.value; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator *=(TValue rhs) + type_def& operator *=(TValue rhs) { value *= rhs; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator *=(const type_def& rhs) + type_def& operator *=(const type_def& rhs) { value *= rhs.value; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator /=(TValue rhs) + type_def& operator /=(TValue rhs) { value /= rhs; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator /=(const type_def& rhs) + type_def& operator /=(const type_def& rhs) { value /= rhs.value; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator %=(TValue rhs) + type_def& operator %=(TValue rhs) { value %= rhs; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator %=(const type_def& rhs) + type_def& operator %=(const type_def& rhs) { value %= rhs.value; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator &=(TValue rhs) + type_def& operator &=(TValue rhs) { value &= rhs; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator &=(const type_def& rhs) + type_def& operator &=(const type_def& rhs) { value &= rhs.value; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator |=(TValue rhs) + type_def& operator |=(TValue rhs) { value |= rhs; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator |=(const type_def& rhs) + type_def& operator |=(const type_def& rhs) { value |= rhs.value; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator ^=(TValue rhs) + type_def& operator ^=(TValue rhs) { value ^= rhs; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator ^=(const type_def& rhs) + type_def& operator ^=(const type_def& rhs) { value ^= rhs.value; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator <<=(TValue rhs) + type_def& operator <<=(TValue rhs) { value <<= rhs; return *this; } //********************************************************************* - ETL_CONSTEXPR type_def& operator >>=(TValue rhs) + type_def& operator >>=(TValue rhs) { value >>= rhs; return *this; From 014dc5d59ab9619e05b00a17457c8b6f30dfbb2b Mon Sep 17 00:00:00 2001 From: Drew Rife Date: Tue, 12 Mar 2024 18:41:44 -0400 Subject: [PATCH 6/7] test: comparisons constexpr --- test/test_type_def.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/test_type_def.cpp b/test/test_type_def.cpp index dea46ec15..b2f271728 100644 --- a/test/test_type_def.cpp +++ b/test/test_type_def.cpp @@ -189,5 +189,28 @@ namespace CHECK(!(t1 >= t2)); CHECK(t2 >= t4); } + + //************************************************************************* + TEST(test_comparisons_constexpr) + { + class __type_t__; + typedef etl::type_def<__type_t__, uint32_t> type_t; + + constexpr type_t t1(1); + constexpr type_t t2(2); + constexpr type_t t3(t1); + constexpr type_t t4(t2); + + CHECK(t1 < t2); + CHECK(!(t2 < t1)); + CHECK(t1 <= t2); + CHECK(!(t2 <= t1)); + CHECK(t1 <= t3); + CHECK(t2 > t1); + CHECK(!(t1 > t2)); + CHECK(t2 >= t1); + CHECK(!(t1 >= t2)); + CHECK(t2 >= t4); + } }; } From 11c92d5f80a8b93b619e8b1a838de9ba95d2c5ad Mon Sep 17 00:00:00 2001 From: Drew Rife Date: Tue, 12 Mar 2024 18:51:33 -0400 Subject: [PATCH 7/7] fix: cpp11 support for constexpr get method --- include/etl/type_def.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/etl/type_def.h b/include/etl/type_def.h index b5f74946c..9b0033135 100644 --- a/include/etl/type_def.h +++ b/include/etl/type_def.h @@ -257,11 +257,19 @@ namespace etl return value; } +#if ETL_USING_CPP14 //********************************************************************* ETL_CONSTEXPR const TValue& get() const { return value; } +#else + //********************************************************************* + const TValue& get() const + { + return value; + } +#endif // ETL_USING_CPP14 //********************************************************************* friend ETL_CONSTEXPR bool operator <(const type_def& lhs, const type_def& rhs)