From 0e48228c5476d78cf70f7bafe9af90261e6b1af8 Mon Sep 17 00:00:00 2001 From: Chanwoo Ahn Date: Thu, 7 Mar 2024 20:15:26 +0900 Subject: [PATCH] fix: Fix to support full 256 alternatives --- include/efp/enum.hpp | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/include/efp/enum.hpp b/include/efp/enum.hpp index 264253b..bb3f121 100644 --- a/include/efp/enum.hpp +++ b/include/efp/enum.hpp @@ -9,7 +9,7 @@ namespace detail { // Use bit operation and recursion constexpr uint8_t power_2_ceiling(uint8_t n, uint8_t power = 2) { - return (power >= n) ? power : power_2_ceiling(n, power << 1); + return (power >= n) ? power - 1 : power_2_ceiling(n, power << 1); } // clang-format off @@ -32,7 +32,7 @@ namespace detail { struct _EnumSwitch {}; template class Case, typename... Args> - struct _EnumSwitch<2, alt_num, Case, Args...> { + struct _EnumSwitch<2 - 1, alt_num, Case, Args...> { static auto call(uint8_t index, Args&&... args) -> decltype(Case<0>::call(std::forward(args)...)) { switch (index) { @@ -44,7 +44,7 @@ namespace detail { }; template class Case, typename... Args> - struct _EnumSwitch<4, alt_num, Case, Args...> { + struct _EnumSwitch<4 - 1, alt_num, Case, Args...> { static auto call(uint8_t index, Args&&... args) -> decltype(Case<0>::call(std::forward(args)...)) { switch (index) { @@ -56,7 +56,7 @@ namespace detail { }; template class Case, typename... Args> - struct _EnumSwitch<8, alt_num, Case, Args...> { + struct _EnumSwitch<8 - 1, alt_num, Case, Args...> { static auto call(uint8_t index, Args&&... args) -> decltype(Case<0>::call(std::forward(args)...)) { switch (index) { @@ -68,7 +68,7 @@ namespace detail { }; template class Case, typename... Args> - struct _EnumSwitch<16, alt_num, Case, Args...> { + struct _EnumSwitch<16 - 1, alt_num, Case, Args...> { static auto call(uint8_t index, Args&&... args) -> decltype(Case<0>::call(std::forward(args)...)) { switch (index) { @@ -80,7 +80,7 @@ namespace detail { }; template class Case, typename... Args> - struct _EnumSwitch<32, alt_num, Case, Args...> { + struct _EnumSwitch<32 - 1, alt_num, Case, Args...> { static auto call(uint8_t index, Args&&... args) -> decltype(Case<0>::call(std::forward(args)...)) { switch (index) { @@ -92,7 +92,7 @@ namespace detail { }; template class Case, typename... Args> - struct _EnumSwitch<64, alt_num, Case, Args...> { + struct _EnumSwitch<64 - 1, alt_num, Case, Args...> { static auto call(uint8_t index, Args&&... args) -> decltype(Case<0>::call(std::forward(args)...)) { switch (index) { @@ -104,7 +104,7 @@ namespace detail { }; template class Case, typename... Args> - struct _EnumSwitch<128, alt_num, Case, Args...> { + struct _EnumSwitch<128 - 1, alt_num, Case, Args...> { static auto call(uint8_t index, Args&&... args) -> decltype(Case<0>::call(std::forward(args)...)) { switch (index) { @@ -115,18 +115,17 @@ namespace detail { } }; - // ! temp - // template class Case, typename... Args> - // struct _EnumSwitch<256, alt_num, Case, Args...> { - // static auto call(uint8_t index, Args&&... args) - // -> decltype(Case<0>::call(std::forward(args)...)) { - // switch (index) { - // EFP_STAMP256(0, EFP_ENUM_CASE) - // default: - // throw RuntimeError("Invalid alternative index"); - // } - // } - // }; + template class Case, typename... Args> + struct _EnumSwitch<256 - 1, alt_num, Case, Args...> { + static auto call(uint8_t index, Args&&... args) + -> decltype(Case<0>::call(std::forward(args)...)) { + switch (index) { + EFP_STAMP256(0, EFP_ENUM_CASE) + default: + throw RuntimeError("Invalid alternative index"); + } + } + }; #undef EFP_ENUM_CASE