mirror of
https://github.com/aantron/better-enums.git
synced 2025-12-06 16:56:42 +08:00
These modifications ensure enum.h can be used in a wider selection of end user projects without triggering warnings. GCC 4.9.2 was used with the following warning flags set: -Wall -Wextra -Wshadow -Weffc++ -Wno-unused-parameter -Wno-unused-local-typedefs -Wno-long-long -Wstrict-aliasing -Werror -pedantic -std=c++1y -Wformat=2 -Wmissing-include-dirs -Wsync-nand -Wuninitialized -Wconditionally-supported -Wconversion -Wuseless-cast -Wzero-as-null-pointer-constant This commit includes the modifications required to enable successful use of enum.h via both the "test" and "example" directories.
45 lines
1.1 KiB
C++
45 lines
1.1 KiB
C++
// Usage with std::bitset.
|
|
|
|
#include <bitset>
|
|
#include <iostream>
|
|
#include <enum.h>
|
|
|
|
// Computes the maximum value of an enum at compile time.
|
|
template <typename Enum>
|
|
constexpr Enum maximum(Enum accumulator = Enum::_values[0], size_t index = 1)
|
|
{
|
|
return
|
|
index >= Enum::_size ? accumulator :
|
|
+Enum::_values[index] > accumulator ?
|
|
maximum(+Enum::_values[index], index + 1) :
|
|
maximum(accumulator, index + 1);
|
|
}
|
|
|
|
ENUM(Channel, int, Red, Green, Blue)
|
|
|
|
int main()
|
|
{
|
|
using ChannelSet = std::bitset<maximum<Channel>().to_integral() + 1>;
|
|
|
|
ChannelSet red_only;
|
|
red_only.set(Channel::Red);
|
|
|
|
ChannelSet blue_only;
|
|
blue_only.set(Channel::Blue);
|
|
|
|
ChannelSet red_and_blue = red_only | blue_only;
|
|
|
|
for (Channel channel : Channel::_values) {
|
|
std::cout
|
|
<< channel.to_string()
|
|
<< " bit is set to "
|
|
<< red_and_blue[channel.to_integral()]
|
|
<< std::endl;
|
|
}
|
|
|
|
if (red_and_blue[Channel::Green])
|
|
std::cout << "bit set contains Green" << std::endl;
|
|
|
|
return 0;
|
|
}
|