Difference between revisions of "CUtlFlags"

From Valve Developer Community
Jump to: navigation, search
(Created Page.)
 
m
 
Line 62: Line 62:
 
[[Category:Tier1]]
 
[[Category:Tier1]]
 
[[Category:Classes]]
 
[[Category:Classes]]
 +
[[Category:Flags]]

Latest revision as of 11:06, 8 April 2011

CUtlFlags is a simple class that makes the use of flags easier. Code can be found in public/tier1/utlflags.h.

Setting flags

Setting a flag can be done using the SetFlag method, which takes a bitmask and the option to turn the flag on or off.

// Example of a flag enum
enum FlagEnum_t
{
	FLAG_ONE = 0x0001,
	FLAG_TWO = 0x0002,
	FLAG_THREE = 0x0004,
	FLAG_FOUR = 0x0008,
};

// Initialize flag class
CUtlFlags< unsigned short > flags;

// Turn on FLAG_ONE and FLAG_TWO
flags.SetFlag( FLAG_ONE );
flags.SetFlag( FLAG_TWO );

// Turn off FLAG_TWO
flags.SetFlag( FLAG_TWO, false );

Clearing flags

Just as easily, you can clear a flag using SetFlag( FLAG, false ) or the more succinct ClearFlag( FLAG ):

// Clear FLAG_ONE
flags.ClearFlag( FLAG_ONE );

You can also clear all flags:

// Clear all flags
flags.ClearAllFlags();

Querying flags

Using IsFlagSet( FLAG ) and IsAnyFlagSet(), you can check if a certain flag is set:

// Clear all flags
flags.ClearAllFlags();

flags.IsFlagSet( FLAG_ONE ); // Returns false

flags.SetFlag( FLAG_TWO );

flags.IsAnyFlagSet(); // Returns true