src/cflags

A C-compatible bitmask flags interface, with a subset of nim set functionality

Types

Flags[T; E] = distinct T

A C-compatible bitmask-set interface

this exists because nim sets aren't compatible with C bitmasks

T: SomeInteger is the backing type for the flags type and determines the base type and size

E: enum is the enum type containing the range of values that the set is meant to represent, be careful not to accidentally overlap the bits in the values

Overlapping enum values in bitfields is sometimes done on purpose but note that when stringized only the basic values will be shown, not overlapping values

Procs

proc `$`[T, E](flags: Flags[T, E]): string {.noSideEffect.}

stringize a flags in a format similar to a set

cannot be called at compiletime

proc `*`[T, E](flags1, flags2: Flags[T, E]): Flags[T, E] {.noSideEffect.}

intersect of flags1 and flags2

can be called at compiletime

proc `+`[T, E](flags1, flags2: Flags[T, E]): Flags[T, E] {.noSideEffect.}

union of flags1 and flags2

can be called at compiletime

proc `-`[T, E](flags1, flags2: Flags[T, E]): Flags[T, E] {.noSideEffect.}

difference of flags1 and flags2

can be called at compiletime

proc `==`[T, E](flags1, flags2: Flags[T, E]): bool {.noSideEffect.}

check if setflags is an identity of flags

can be called at compiletime

proc `==`[T, E](flags: Flags[T, E]; arrflags: openArray[E]): bool {.noSideEffect.}

check if arrflags is an identity of flags

can be called at compiletime

proc `==`[T, E](flags: Flags[T, E]; setflags: set[E]): bool {.noSideEffect.}

check if setflags is an identity of flags

cannot be called at compiletime

func card[T, E](flags: Flags[T, E]): int

returns number of set flags in flags

cannot be called at compiletime

proc contains[T, E](flags, subflags: Flags[T, E]): bool {.noSideEffect.}

check if subflags is a subset or identity of flags

can be called at compiletime

proc contains[T, E](flags: Flags[T, E]; arrflags: openArray[E]): bool {.
    noSideEffect.}

check if arrflags is a subset or identity of flags

can be called at compiletime

proc contains[T, E](flags: Flags[T, E]; flag: E): bool {.noSideEffect.}

check if flag is contained in flags

can be called at compiletime

proc contains[T, E](flags: Flags[T, E]; setflags: set[E]): bool {.noSideEffect.}

check if setflags is a subset or identity of flags

cannot be called at compiletime

func makeFlags[T, E](BackingType: typedesc[T]; varflags: varargs[E]): Flags[T, E]

make a Flags type from variadic arguments BackingType is used to streamline generic type substitution

can be called at comptime, use this or toFlags[T, E](arrflags, BackingType) if you need a flag compiletime constant

func set[T, E](flags: var Flags[T, E]; flag: E)
set a flag value in flags
func subsetOf[T, E](flags, superflags: Flags[T, E]): bool

check if flags is a subset of superflags (but not an identity of)

can be called at compiletime

func supersetOf[T, E](flags, subflags: Flags[T, E]): bool

check if flags is a superset of subflags (but not an identity of)

can be called at compiletime

func toFlags[T, E](arrflags: openArray[E]): Flags[T, E]
func toFlags[T, E](arrflags: openArray[E]; BackingType: typedesc[T]): Flags[T, E]

convert an open array into a flags value

BackingType is used to streamline generic type substitution

can be called at comptime, use this or makeFlags if you need a flag compiletime constant

func toFlags[T, E](setflags: set[E]): Flags[T, E]
func toFlags[T, E](setflags: set[E]; BackingType: typedesc[T]): Flags[T, E]

convert a nim set into a flags value

BackingType is used to streamline generic type substitution

cannot be called at compiletime due to tyIntX to tyEnum castin the holeyItems iterator

func toSet[T, E](flags: Flags[T, E]): set[E]

convert flags to a nim set

cannot be called at compiletime

func unset[T, E](flags: var Flags[T, E]; flag: E)
unset a flag value in flags

Iterators

iterator holeyItems[T: enum](a: set[T]): T {.inline.}

Iterates over each element of a. holeyItems iterates only over the elements that are really in the set (and not over the ones the set is able to hold).

uses a cast expression instead of a normal cast to allow handling enums with holes without HoleEnumConv warnings

cannot be called at compiletime because the VM doesn't support tyIntX to tyEnum casts

iterator items[T, E](flags: Flags[T, E]): E {.inline.}

iterator over flags contained in flags

not callable at compiletime