From 646632e43af96153249aa902c9c9a0bcee8688e4 Mon Sep 17 00:00:00 2001 From: Piotr Kosek Date: Fri, 31 Aug 2018 16:42:57 +0200 Subject: [PATCH 01/12] Added indexable properties on enums: - _to_index which return value 0... size-1 (even if enums are note sequential) - _from_index used for other way around - _from_index_nothrow no throw version - _from_index_unchecked returns invalid enum in case arg>=size Code and test cases added. No documentation updates (my English is too poor for that) --- enum.h | 55 ++++++++++++++++++++++++ test/cxxtest/general.h | 97 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) diff --git a/enum.h b/enum.h index ab5ea18a..b24932f3 100644 --- a/enum.h +++ b/enum.h @@ -343,6 +343,12 @@ BETTER_ENUMS_CONSTEXPR_ static T* _or_null(optional maybe) return maybe ? *maybe : BETTER_ENUMS_NULLPTR; } +template +BETTER_ENUMS_CONSTEXPR_ static T _or_zero(optional maybe) +{ + return maybe ? *maybe : static_cast(0); +} + // Functional sequencing. This is essentially a comma operator wrapped in a @@ -615,6 +621,15 @@ class Enum { \ _from_integral_unchecked(_integral value); \ BETTER_ENUMS_CONSTEXPR_ static _optional \ _from_integral_nothrow(_integral value); \ + \ + BETTER_ENUMS_CONSTEXPR_ _integral _to_index() const; \ + BETTER_ENUMS_IF_EXCEPTIONS( \ + BETTER_ENUMS_CONSTEXPR_ static Enum _from_index(_integral value); \ + ) \ + BETTER_ENUMS_CONSTEXPR_ static Enum \ + _from_index_unchecked(_integral value); \ + BETTER_ENUMS_CONSTEXPR_ static _optional \ + _from_index_nothrow(_integral value); \ \ ToStringConstexpr const char* _to_string() const; \ BETTER_ENUMS_IF_EXCEPTIONS( \ @@ -661,6 +676,8 @@ class Enum { \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ _from_value_loop(_integral value, std::size_t index = 0); \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ + _from_index_loop(_integral value, std::size_t index = 0); \ + BETTER_ENUMS_CONSTEXPR_ static _optional_index \ _from_string_loop(const char *name, std::size_t index = 0); \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ _from_string_nocase_loop(const char *name, std::size_t index = 0); \ @@ -700,6 +717,17 @@ Enum::_from_value_loop(Enum::_integral value, std::size_t index) \ _from_value_loop(value, index + 1); \ } \ \ +BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional \ +Enum::_from_index_loop(Enum::_integral value, std::size_t index) \ +{ \ + return \ + index == _size() ? \ + _optional() : \ + BETTER_ENUMS_NS(Enum)::index == value ? \ + _optional(_value_array[index]) : \ + _from_value_loop(value, index + 1); \ +} \ + \ BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional_index \ Enum::_from_string_loop(const char *name, std::size_t index) \ { \ @@ -727,6 +755,33 @@ BETTER_ENUMS_CONSTEXPR_ inline Enum::_integral Enum::_to_integral() const \ return _integral(_value); \ } \ \ +BETTER_ENUMS_CONSTEXPR_ inline Enum::_integral Enum::_to_index() const \ +{ \ + return _from_value_loop(value) \ +} \ + \ +BETTER_ENUMS_CONSTEXPR_ inline Enum \ +Enum::_from_index_unchecked(_integral value) \ +{ \ + return \ + ::better_enums::_or_zero(_from_index_nothrow(value)); \ +} \ + \ +BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional \ +Enum::_from_index_nothrow(_integral value) \ +{ \ + return _from_index_loop(value); \ +} \ + \ +BETTER_ENUMS_IF_EXCEPTIONS( \ +BETTER_ENUMS_CONSTEXPR_ inline Enum Enum::_from_index(_integral value) \ +{ \ + return \ + ::better_enums::_or_throw(_from_index_nothrow(value), \ + #Enum "::_from_index: invalid argument"); \ +} \ +) \ + \ BETTER_ENUMS_CONSTEXPR_ inline Enum \ Enum::_from_integral_unchecked(_integral value) \ { \ diff --git a/test/cxxtest/general.h b/test/cxxtest/general.h index caa54146..808c73a4 100644 --- a/test/cxxtest/general.h +++ b/test/cxxtest/general.h @@ -313,6 +313,103 @@ class EnumTests : public CxxTest::TestSuite { +test::Namespaced::One); TS_ASSERT_EQUALS(strcmp(*test::Namespaced::_names().begin(), "One"), 0); } + + void test_to_index() + { + TS_ASSERT_EQUALS((+Channel::Red)._to_index(), 0); + TS_ASSERT_EQUALS((+Channel::Green)._to_index(), 1); + TS_ASSERT_EQUALS((+Channel::Blue)._to_index(), 2); + + TS_ASSERT_EQUALS((+Depth::HighColor)._to_index(), 0); + TS_ASSERT_EQUALS((+Depth::TrueColor)._to_index(), 1); + + TS_ASSERT_EQUALS((+Compression::None)._to_index(), 0); + TS_ASSERT_EQUALS((+Compression::Huffman)._to_index(), 1); + TS_ASSERT_EQUALS((+Compression::Default)._to_index(), 2); + } + + void test_from_index() + { + TS_ASSERT_EQUALS((+Channel::Red)), Depth::_from_index(0)); + TS_ASSERT_EQUALS((+Channel::Green), Depth::_from_index(1)); + TS_ASSERT_EQUALS((+Channel::Blue), Depth::_from_index(1)); + TS_ASSERT_THROWS(Channel::_from_index(42), std::runtime_error); + + TS_ASSERT_EQUALS((+Depth::HighColor)), Depth::_from_index(0)); + TS_ASSERT_EQUALS((+Depth::TrueColor), Depth::_from_index(1)); + TS_ASSERT_THROWS(Depth::_from_index(42), std::runtime_error); + + TS_ASSERT_EQUALS((+Compression::None), Compression::_from_index(0)); + TS_ASSERT_EQUALS((+Compression::Huffman), Compression::_from_index(1)); + TS_ASSERT_EQUALS((+Compression::Default), Compression::_from_index(2)); + TS_ASSERT_THROWS(Compression::_from_index(42), std::runtime_error); + } + + void test_from_index_nothrow() + { + better_enums::optional maybe_channel = Channel::_from_index(0); + TS_ASSERT(maybe_channel); + TS_ASSERT_EQUALS(*maybe_channel, +Channel::Red); + + maybe_channel = Channel::_from_index(1); + TS_ASSERT(maybe_channel); + TS_ASSERT_EQUALS(*maybe_channel, +Channel::Green); + + maybe_channel = Channel::_from_index(2); + TS_ASSERT(maybe_channel); + TS_ASSERT_EQUALS(*maybe_channel, +Channel::Blue); + TS_ASSERT(!Channel::_from_index(45)); + + better_enums::optional maybe_depth = Depth::_from_index(0); + TS_ASSERT(maybe_depth); + TS_ASSERT_EQUALS(*maybe_depth, +Depth::HighColor); + + maybe_depth = Depth::_from_index(1); + TS_ASSERT(maybe_depth); + TS_ASSERT_EQUALS(*maybe_depth, +Depth::TrueColor); + TS_ASSERT(!Channel::_from_index(45)); + + better_enums::optional maybe_depth = Depth::_from_index(0); + TS_ASSERT(maybe_depth); + TS_ASSERT_EQUALS(*maybe_depth, +Depth::HighColor); + + maybe_depth = Depth::_from_index(1); + TS_ASSERT(maybe_depth); + TS_ASSERT_EQUALS(*maybe_depth, +Depth::TrueColor); + TS_ASSERT(!Channel::_from_index(45)); + + better_enums::optional maybe_depth = Depth::_from_index(0); + TS_ASSERT(maybe_depth); + TS_ASSERT_EQUALS(*maybe_depth, +Depth::HighColor); + + better_enums::optional maybe_compression = Compression::_from_index(0); + TS_ASSERT(maybe_compression); + TS_ASSERT_EQUALS(*maybe_compression, +Compression::None); + + maybe_compression = Compression::_from_index(1); + TS_ASSERT(maybe_compression); + TS_ASSERT_EQUALS(*maybe_compression, +Compression::Huffman); + + maybe_compression = Compression::_from_index(2); + TS_ASSERT(maybe_compression); + TS_ASSERT_EQUALS(*maybe_compression, +Compression::Default); + TS_ASSERT(!Compression::_from_index(45)); + } + + void test_from_index_unchecked() + { + + TS_ASSERT_EQUALS((+Channel::Red), Channel::_from_index_unchecked(0)); + TS_ASSERT_EQUALS((+Channel::Green), Channel::_from_index_unchecked(1)); + TS_ASSERT_EQUALS((+Channel::Blue), Channel::_from_index_unchecked(2)); + + TS_ASSERT_EQUALS((+Depth::HighColor)), Depth::_from_index_unchecked(0)); + TS_ASSERT_EQUALS((+Depth::TrueColor), Depth::_from_index_unchecked(1)); + + TS_ASSERT_EQUALS((+Compression::None), Compression::_from_index_unchecked(0)); + TS_ASSERT_EQUALS((+Compression::Huffman), Compression::_from_index_unchecked(1)); + TS_ASSERT_EQUALS((+Compression::Default), Compression::_from_index_unchecked(2)); + } }; From 93ceb8a37d12f656f188864b577d00d8029c05a2 Mon Sep 17 00:00:00 2001 From: Piotr Kosek Date: Fri, 31 Aug 2018 17:07:33 +0200 Subject: [PATCH 02/12] Fixed not compiling code --- enum.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/enum.h b/enum.h index b24932f3..8025406c 100644 --- a/enum.h +++ b/enum.h @@ -346,7 +346,7 @@ BETTER_ENUMS_CONSTEXPR_ static T* _or_null(optional maybe) template BETTER_ENUMS_CONSTEXPR_ static T _or_zero(optional maybe) { - return maybe ? *maybe : static_cast(0); + return maybe ? *maybe : T::_from_integral_unchecked(0); } @@ -675,7 +675,7 @@ class Enum { \ \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ _from_value_loop(_integral value, std::size_t index = 0); \ - BETTER_ENUMS_CONSTEXPR_ static _optional_index \ + BETTER_ENUMS_CONSTEXPR_ static _optional \ _from_index_loop(_integral value, std::size_t index = 0); \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ _from_string_loop(const char *name, std::size_t index = 0); \ @@ -721,11 +721,9 @@ BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional \ Enum::_from_index_loop(Enum::_integral value, std::size_t index) \ { \ return \ - index == _size() ? \ + index >= _size() ? \ _optional() : \ - BETTER_ENUMS_NS(Enum)::index == value ? \ - _optional(_value_array[index]) : \ - _from_value_loop(value, index + 1); \ + _optional(BETTER_ENUMS_NS(Enum)::_value_array[index]); \ } \ \ BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional_index \ @@ -757,7 +755,7 @@ BETTER_ENUMS_CONSTEXPR_ inline Enum::_integral Enum::_to_integral() const \ \ BETTER_ENUMS_CONSTEXPR_ inline Enum::_integral Enum::_to_index() const \ { \ - return _from_value_loop(value) \ + return *_from_value_loop(_value); \ } \ \ BETTER_ENUMS_CONSTEXPR_ inline Enum \ From d0c7473450f3e4ed31285fa1c5b97db71081cec3 Mon Sep 17 00:00:00 2001 From: Piotr Kosek Date: Fri, 31 Aug 2018 17:14:26 +0200 Subject: [PATCH 03/12] Fixed a problem with unusued value (inside _from_index_loop ) --- enum.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/enum.h b/enum.h index 8025406c..2c938239 100644 --- a/enum.h +++ b/enum.h @@ -676,7 +676,7 @@ class Enum { \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ _from_value_loop(_integral value, std::size_t index = 0); \ BETTER_ENUMS_CONSTEXPR_ static _optional \ - _from_index_loop(_integral value, std::size_t index = 0); \ + _from_index_loop(_integral value); \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ _from_string_loop(const char *name, std::size_t index = 0); \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ @@ -718,12 +718,12 @@ Enum::_from_value_loop(Enum::_integral value, std::size_t index) \ } \ \ BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional \ -Enum::_from_index_loop(Enum::_integral value, std::size_t index) \ +Enum::_from_index_loop(Enum::_integral value) \ { \ return \ index >= _size() ? \ _optional() : \ - _optional(BETTER_ENUMS_NS(Enum)::_value_array[index]); \ + _optional(BETTER_ENUMS_NS(Enum)::_value_array[value]); \ } \ \ BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional_index \ From aa6527dad0766f3af09110f157a5c3eef7d998d5 Mon Sep 17 00:00:00 2001 From: Piotr Kosek Date: Fri, 31 Aug 2018 17:17:26 +0200 Subject: [PATCH 04/12] Too trigger happy :-( --- enum.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/enum.h b/enum.h index 2c938239..83d41cd7 100644 --- a/enum.h +++ b/enum.h @@ -721,7 +721,7 @@ BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional \ Enum::_from_index_loop(Enum::_integral value) \ { \ return \ - index >= _size() ? \ + value >= _size() ? \ _optional() : \ _optional(BETTER_ENUMS_NS(Enum)::_value_array[value]); \ } \ From 822f29ee58a56d717d5d02590c89403a9091af55 Mon Sep 17 00:00:00 2001 From: Piotr Kosek Date: Fri, 31 Aug 2018 17:24:29 +0200 Subject: [PATCH 05/12] Fridays... --- enum.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/enum.h b/enum.h index 83d41cd7..9ce673d4 100644 --- a/enum.h +++ b/enum.h @@ -676,7 +676,7 @@ class Enum { \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ _from_value_loop(_integral value, std::size_t index = 0); \ BETTER_ENUMS_CONSTEXPR_ static _optional \ - _from_index_loop(_integral value); \ + _from_index_loop(std::size_t index); \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ _from_string_loop(const char *name, std::size_t index = 0); \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ @@ -718,12 +718,12 @@ Enum::_from_value_loop(Enum::_integral value, std::size_t index) \ } \ \ BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional \ -Enum::_from_index_loop(Enum::_integral value) \ +Enum::_from_index_loop(std::size_t index) \ { \ return \ - value >= _size() ? \ + index >= _size() ? \ _optional() : \ - _optional(BETTER_ENUMS_NS(Enum)::_value_array[value]); \ + _optional(BETTER_ENUMS_NS(Enum)::_value_array[index]); \ } \ \ BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional_index \ From e1341951a24ea07f23a16a553cee7bd69e05d1ff Mon Sep 17 00:00:00 2001 From: Piotr Kosek Date: Fri, 31 Aug 2018 17:31:54 +0200 Subject: [PATCH 06/12] ... --- enum.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/enum.h b/enum.h index 9ce673d4..eeb3cba2 100644 --- a/enum.h +++ b/enum.h @@ -621,8 +621,8 @@ class Enum { \ _from_integral_unchecked(_integral value); \ BETTER_ENUMS_CONSTEXPR_ static _optional \ _from_integral_nothrow(_integral value); \ - \ - BETTER_ENUMS_CONSTEXPR_ _integral _to_index() const; \ + \ + BETTER_ENUMS_CONSTEXPR_ std::size_t _to_index() const; \ BETTER_ENUMS_IF_EXCEPTIONS( \ BETTER_ENUMS_CONSTEXPR_ static Enum _from_index(_integral value); \ ) \ @@ -753,7 +753,7 @@ BETTER_ENUMS_CONSTEXPR_ inline Enum::_integral Enum::_to_integral() const \ return _integral(_value); \ } \ \ -BETTER_ENUMS_CONSTEXPR_ inline Enum::_integral Enum::_to_index() const \ +BETTER_ENUMS_CONSTEXPR_ inline std::size_t Enum::_to_index() const \ { \ return *_from_value_loop(_value); \ } \ From 7ad65be8581da277aed8729a4daf998bace4606e Mon Sep 17 00:00:00 2001 From: Piotr Kosek Date: Fri, 31 Aug 2018 17:36:27 +0200 Subject: [PATCH 07/12] Fixed tests --- test/cxxtest/general.h | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/test/cxxtest/general.h b/test/cxxtest/general.h index 808c73a4..f27241eb 100644 --- a/test/cxxtest/general.h +++ b/test/cxxtest/general.h @@ -368,20 +368,7 @@ class EnumTests : public CxxTest::TestSuite { TS_ASSERT(maybe_depth); TS_ASSERT_EQUALS(*maybe_depth, +Depth::TrueColor); TS_ASSERT(!Channel::_from_index(45)); - - better_enums::optional maybe_depth = Depth::_from_index(0); - TS_ASSERT(maybe_depth); - TS_ASSERT_EQUALS(*maybe_depth, +Depth::HighColor); - - maybe_depth = Depth::_from_index(1); - TS_ASSERT(maybe_depth); - TS_ASSERT_EQUALS(*maybe_depth, +Depth::TrueColor); - TS_ASSERT(!Channel::_from_index(45)); - - better_enums::optional maybe_depth = Depth::_from_index(0); - TS_ASSERT(maybe_depth); - TS_ASSERT_EQUALS(*maybe_depth, +Depth::HighColor); - + better_enums::optional maybe_compression = Compression::_from_index(0); TS_ASSERT(maybe_compression); TS_ASSERT_EQUALS(*maybe_compression, +Compression::None); @@ -403,7 +390,7 @@ class EnumTests : public CxxTest::TestSuite { TS_ASSERT_EQUALS((+Channel::Green), Channel::_from_index_unchecked(1)); TS_ASSERT_EQUALS((+Channel::Blue), Channel::_from_index_unchecked(2)); - TS_ASSERT_EQUALS((+Depth::HighColor)), Depth::_from_index_unchecked(0)); + TS_ASSERT_EQUALS((+Depth::HighColor), Depth::_from_index_unchecked(0)); TS_ASSERT_EQUALS((+Depth::TrueColor), Depth::_from_index_unchecked(1)); TS_ASSERT_EQUALS((+Compression::None), Compression::_from_index_unchecked(0)); From a3dca4ff32f95ebdcc180459570fb1e8171acd01 Mon Sep 17 00:00:00 2001 From: Piotr Kosek Date: Fri, 31 Aug 2018 17:40:23 +0200 Subject: [PATCH 08/12] ... --- test/cxxtest/general.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/cxxtest/general.h b/test/cxxtest/general.h index f27241eb..48dafb07 100644 --- a/test/cxxtest/general.h +++ b/test/cxxtest/general.h @@ -330,12 +330,12 @@ class EnumTests : public CxxTest::TestSuite { void test_from_index() { - TS_ASSERT_EQUALS((+Channel::Red)), Depth::_from_index(0)); + TS_ASSERT_EQUALS((+Channel::Red), Depth::_from_index(0)); TS_ASSERT_EQUALS((+Channel::Green), Depth::_from_index(1)); TS_ASSERT_EQUALS((+Channel::Blue), Depth::_from_index(1)); TS_ASSERT_THROWS(Channel::_from_index(42), std::runtime_error); - TS_ASSERT_EQUALS((+Depth::HighColor)), Depth::_from_index(0)); + TS_ASSERT_EQUALS((+Depth::HighColor), Depth::_from_index(0)); TS_ASSERT_EQUALS((+Depth::TrueColor), Depth::_from_index(1)); TS_ASSERT_THROWS(Depth::_from_index(42), std::runtime_error); From b5b478d8054d4db90248c1d54195fa137e25e70a Mon Sep 17 00:00:00 2001 From: Piotr Kosek Date: Fri, 31 Aug 2018 17:49:54 +0200 Subject: [PATCH 09/12] Fixed non-passed tests --- test/cxxtest/general.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/test/cxxtest/general.h b/test/cxxtest/general.h index 48dafb07..a756adf7 100644 --- a/test/cxxtest/general.h +++ b/test/cxxtest/general.h @@ -330,9 +330,9 @@ class EnumTests : public CxxTest::TestSuite { void test_from_index() { - TS_ASSERT_EQUALS((+Channel::Red), Depth::_from_index(0)); - TS_ASSERT_EQUALS((+Channel::Green), Depth::_from_index(1)); - TS_ASSERT_EQUALS((+Channel::Blue), Depth::_from_index(1)); + TS_ASSERT_EQUALS((+Channel::Red), Channel::_from_index(0)); + TS_ASSERT_EQUALS((+Channel::Green), Channel::_from_index(1)); + TS_ASSERT_EQUALS((+Channel::Blue), Channel::_from_index(2)); TS_ASSERT_THROWS(Channel::_from_index(42), std::runtime_error); TS_ASSERT_EQUALS((+Depth::HighColor), Depth::_from_index(0)); @@ -358,7 +358,9 @@ class EnumTests : public CxxTest::TestSuite { maybe_channel = Channel::_from_index(2); TS_ASSERT(maybe_channel); TS_ASSERT_EQUALS(*maybe_channel, +Channel::Blue); - TS_ASSERT(!Channel::_from_index(45)); + + maybe_channel = Channel::_from_index(45); + TS_ASSERT(!maybe_channel); better_enums::optional maybe_depth = Depth::_from_index(0); TS_ASSERT(maybe_depth); @@ -367,7 +369,9 @@ class EnumTests : public CxxTest::TestSuite { maybe_depth = Depth::_from_index(1); TS_ASSERT(maybe_depth); TS_ASSERT_EQUALS(*maybe_depth, +Depth::TrueColor); - TS_ASSERT(!Channel::_from_index(45)); + + maybe_depth = Depth::_from_index(45); + TS_ASSERT(!maybe_depth); better_enums::optional maybe_compression = Compression::_from_index(0); TS_ASSERT(maybe_compression); @@ -380,7 +384,9 @@ class EnumTests : public CxxTest::TestSuite { maybe_compression = Compression::_from_index(2); TS_ASSERT(maybe_compression); TS_ASSERT_EQUALS(*maybe_compression, +Compression::Default); - TS_ASSERT(!Compression::_from_index(45)); + + maybe_compression = Compression::_from_index(45); + TS_ASSERT(!maybe_compression); } void test_from_index_unchecked() From db287125b55162804bfd580042c1b24de085d3d7 Mon Sep 17 00:00:00 2001 From: Piotr Kosek Date: Fri, 31 Aug 2018 17:53:40 +0200 Subject: [PATCH 10/12] Tests fixed again --- test/cxxtest/general.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/cxxtest/general.h b/test/cxxtest/general.h index a756adf7..7dbef050 100644 --- a/test/cxxtest/general.h +++ b/test/cxxtest/general.h @@ -325,7 +325,7 @@ class EnumTests : public CxxTest::TestSuite { TS_ASSERT_EQUALS((+Compression::None)._to_index(), 0); TS_ASSERT_EQUALS((+Compression::Huffman)._to_index(), 1); - TS_ASSERT_EQUALS((+Compression::Default)._to_index(), 2); +// TS_ASSERT_EQUALS((+Compression::Default)._to_index(), 2); // This won't pass as Compression::Huffman == Compression::Default } void test_from_index() @@ -347,45 +347,45 @@ class EnumTests : public CxxTest::TestSuite { void test_from_index_nothrow() { - better_enums::optional maybe_channel = Channel::_from_index(0); + better_enums::optional maybe_channel = Channel::_from_index_nothrow(0); TS_ASSERT(maybe_channel); TS_ASSERT_EQUALS(*maybe_channel, +Channel::Red); - maybe_channel = Channel::_from_index(1); + maybe_channel = Channel::_from_index_nothrow(1); TS_ASSERT(maybe_channel); TS_ASSERT_EQUALS(*maybe_channel, +Channel::Green); - maybe_channel = Channel::_from_index(2); + maybe_channel = Channel::_from_index_nothrow(2); TS_ASSERT(maybe_channel); TS_ASSERT_EQUALS(*maybe_channel, +Channel::Blue); - maybe_channel = Channel::_from_index(45); + maybe_channel = Channel::_from_index_nothrow(45); TS_ASSERT(!maybe_channel); - better_enums::optional maybe_depth = Depth::_from_index(0); + better_enums::optional maybe_depth = Depth::_from_index_nothrow(0); TS_ASSERT(maybe_depth); TS_ASSERT_EQUALS(*maybe_depth, +Depth::HighColor); - maybe_depth = Depth::_from_index(1); + maybe_depth = Depth::_from_index_nothrow(1); TS_ASSERT(maybe_depth); TS_ASSERT_EQUALS(*maybe_depth, +Depth::TrueColor); - maybe_depth = Depth::_from_index(45); + maybe_depth = Depth::_from_index_nothrow(45); TS_ASSERT(!maybe_depth); - better_enums::optional maybe_compression = Compression::_from_index(0); + better_enums::optional maybe_compression = Compression::_from_index_nothrow(0); TS_ASSERT(maybe_compression); TS_ASSERT_EQUALS(*maybe_compression, +Compression::None); - maybe_compression = Compression::_from_index(1); + maybe_compression = Compression::_from_index_nothrow(1); TS_ASSERT(maybe_compression); TS_ASSERT_EQUALS(*maybe_compression, +Compression::Huffman); - maybe_compression = Compression::_from_index(2); + maybe_compression = Compression::_from_index_nothrow(2); TS_ASSERT(maybe_compression); TS_ASSERT_EQUALS(*maybe_compression, +Compression::Default); - maybe_compression = Compression::_from_index(45); + maybe_compression = Compression::_from_index_nothrow(45); TS_ASSERT(!maybe_compression); } From f044492ae8a02341e6695813848f55c87a2868c2 Mon Sep 17 00:00:00 2001 From: Piotr Kosek Date: Fri, 31 Aug 2018 17:56:56 +0200 Subject: [PATCH 11/12] (Hopefully last fix) _from_index will now take std::size_t --- enum.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/enum.h b/enum.h index eeb3cba2..5d534eb5 100644 --- a/enum.h +++ b/enum.h @@ -624,12 +624,12 @@ class Enum { \ \ BETTER_ENUMS_CONSTEXPR_ std::size_t _to_index() const; \ BETTER_ENUMS_IF_EXCEPTIONS( \ - BETTER_ENUMS_CONSTEXPR_ static Enum _from_index(_integral value); \ + BETTER_ENUMS_CONSTEXPR_ static Enum _from_index(std::size_t value); \ ) \ BETTER_ENUMS_CONSTEXPR_ static Enum \ - _from_index_unchecked(_integral value); \ + _from_index_unchecked(std::size_t value); \ BETTER_ENUMS_CONSTEXPR_ static _optional \ - _from_index_nothrow(_integral value); \ + _from_index_nothrow(std::size_t value); \ \ ToStringConstexpr const char* _to_string() const; \ BETTER_ENUMS_IF_EXCEPTIONS( \ @@ -759,23 +759,23 @@ BETTER_ENUMS_CONSTEXPR_ inline std::size_t Enum::_to_index() const \ } \ \ BETTER_ENUMS_CONSTEXPR_ inline Enum \ -Enum::_from_index_unchecked(_integral value) \ +Enum::_from_index_unchecked(std::size_t index) \ { \ return \ - ::better_enums::_or_zero(_from_index_nothrow(value)); \ + ::better_enums::_or_zero(_from_index_nothrow(index)); \ } \ \ BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional \ -Enum::_from_index_nothrow(_integral value) \ +Enum::_from_index_nothrow(std::size_t index) \ { \ - return _from_index_loop(value); \ + return _from_index_loop(index); \ } \ \ BETTER_ENUMS_IF_EXCEPTIONS( \ -BETTER_ENUMS_CONSTEXPR_ inline Enum Enum::_from_index(_integral value) \ +BETTER_ENUMS_CONSTEXPR_ inline Enum Enum::_from_index(std::size_t index) \ { \ return \ - ::better_enums::_or_throw(_from_index_nothrow(value), \ + ::better_enums::_or_throw(_from_index_nothrow(index), \ #Enum "::_from_index: invalid argument"); \ } \ ) \ From 8e2bc1d8769d098cd08dd107c7feadd982481461 Mon Sep 17 00:00:00 2001 From: Piotr Kosek Date: Sat, 1 Sep 2018 12:41:17 +0200 Subject: [PATCH 12/12] Removed _from_index_loop and moved its functionality to _from_index_nothrow --- enum.h | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/enum.h b/enum.h index 5d534eb5..2034824c 100644 --- a/enum.h +++ b/enum.h @@ -675,8 +675,6 @@ class Enum { \ \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ _from_value_loop(_integral value, std::size_t index = 0); \ - BETTER_ENUMS_CONSTEXPR_ static _optional \ - _from_index_loop(std::size_t index); \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ _from_string_loop(const char *name, std::size_t index = 0); \ BETTER_ENUMS_CONSTEXPR_ static _optional_index \ @@ -717,15 +715,6 @@ Enum::_from_value_loop(Enum::_integral value, std::size_t index) \ _from_value_loop(value, index + 1); \ } \ \ -BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional \ -Enum::_from_index_loop(std::size_t index) \ -{ \ - return \ - index >= _size() ? \ - _optional() : \ - _optional(BETTER_ENUMS_NS(Enum)::_value_array[index]); \ -} \ - \ BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional_index \ Enum::_from_string_loop(const char *name, std::size_t index) \ { \ @@ -768,7 +757,10 @@ Enum::_from_index_unchecked(std::size_t index) \ BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional \ Enum::_from_index_nothrow(std::size_t index) \ { \ - return _from_index_loop(index); \ + return \ + index >= _size() ? \ + _optional() : \ + _optional(BETTER_ENUMS_NS(Enum)::_value_array[index]); \ } \ \ BETTER_ENUMS_IF_EXCEPTIONS( \