Skip to content

Conversation

@axelriet
Copy link
Contributor

Adding inline bitwise mask/raise macros.

  • WI_MaskFlag(var, flag)
  • WI_MaskAllFlags(var, flag)
  • WI_RaiseFlag(var, flag)
  • WI_RaiseAllFlags(var, flag)

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

  • SetFileAttributes(Path, WI_MaskFlag(Attr, FILE_ATTRIBUTE_READONLY)); // Mask flag inline without modifying the variable
  • SetFileAttributes(Path, WI_RaiseFlag(Attr, FILE_ATTRIBUTE_READONLY)); // Raise flag inline without modifying the variable

Adding inline bitwise mask/raise macros.
@oldnewthing
Copy link
Member

"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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants