#! @file flags.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains `flags32`, a set of up to 32 flags held as a `u32`, whose bits are #! named by the members of an enum. module core; #! A set of up to 32 flags, held as a `u32`, whose bits are named by the members #! of an enum. #! #! It converts to and from `u32` as any alias does, so it is written where an #! integer of flags is wanted, and its methods read and write the bits by member #! or by index. #! @tparam T The enum naming the bits, whose members' values are the bits they #! set. pub type flags32 = u32 where T : ordinal; #! Whether the flags hold any bit of a value. #! @param this The flags to read. #! @param value The value whose bits to look for. #! @returns Whether any of its bits is set. pub def flags32::get(&this, value: T): bool { return (this & value as u32) != 0; } #! Whether the flags hold a bit. #! @param this The flags to read. #! @param index Which bit, from 0 to 31. #! @returns Whether it is set. pub def flags32::get(&this, index: u8): bool { return (this & ((1 as u32) << index as u32)) != 0; } #! Sets the bits of a value. #! @param this The flags to change. #! @param value The value whose bits to set. pub def flags32::set(&mut this, value: T): void { this |= value as u32; } #! Sets or clears a bit. #! @param this The flags to change. #! @param index Which bit, from 0 to 31. #! @param value Whether the bit is set, or else cleared. pub def flags32::set(&mut this, index: u8, value: bool): void { val bit := (1 as u32) << index as u32; this = value ? this | bit : this & ~bit; } #! The flags with the bits of a value set too. #! @param this The flags to start from. #! @param value The value whose bits to set. #! @returns The flags with those bits set, leaving these as they are. pub def flags32::with(&this, value: T): flags32 { return this | value as u32; } #! The flags with a bit set or cleared. #! @param this The flags to start from. #! @param index Which bit, from 0 to 31. #! @param value Whether the bit is set, or else cleared. #! @returns The flags with the bit changed, leaving these as they are. pub def flags32::with(&this, index: u8, value: bool): flags32 { val bit := (1 as u32) << index as u32; return value ? this | bit : this & ~bit; }