Implemented new derived class equality logic

Used new derived class equality class from https://codinghelmet.com/articles/equals-override which helped me understand the logic for deriving equality between base classes and derived classes. It has now exposed another error in the way FORCED_VIDEO_MODE is being applied and that is now something else I need to fix!
This commit is contained in:
Terry MacDonald 2021-09-04 16:32:42 +12:00
parent 964d982bd1
commit a112326e50
7 changed files with 105 additions and 144 deletions

View File

@ -288,7 +288,6 @@ namespace DisplayMagician {
ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.DETECT;
logger.Info($"Leaving DisplayMagician to detect the best Video Library to use.");
}
var argumentShortcut = runShortcutCmd.Argument("\"SHORTCUT_UUID\"", "(required) The UUID of the shortcut to run from those stored in the shortcut library.").IsRequired();
argumentShortcut.Validators.Add(new ShortcutMustExistValidator());

View File

@ -23,6 +23,8 @@ namespace DisplayMagicianShared.AMD
public bool IsSLSEnabled;
//public ADL_SLS_MAP[] SLSMap;
public override bool Equals(object obj) => obj is AMD_ADAPTER_CONFIG other && this.Equals(other);
public bool Equals(AMD_ADAPTER_CONFIG other)
=> AdapterIndex == other.AdapterIndex &&
AdapterBusNumber == other.AdapterBusNumber &&
@ -37,6 +39,10 @@ namespace DisplayMagicianShared.AMD
{
return (AdapterIndex, AdapterBusNumber, AdapterDeviceNumber, IsPrimaryAdapter, SLSMapIndex, IsSLSEnabled).GetHashCode();
}
public static bool operator ==(AMD_ADAPTER_CONFIG lhs, AMD_ADAPTER_CONFIG rhs) => lhs.Equals(rhs);
public static bool operator !=(AMD_ADAPTER_CONFIG lhs, AMD_ADAPTER_CONFIG rhs) => !(lhs == rhs);
}
[StructLayout(LayoutKind.Sequential)]
@ -45,6 +51,8 @@ namespace DisplayMagicianShared.AMD
public List<AMD_ADAPTER_CONFIG> AdapterConfigs;
public List<string> DisplayIdentifiers;
public override bool Equals(object obj) => obj is AMD_DISPLAY_CONFIG other && this.Equals(other);
public bool Equals(AMD_DISPLAY_CONFIG other)
=> AdapterConfigs.SequenceEqual(other.AdapterConfigs);
@ -52,6 +60,10 @@ namespace DisplayMagicianShared.AMD
{
return (AdapterConfigs).GetHashCode();
}
public static bool operator ==(AMD_DISPLAY_CONFIG lhs, AMD_DISPLAY_CONFIG rhs) => lhs.Equals(rhs);
public static bool operator !=(AMD_DISPLAY_CONFIG lhs, AMD_DISPLAY_CONFIG rhs) => !(lhs == rhs);
}
public class AMDLibrary : IDisposable

View File

@ -12,7 +12,7 @@ using System.Drawing.Imaging;
namespace DisplayMagicianShared.AMD
{
public class AMDProfileItem : ProfileItem, IEquatable<AMDProfileItem>, IComparable
public class AMDProfileItem : ProfileItem, IComparable
{
private static List<AMDProfileItem> _allSavedProfiles = new List<AMDProfileItem>();
private ProfileIcon _profileIcon;
@ -363,47 +363,29 @@ namespace DisplayMagicianShared.AMD
return _screens;
}
/*public int CompareTo(object obj)
public override int CompareTo(object obj)
{
if (!(obj is ProfileItem)) throw new ArgumentException("Object to CompareTo is not a AMDProfileItem"); ;
if (!(obj is AMDProfileItem)) throw new ArgumentException("Object to CompareTo is not a AMDProfileItem"); ;
AMDProfileItem otherProfile = (AMDProfileItem)obj;
return this.Name.CompareTo(otherProfile.Name);
}*/
}
// The public override for the Object.Equals
public override bool Equals(object obj) => this.Equals(obj as AMDProfileItem);
public override bool Equals(object obj)
{
return EqualsDerived(obj) &&
obj.GetType() == typeof(AMDProfileItem);
}
// Profiles are equal if their Viewports are equal
public bool Equals(AMDProfileItem other)
public override bool EqualsDerived(object obj)
{
// If parameter is null, return false.
if (other is null)
return false;
// Optimization for a common success case.
if (Object.ReferenceEquals(this, other))
return true;
// If run-time types are not exactly the same, return false.
if (this.GetType() != other.GetType())
return false;
// If AMD Display Config is different then return false.
if (!AMDDisplayConfig.Equals(other.AMDDisplayConfig))
return false;
// If Windows Display Config is different then return false.
if (!WindowsDisplayConfig.Equals(other.WindowsDisplayConfig))
return false;
// If Display Identifiers are different then return false.
if (!ProfileDisplayIdentifiers.SequenceEqual(other.ProfileDisplayIdentifiers))
return false;
// Otherwise if all the tests work, then we're good!
return true;
return base.EqualsDerived(obj) &&
!object.ReferenceEquals(obj, null) &&
obj is AMDProfileItem &&
((AMDProfileItem)obj).AMDDisplayConfig == this.AMDDisplayConfig &&
((AMDProfileItem)obj).WindowsDisplayConfig == this.WindowsDisplayConfig;
}
public override int GetHashCode()
@ -414,22 +396,18 @@ namespace DisplayMagicianShared.AMD
public static bool operator ==(AMDProfileItem lhs, AMDProfileItem rhs)
{
if (lhs is null)
{
if (rhs is null)
{
return true;
}
if (object.ReferenceEquals(lhs, rhs))
return true;
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return lhs.Equals(rhs);
if (!object.ReferenceEquals(lhs, null) &&
!object.ReferenceEquals(rhs, null) &&
lhs.Equals(rhs))
return true;
return false;
}
public static bool operator !=(AMDProfileItem lhs, AMDProfileItem rhs) => !(lhs == rhs);
}
}

