Skip to content

Commit

Permalink
Merge pull request #5462 from YohDeadfall/has-enum
Browse files Browse the repository at this point in the history
HasFlagCustom made safe and supports all enums
  • Loading branch information
MarchingCube authored Feb 11, 2021
2 parents 6ff478b + 0a7ca87 commit 6332dfc
Show file tree
Hide file tree
Showing 25 changed files with 150 additions and 150 deletions.
30 changes: 26 additions & 4 deletions src/Avalonia.Base/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,32 @@ public static class EnumExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool HasFlagCustom<T>(this T value, T flag) where T : unmanaged, Enum
{
var intValue = *(int*)&value;
var intFlag = *(int*)&flag;

return (intValue & intFlag) == intFlag;
if (sizeof(T) == 1)
{
var byteValue = Unsafe.As<T, byte>(ref value);
var byteFlag = Unsafe.As<T, byte>(ref flag);
return (byteValue & byteFlag) == byteFlag;
}
else if (sizeof(T) == 2)
{
var shortValue = Unsafe.As<T, short>(ref value);
var shortFlag = Unsafe.As<T, short>(ref flag);
return (shortValue & shortFlag) == shortFlag;
}
else if (sizeof(T) == 4)
{
var intValue = Unsafe.As<T, int>(ref value);
var intFlag = Unsafe.As<T, int>(ref flag);
return (intValue & intFlag) == intFlag;
}
else if (sizeof(T) == 8)
{
var longValue = Unsafe.As<T, long>(ref value);
var longFlag = Unsafe.As<T, long>(ref flag);
return (longValue & longFlag) == longFlag;
}
else
throw new NotSupportedException("Enum with size of " + Unsafe.SizeOf<T>() + " are not supported");
}
}
}
4 changes: 2 additions & 2 deletions src/Avalonia.Base/Utilities/TypeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ private static MethodInfo FindTypeConversionOperatorMethod(Type fromType, Type t
const string implicitName = "op_Implicit";
const string explicitName = "op_Explicit";

bool allowImplicit = (operatorType & OperatorType.Implicit) != 0;
bool allowExplicit = (operatorType & OperatorType.Explicit) != 0;
bool allowImplicit = operatorType.HasFlagCustom(OperatorType.Implicit);
bool allowExplicit = operatorType.HasFlagCustom(OperatorType.Explicit);

foreach (MethodInfo method in fromType.GetMethods())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2595,7 +2595,7 @@ private void AdjustCurrencyForRemove(int index)
/// <returns>Whether the specified flag is set</returns>
private bool CheckFlag(CollectionViewFlags flags)
{
return (_flags & flags) != 0;
return _flags.HasFlagCustom(flags);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Controls/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ protected override void OnKeyDown(KeyEventArgs e)
return;

if (e.Key == Key.F4 ||
((e.Key == Key.Down || e.Key == Key.Up) && ((e.KeyModifiers & KeyModifiers.Alt) != 0)))
((e.Key == Key.Down || e.Key == Key.Up) && e.KeyModifiers.HasFlagCustom(KeyModifiers.Alt)))
{
IsDropDownOpen = !IsDropDownOpen;
e.Handled = true;
Expand Down
45 changes: 16 additions & 29 deletions src/Avalonia.Controls/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ internal double GetFinalRowDefinitionHeight(int rowIndex)
/// </summary>
internal bool MeasureOverrideInProgress
{
get { return (CheckFlagsAnd(Flags.MeasureOverrideInProgress)); }
get { return CheckFlags(Flags.MeasureOverrideInProgress); }
set { SetFlags(value, Flags.MeasureOverrideInProgress); }
}

Expand All @@ -646,7 +646,7 @@ internal bool MeasureOverrideInProgress
/// </summary>
internal bool ArrangeOverrideInProgress
{
get { return (CheckFlagsAnd(Flags.ArrangeOverrideInProgress)); }
get { return CheckFlags(Flags.ArrangeOverrideInProgress); }
set { SetFlags(value, Flags.ArrangeOverrideInProgress); }
}

Expand Down Expand Up @@ -2350,25 +2350,12 @@ private void SetFlags(bool value, Flags flags)
}

/// <summary>
/// CheckFlagsAnd returns <c>true</c> if all the flags in the
/// CheckFlags returns <c>true</c> if all the flags in the
/// given bitmask are set on the object.
/// </summary>
private bool CheckFlagsAnd(Flags flags)
private bool CheckFlags(Flags flags)
{
return ((_flags & flags) == flags);
}

/// <summary>
/// CheckFlagsOr returns <c>true</c> if at least one flag in the
/// given bitmask is set.
/// </summary>
/// <remarks>
/// If no bits are set in the given bitmask, the method returns
/// <c>true</c>.
/// </remarks>
private bool CheckFlagsOr(Flags flags)
{
return (flags == 0 || (_flags & flags) != 0);
return _flags.HasFlagCustom(flags);
}

private static void OnShowGridLinesPropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
Expand Down Expand Up @@ -2535,7 +2522,7 @@ private CellCache[] PrivateCells
/// </summary>
private bool CellsStructureDirty
{
get { return (!CheckFlagsAnd(Flags.ValidCellsStructure)); }
get { return !CheckFlags(Flags.ValidCellsStructure); }
set { SetFlags(!value, Flags.ValidCellsStructure); }
}

Expand All @@ -2544,7 +2531,7 @@ private bool CellsStructureDirty
/// </summary>
private bool ListenToNotifications
{
get { return (CheckFlagsAnd(Flags.ListenToNotifications)); }
get { return CheckFlags(Flags.ListenToNotifications); }
set { SetFlags(value, Flags.ListenToNotifications); }
}

Expand All @@ -2553,7 +2540,7 @@ private bool ListenToNotifications
/// </summary>
private bool SizeToContentU
{
get { return (CheckFlagsAnd(Flags.SizeToContentU)); }
get { return CheckFlags(Flags.SizeToContentU); }
set { SetFlags(value, Flags.SizeToContentU); }
}

Expand All @@ -2562,7 +2549,7 @@ private bool SizeToContentU
/// </summary>
private bool SizeToContentV
{
get { return (CheckFlagsAnd(Flags.SizeToContentV)); }
get { return CheckFlags(Flags.SizeToContentV); }
set { SetFlags(value, Flags.SizeToContentV); }
}

Expand All @@ -2571,7 +2558,7 @@ private bool SizeToContentV
/// </summary>
private bool HasStarCellsU
{
get { return (CheckFlagsAnd(Flags.HasStarCellsU)); }
get { return CheckFlags(Flags.HasStarCellsU); }
set { SetFlags(value, Flags.HasStarCellsU); }
}

Expand All @@ -2580,7 +2567,7 @@ private bool HasStarCellsU
/// </summary>
private bool HasStarCellsV
{
get { return (CheckFlagsAnd(Flags.HasStarCellsV)); }
get { return CheckFlags(Flags.HasStarCellsV); }
set { SetFlags(value, Flags.HasStarCellsV); }
}

Expand All @@ -2589,7 +2576,7 @@ private bool HasStarCellsV
/// </summary>
private bool HasGroup3CellsInAutoRows
{
get { return (CheckFlagsAnd(Flags.HasGroup3CellsInAutoRows)); }
get { return CheckFlags(Flags.HasGroup3CellsInAutoRows); }
set { SetFlags(value, Flags.HasGroup3CellsInAutoRows); }
}

Expand Down Expand Up @@ -2803,10 +2790,10 @@ private struct CellCache
internal LayoutTimeSizeType SizeTypeU;
internal LayoutTimeSizeType SizeTypeV;
internal int Next;
internal bool IsStarU { get { return ((SizeTypeU & LayoutTimeSizeType.Star) != 0); } }
internal bool IsAutoU { get { return ((SizeTypeU & LayoutTimeSizeType.Auto) != 0); } }
internal bool IsStarV { get { return ((SizeTypeV & LayoutTimeSizeType.Star) != 0); } }
internal bool IsAutoV { get { return ((SizeTypeV & LayoutTimeSizeType.Auto) != 0); } }
internal bool IsStarU => SizeTypeU.HasFlagCustom(LayoutTimeSizeType.Star);
internal bool IsAutoU => SizeTypeU.HasFlagCustom(LayoutTimeSizeType.Auto);
internal bool IsStarV => SizeTypeV.HasFlagCustom(LayoutTimeSizeType.Star);
internal bool IsAutoV => SizeTypeV.HasFlagCustom(LayoutTimeSizeType.Auto);
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Avalonia.Controls/ListBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ protected override void OnGotFocus(GotFocusEventArgs e)
e.Handled = UpdateSelectionFromEventSource(
e.Source,
true,
(e.KeyModifiers & KeyModifiers.Shift) != 0,
(e.KeyModifiers & KeyModifiers.Control) != 0);
e.KeyModifiers.HasFlagCustom(KeyModifiers.Shift),
e.KeyModifiers.HasFlagCustom(KeyModifiers.Control));
}
}

