Core Function HasFlag
From Sputnik Wiki
HasFlag( <enumInst>, <flag/flags> )
Contents |
Description
Check if a flag (or array of flags) is enabled in a given enum instance.
Parameters
enumInst
Instance of an enum (or any integer will do)
flag
Either:
An enum flag (or any integer)
OR
An array of enum flags.
Return Value
Success: Returns true
Failure: Returns false
Remarks
This is just an easier way to do bit flags.
Example
Basic example
enum { $OPT_A = 0x01, $OPT_B = 0x02, $OPT_C = 0x04, $OPT_H = 0x08 }; $opt = $OPT_A; say "HasFlag 'A' : " . HasFlag($opt, $OPT_A); say "HasFlag 'C' : " . HasFlag($opt, $OPT_C); say "HasFlag 'H' : " . HasFlag($opt, $OPT_H); say "Turning H on"; SetFlag($opt, $OPT_H, true); say "HasFlag 'H' (again) : " . HasFlag($opt, $OPT_H); say "Turning H off"; SetFlag($opt, $OPT_H, false); say "HasFlag 'H' (again) : " . HasFlag($opt, $OPT_H); // PRINTS // HasFlag 'A' : true // HasFlag 'C' : false // HasFlag 'H' : false // Turning H on // HasFlag 'H' (again) : true // Turning H off // HasFlag 'H' (again) : false
Basic example using a literal enum
enum Options { $OPT_A = 0x01, $OPT_B = 0x02, $OPT_C = 0x04, $OPT_H = 0x08 }; $opt = Options->$OPT_A; say "HasFlag 'A' : " . HasFlag($opt, Options->$OPT_A); say "HasFlag 'C' : " . HasFlag($opt, Options->$OPT_C); say "HasFlag 'H' : " . HasFlag($opt, Options->$OPT_H); say "Turning H on"; SetFlag($opt, Options->$OPT_H, true); say "HasFlag 'H' (again) : " . HasFlag($opt, Options->$OPT_H); say "Turning H off"; SetFlag($opt, Options->$OPT_H, false); say "HasFlag 'H' (again) : " . HasFlag($opt, Options->$OPT_H); // PRINTS // HasFlag 'A' : true // HasFlag 'C' : false // HasFlag 'H' : false // Turning H on // HasFlag 'H' (again) : true // Turning H off // HasFlag 'H' (again) : false
Example of using arrays as the flags
enum { $OPT_A = 0x01, $OPT_B = 0x02, $OPT_C = 0x04, $OPT_H = 0x08 }; $opt = $OPT_A; say "HasFlag 'A' : " . HasFlag($opt, $OPT_A); say "HasFlag 'C' : " . HasFlag($opt, $OPT_C); say "HasFlag 'H' : " . HasFlag($opt, $OPT_H); say "Turning H and C on"; SetFlag($opt, array($OPT_H, $OPT_C)); say "HasFlag 'C' (again) : " . HasFlag($opt, $OPT_C); say "HasFlag 'H' (again) : " . HasFlag($opt, $OPT_H); say "HasFlag 'H AND C' (again) : " . HasFlag($opt, array($OPT_H, $OPT_C)); say "Turning H and C off"; SetFlag($opt, array($OPT_H, $OPT_C), false); say "HasFlag 'C' (again) : " . HasFlag($opt, $OPT_C); say "HasFlag 'H' (again) : " . HasFlag($opt, $OPT_H); say "HasFlag 'H AND C' (again) : " . HasFlag($opt, array($OPT_H, $OPT_C)); say "Finally check A"; say "HasFlag 'A' : " . HasFlag($opt, $OPT_A); // PRINTS // HasFlag 'A' : true // HasFlag 'C' : false // HasFlag 'H' : false // Turning H and C on // HasFlag 'C' (again) : true // HasFlag 'H' (again) : true // HasFlag 'H AND C' (again) : true // Turning H and C off // HasFlag 'C' (again) : false // HasFlag 'H' (again) : false // HasFlag 'H AND C' (again) : false // Finally check A // HasFlag 'A' : true