Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declare accessor properties for bitfields #1113

Merged
merged 1 commit into from
Jan 12, 2024
Merged

Declare accessor properties for bitfields #1113

merged 1 commit into from
Jan 12, 2024

Conversation

AArnott
Copy link
Member

@AArnott AArnott commented Jan 12, 2024

Here is a sample of the output, as taken from generation of the BM_REQUEST_TYPE struct:

internal partial struct _BM
{
	internal byte _bitfield;

	/// <summary>Gets or sets bits 0-1 in the <see cref="_bitfield" /> field.</summary>
	internal byte Recipient
	{
		get => (byte)((this._bitfield & unchecked((byte)0x03)) >> 0);
		set
		{
			if ((value & unchecked((byte)~0x03)) != 0)
				throw new ArgumentOutOfRangeException(nameof(value));
			this._bitfield = (byte)((this._bitfield & unchecked((byte)~0x03)) | ((byte)value << 0));
		}
	}

	/// <summary>Gets or sets bits 2-4 in the <see cref="_bitfield" /> field.</summary>
	internal byte Reserved
	{
		get => (byte)((this._bitfield & unchecked((byte)0x1C)) >> 2);
		set
		{
			if ((value & unchecked((byte)~0x07)) != 0)
				throw new ArgumentOutOfRangeException(nameof(value));
			this._bitfield = (byte)((this._bitfield & unchecked((byte)~0x1C)) | ((byte)value << 2));
		}
	}

	/// <summary>Gets or sets bits 5-6 in the <see cref="_bitfield" /> field.</summary>
	internal byte Type
	{
		get => (byte)((this._bitfield & unchecked((byte)0x60)) >> 5);
		set
		{
			if ((value & unchecked((byte)~0x03)) != 0)
				throw new ArgumentOutOfRangeException(nameof(value));
			this._bitfield = (byte)((this._bitfield & unchecked((byte)~0x60)) | ((byte)value << 5));
		}
	}

	/// <summary>Gets or sets bit 7 in the <see cref="_bitfield" /> field.</summary>
	internal bool Dir
	{
		get => (this._bitfield & 0x80) != 0;
		set => this._bitfield = (byte)(value ? this._bitfield | 0x80 : (this._bitfield & unchecked((byte)~0x80)));
	}
}

Notice how the types of the accessor properties vary by the length of each bit field. It can be bool, byte, ushort, uint or ulong, as necessary to be at least as large as the length of the bit field.

Closes #987

@AArnott AArnott merged commit bb31419 into main Jan 12, 2024
1 of 2 checks passed
@AArnott AArnott deleted the fix987 branch January 12, 2024 04:26
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.

Improve bit fields support
1 participant