microsoft / wil

Windows Implementation Library
MIT License
2.57k stars 234 forks source link

Add WI_MaskFlag/WI_RaiseFlag inline macros #469

Open axelriet opened 2 weeks ago

axelriet commented 2 weeks ago

Adding inline bitwise mask/raise macros.

The new macros are meant for inline usage without side effects on the variable. Examples:

oldnewthing commented 2 weeks ago

"MaskFlag" is ambiguous. Am I masking out the flag, or masking in the flag? In bitmap graphics, a mask is something that has set bits to represent the bits you want to keep, so my initial guess was that MaskFlag is "remove all flags except the ones in the mask".

"RaiseFlag" sounds like it's raising an exception.

How about WI_WithFlag and WI_WithoutFlag?

SetFileAttributes(Path, WI_WithFlag(Attr, FILE_ATTRIBUTE_READONLY)) // Turn on the readonly attribute
SetFileAttributes(Path, WI_WithoutFlag(Attr, FILE_ATTRIBUTE_READONLY)) // Turn off the readonly attribute

Maybe we should also break the WI_ClearFlag family of macros so that they cannot be used as expressions.

#define WI_ClearAllFlags(var, flags) do { (var) &= ~(flags)); } while ((void)0, 0)

That way if somebody tries

SetFileAttributes(Path, WI_ClearFlag(Attr, FILE_ATTRIBUTE_READONLY)) // Turn off the readonly attribute

they get a build break instead of a mystery variable mutation.