Expand All @@ -154,8 +154,8 @@ protected override void OnPointerPressed(PointerPressedEventArgs e)
e.Handled = UpdateSelectionFromEventSource(
e.Source,
true,
(e.KeyModifiers & KeyModifiers.Shift) != 0,
(e.KeyModifiers & KeyModifiers.Control) != 0,
e.KeyModifiers.HasFlagCustom(KeyModifiers.Shift),
e.KeyModifiers.HasFlagCustom(KeyModifiers.Control),
point.Properties.IsRightButtonPressed);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Avalonia.Controls/Platform/InProcessDragSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ private DragDropEffects GetPreferredEffect(DragDropEffects effect, RawInputModif
{
if (effect == DragDropEffects.Copy || effect == DragDropEffects.Move || effect == DragDropEffects.Link || effect == DragDropEffects.None)
return effect; // No need to check for the modifiers.
if (effect.HasFlag(DragDropEffects.Link) && modifiers.HasFlag(RawInputModifiers.Alt))
if (effect.HasFlagCustom(DragDropEffects.Link) && modifiers.HasFlagCustom(RawInputModifiers.Alt))
return DragDropEffects.Link;
if (effect.HasFlag(DragDropEffects.Copy) && modifiers.HasFlag(RawInputModifiers.Control))
if (effect.HasFlagCustom(DragDropEffects.Copy) && modifiers.HasFlagCustom(RawInputModifiers.Control))
return DragDropEffects.Copy;
return DragDropEffects.Move;
}

private StandardCursorType GetCursorForDropEffect(DragDropEffects effects)
{
if (effects.HasFlag(DragDropEffects.Copy))
if (effects.HasFlagCustom(DragDropEffects.Copy))
return StandardCursorType.DragCopy;
if (effects.HasFlag(DragDropEffects.Move))
if (effects.HasFlagCustom(DragDropEffects.Move))
return StandardCursorType.DragMove;
if (effects.HasFlag(DragDropEffects.Link))
if (effects.HasFlagCustom(DragDropEffects.Link))
return StandardCursorType.DragLink;
return StandardCursorType.No;
}
Expand Down Expand Up @@ -161,7 +161,7 @@ private void ProcessMouseEvents(RawPointerEventArgs e)

void CheckDraggingAccepted(RawInputModifiers changedMouseButton)
{
if (_initialInputModifiers.Value.HasFlag(changedMouseButton))
if (_initialInputModifiers.Value.HasFlagCustom(changedMouseButton))
{
var result = RaiseEventAndUpdateCursor(RawDragEventType.Drop, e.Root, e.Position, e.InputModifiers);
UpdateCursor(null, DragDropEffects.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,8 @@ static class PopupPositioningEdgeHelper
{
public static void ValidateEdge(this PopupAnchor edge)
{
if (((edge & PopupAnchor.Left) != 0 && (edge & PopupAnchor.Right) != 0)
||
((edge & PopupAnchor.Top) != 0 && (edge & PopupAnchor.Bottom) != 0))
if (edge.HasFlagCustom(PopupAnchor.Left) && edge.HasFlagCustom(PopupAnchor.Right) ||
edge.HasFlagCustom(PopupAnchor.Top) && edge.HasFlagCustom(PopupAnchor.Bottom))
throw new ArgumentException("Opposite edges specified");
}

Expand All @@ -266,25 +265,25 @@ public static void ValidateGravity(this PopupGravity gravity)

public static PopupAnchor Flip(this PopupAnchor edge)
{
var hmask = PopupAnchor.Left | PopupAnchor.Right;
var vmask = PopupAnchor.Top | PopupAnchor.Bottom;
if ((edge & hmask) != 0)
edge ^= hmask;
if ((edge & vmask) != 0)
edge ^= vmask;
if (edge.HasFlagCustom(PopupAnchor.HorizontalMask))
edge ^= PopupAnchor.HorizontalMask;

if (edge.HasFlagCustom(PopupAnchor.VerticalMask))
edge ^= PopupAnchor.VerticalMask;

return edge;
}

public static PopupAnchor FlipX(this PopupAnchor edge)
{
if ((edge & PopupAnchor.HorizontalMask) != 0)
if (edge.HasFlagCustom(PopupAnchor.HorizontalMask))
edge ^= PopupAnchor.HorizontalMask;
return edge;
}

public static PopupAnchor FlipY(this PopupAnchor edge)
{
if ((edge & PopupAnchor.VerticalMask) != 0)
if (edge.HasFlagCustom(PopupAnchor.VerticalMask))
edge ^= PopupAnchor.VerticalMask;
return edge;
}
Expand Down
Loading

0 comments on commit 6332dfc

Please sign in to comment.