Skip to content

Instantly share code, notes, and snippets.

@LuckyResistor
Created May 6, 2018
Embed
What would you like to do?
A demonstration of more advanced flags usage.
#include "Flags.hpp"
#include <iostream>
enum class Demo : uint16_t {
Enabled = (1<<0),
Mode5k = (0x0<<2),
Mode10k = (0x1<<2),
Mode16k = (0x2<<2),
Mode20k = (0x3<<2),
ModeMask = (0x3<<2),
Speed0 = (0x0<<9),
Speed1 = (0x1<<9),
Speed2 = (0x2<<9),
SpeedMask = (0x7<<9),
SpeedDefault = Speed1,
};
LR_DECLARE_FLAGS(Demo, Demos)
LR_DECLARE_OPERATORS_FOR_FLAGS(Demos)
int main()
{
// Initializer list.
Demos demos = {Demo::Enabled, Demo::Mode10k, Demo::SpeedDefault};
// Flag check.
if (demos.isSet(Demo::Enabled)) {
std::cout << "Enabled" << std::endl;
}
// Masking flags.
if ((demos&Demo::ModeMask) == Demos(Demo::Mode10k)) {
std::cout << "Mode 10k" << std::endl;
}
// Using a switch statement for masked flags.
switch (demos&Demo::SpeedMask) {
case Demos(Demo::Speed0):
std::cout << "Speed 0" << std::endl;
break;
case Demos(Demo::Speed1):
std::cout << "Speed 1" << std::endl;
break;
case Demos(Demo::Speed2):
std::cout << "Speed 2" << std::endl;
break;
default:
break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment