# Flags

`flags.h` gives every "has a set of boolean flags" object in the codebase (`Node`, `Device`, `Model`, `Instance`, and others) a common, type-safe way to store and manipulate them as a scoped `enum class` bitmask, instead of a raw `int`.

## `DEFINE_FLAG_OPERATORS`

A plain `enum class` gets no bitwise operators by design - `~`/`|`/`&`/`<<`/`>>` don't exist for it, so using one as a bitmask normally forces an ugly cast to the underlying integer type at every use. `DEFINE_FLAG_OPERATORS(EnumType)` generates `constexpr` `~`, `|`, `&`, `<<`, `>>`, and `==`/`!=` (against a plain `int`, for comparing against `0`) for a specific `enum class`, each implemented by casting to `std::underlying_type_t<EnumType>`, doing the integer operation, and casting back. Invoked once per flag enum (see `NodeFlags` in [Nodes](node.md), `DeviceFlags`/`ModelFlags`/`InstanceFlags` in [Device, Instance, and Model Base Classes](devbase.md), and `Rpn::BrFlags` in [Expression Format](rpnexpr.md)), it lets each of those enums be combined and tested with ordinary operator syntax while still being a distinct type the compiler won't let you accidentally mix with an unrelated flag enum.

## `FlagBase<EnumType>`

A small CRTP-free mixin base class holding one `flags_` member of the enum type and the operations every flag-holding class needs: `setFlags()`/`clearFlags()` (`clearFlags()` with no argument clears everything, via a mask of all-ones), `checkFlags(f)` (true iff every bit in `f` is set), and `maskedFlags(mask)`. `flags()` returns the raw current value. Classes that need flags (`Node : public FlagBase<NodeFlags>`, `Device : public FlagBase<DeviceFlags>`, ...) inherit from it rather than reimplementing the same handful of bit operations themselves - one implementation shared across every flag-holding type in the codebase.