View File

@ -14,7 +14,7 @@ using DisplayMagicianShared.Windows;
namespace DisplayMagicianShared.NVIDIA
{
public class NVIDIAProfileItem : ProfileItem, IEquatable<ProfileItem>, IComparable
public class NVIDIAProfileItem : ProfileItem, IComparable
{
private static List<NVIDIAProfileItem> _allSavedProfiles = new List<NVIDIAProfileItem>();
private ProfileIcon _profileIcon;
@ -548,18 +548,32 @@ namespace DisplayMagicianShared.NVIDIA
return _screens;
}
/*public int CompareTo(object obj)
public override int CompareTo(object obj)
{
if (!(obj is NVIDIAProfileItem)) throw new ArgumentException("Object to CompareTo is not a NVIDIAProfileItem"); ;
NVIDIAProfileItem otherProfile = (NVIDIAProfileItem)obj;
return this.Name.CompareTo(otherProfile.Name);
}*/
}
// The public override for the Object.Equals
public override bool Equals(object obj) => this.Equals(obj as NVIDIAProfileItem);
public override bool Equals(object obj)
{
return EqualsDerived(obj) &&
obj.GetType() == typeof(NVIDIAProfileItem);
}
// Profiles are equal if their Viewports are equal
public override bool EqualsDerived(object obj)
{
return base.EqualsDerived(obj) &&
!object.ReferenceEquals(obj, null) &&
obj is NVIDIAProfileItem &&
((NVIDIAProfileItem)obj).NVIDIADisplayConfig == this.NVIDIADisplayConfig &&
((NVIDIAProfileItem)obj).WindowsDisplayConfig == this.WindowsDisplayConfig;
}
/* // Profiles are equal if their Viewports are equal
public bool Equals(NVIDIAProfileItem other)
{
@ -590,7 +604,7 @@ namespace DisplayMagicianShared.NVIDIA
// Otherwise if all the tests work, then we're good!
return true;
}
*/
// If Equals() returns true for this object compared to another
// then GetHashCode() must return the same value for these objects.
public override int GetHashCode()
@ -602,18 +616,15 @@ namespace DisplayMagicianShared.NVIDIA
public static bool operator ==(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs)
{
if (lhs is null)
{
if (rhs is null)
{
return true;
}
if (object.ReferenceEquals(lhs, rhs))
return true;
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return lhs.Equals(rhs);
if (!object.ReferenceEquals(lhs, null) &&
!object.ReferenceEquals(rhs, null) &&
lhs.Equals(rhs))
return true;
return false;
}
public static bool operator !=(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs) => !(lhs == rhs);

View File

@ -10,6 +10,8 @@ using System.Drawing.Imaging;
using System.Text.RegularExpressions;
using IWshRuntimeLibrary;
using DisplayMagicianShared.AMD;
using DisplayMagicianShared.NVIDIA;
using DisplayMagicianShared.Windows;
//using WK.Libraries.HotkeyListenerNS;
namespace DisplayMagicianShared
@ -50,7 +52,7 @@ namespace DisplayMagicianShared
public int Row;
}
public class ProfileItem : IEquatable<ProfileItem>, IComparable
public class ProfileItem : IComparable
{
private static List<ProfileItem> _allSavedProfiles = new List<ProfileItem>();
private ProfileIcon _profileIcon;
@ -502,7 +504,7 @@ namespace DisplayMagicianShared
return new List<ScreenPosition>();
}
public int CompareTo(object obj)
public virtual int CompareTo(object obj)
{
if (!(obj is ProfileItem)) throw new ArgumentException("Object to CompareTo is not a Shortcut"); ;
@ -511,31 +513,18 @@ namespace DisplayMagicianShared
}
// The public override for the Object.Equals
public override bool Equals(object obj) => this.Equals(obj as ProfileItem);
public override bool Equals(object obj)
{
return EqualsDerived(obj) &&
obj.GetType() == typeof(ProfileItem);
}
// Profiles are equal if their Viewports are equal
public bool Equals(ProfileItem other)
public virtual bool EqualsDerived(object obj)
{
// If parameter is null, return false.
if (other is null)
return false;
// Optimization for a common success case.
if (Object.ReferenceEquals(this, other))
return true;
// If run-time types are not exactly the same, return false.
if (this.GetType() != other.GetType())
return false;
if (!this.ProfileDisplayIdentifiers.SequenceEqual(other.ProfileDisplayIdentifiers))
{
return false;
}
// Otherwise if all the tests work, then we're good!
return true;
return !object.ReferenceEquals(obj, null) &&
obj is ProfileItem &&
((ProfileItem)obj).ProfileDisplayIdentifiers == this.ProfileDisplayIdentifiers;
}
// If Equals() returns true for this object compared to another
@ -549,18 +538,15 @@ namespace DisplayMagicianShared
public static bool operator ==(ProfileItem lhs, ProfileItem rhs)
{
if (lhs is null)
{
if (rhs is null)
{
return true;
}
if (object.ReferenceEquals(lhs, rhs))
return true;
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return lhs.Equals(rhs);
if (!object.ReferenceEquals(lhs, null) &&
!object.ReferenceEquals(rhs, null) &&
lhs.Equals(rhs))
return true;
return false;
}
public static bool operator !=(ProfileItem lhs, ProfileItem rhs) => !(lhs == rhs);

View File

@ -647,13 +647,7 @@ namespace DisplayMagicianShared
if (profile == null){
SharedLogger.logger.Error($"ProfileRepository/IsActiveProfile: The requested profile {profile.Name} is null. Not changing anything, and reporting an error");
return false;
}
if (Object.ReferenceEquals(_currentProfile, profile))
{
SharedLogger.logger.Trace($"ProfileRepository/IsActiveProfile: The requested profile {profile.Name} is the currently active profile. Not changing anything.");
return true;
}
}
if (profile is NVIDIAProfileItem && _currentProfile is NVIDIAProfileItem)
{

View File

@ -11,7 +11,7 @@ using System.Drawing.Imaging;
namespace DisplayMagicianShared.Windows
{
public class WinProfileItem : ProfileItem, IEquatable<WinProfileItem>, IComparable
public class WinProfileItem : ProfileItem, IComparable
{
private static List<WinProfileItem> _allSavedProfiles = new List<WinProfileItem>();
private ProfileIcon _profileIcon;
@ -318,44 +318,28 @@ namespace DisplayMagicianShared.Windows
return _screens;
}
/*public int CompareTo(object obj)
public override int CompareTo(object obj)
{
if (!(obj is WinProfileItem)) throw new ArgumentException("Object to CompareTo is not a WinProfileItem"); ;
WinProfileItem otherProfile = (WinProfileItem)obj;
return this.Name.CompareTo(otherProfile.Name);
}*/
}
// The public override for the Object.Equals
public override bool Equals(object obj) => this.Equals(obj as WinProfileItem);
public override bool Equals(object obj)
{
return EqualsDerived(obj) &&
obj.GetType() == typeof(WinProfileItem);
}
// Profiles are equal if their Viewports are equal
public bool Equals(WinProfileItem other)
public override bool EqualsDerived(object obj)
{
// If parameter is null, return false.
if (other is null)
return false;
// Optimization for a common success case.
if (Object.ReferenceEquals(this, other))
return true;
// If run-time types are not exactly the same, return false.
if (this.GetType() != other.GetType())
return false;
// If Windows Display Config is different then return false.
if (!WindowsDisplayConfig.Equals(other.WindowsDisplayConfig))
return false;
// If Display Identifiers are different then return false.
if (!ProfileDisplayIdentifiers.SequenceEqual(other.ProfileDisplayIdentifiers))
return false;
// Otherwise if all the tests work, then we're good!
return true;
return base.EqualsDerived(obj) &&
!object.ReferenceEquals(obj, null) &&
obj is WinProfileItem &&
((WinProfileItem)obj).WindowsDisplayConfig == this.WindowsDisplayConfig;
}
// If Equals() returns true for this object compared to another
@ -369,18 +353,15 @@ namespace DisplayMagicianShared.Windows
public static bool operator ==(WinProfileItem lhs, WinProfileItem rhs)
{
if (lhs is null)
{
if (rhs is null)
{
return true;
}
if (object.ReferenceEquals(lhs, rhs))
return true;
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return lhs.Equals(rhs);
if (!object.ReferenceEquals(lhs, null) &&
!object.ReferenceEquals(rhs, null) &&
lhs.Equals(rhs))
return true;
return false;
}
public static bool operator !=(WinProfileItem lhs, WinProfileItem rhs) => !(lhs == rhs);