better-enums/example/7-bitset.cc
Anton Bachin d9bd109172 Removed range properties.
They can now be easily computed using the random access iterators. There appears
to be a slight performance improvement.
2015-05-18 09:29:35 -05:00

45 lines
1.0 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>() + 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]
<< std::endl;
}
if (red_and_blue[Channel::Green])
std::cout << "bit set contains Green" << std::endl;
return 0;
}