Created
April 26, 2022 07:39
-
-
Save rioki/72fcd7b9b661e85967cb3dd039b5287f to your computer and use it in GitHub Desktop.
one_of: a small piece of code that removes switch on enum statements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename EnumT> constexpr | |
bool one_of(EnumT value, const std::initializer_list<EnumT>& checkValues) | |
{ | |
for (const auto& cv : checkValues) | |
{ | |
if (value == cv) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum class TestEnum | |
{ | |
A, | |
B, | |
C | |
}; | |
TEST(one_of, one_of) | |
{ | |
EXPECT_TRUE(one_of(TestEnum::A, {TestEnum::A, TestEnum::B})); | |
EXPECT_TRUE(one_of(TestEnum::B, {TestEnum::A, TestEnum::B})); | |
EXPECT_FALSE(one_of(TestEnum::C, {TestEnum::A, TestEnum::B})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment