Zebrafish
Zebrafish

Reputation: 13876

What's the easiest way to set bit flags in an enum?

I'm trying to do this, and it's quite tedious to do.

enum class GUIEventEnum : uint64 {
    NONE = 0,
     
    CURSOR_LMB_DOWN = 1 << 1, 
    CURSOR_LMB_UP = 1 << 2,
    CURSOR_LMB_CLICK = 1 << 3,
    CURSOR_RMB_DOWN = 1 << 4,
    CURSOR_RMB_UP = 1 << 5,
    CURSOR_RMB_CLICK = 1 << 6,
    CURSOR_MMB_DOWN = 1 << 7,
    CURSOR_MMB_UP = 1 << 8,
    CURSOR_MMB_CLICK = 1 << 9,
    CURSOR_SCROLL = 1 << 10,
};

And there are going to be many more. Is there a way I can just declare this in a list? like enums?

Upvotes: 0

Views: 299

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473447

If every enumerator is going to have its own bit, then you can write a constexpr function to compute the bit from the enumerator in question:

constexpr inline std::uint64_t event_bit(GUIEventEnum e)
{
  return 0x1 << static_cast<std::uint64_t>(e);
}

Upvotes: 1

Related Questions