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; ProfileRepository.ForcedVideoMode = FORCED_VIDEO_MODE.DETECT;
logger.Info($"Leaving DisplayMagician to detect the best Video Library to use."); 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(); 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()); argumentShortcut.Validators.Add(new ShortcutMustExistValidator());

View File

@ -23,6 +23,8 @@ namespace DisplayMagicianShared.AMD
public bool IsSLSEnabled; public bool IsSLSEnabled;
//public ADL_SLS_MAP[] SLSMap; //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) public bool Equals(AMD_ADAPTER_CONFIG other)
=> AdapterIndex == other.AdapterIndex && => AdapterIndex == other.AdapterIndex &&
AdapterBusNumber == other.AdapterBusNumber && AdapterBusNumber == other.AdapterBusNumber &&
@ -37,6 +39,10 @@ namespace DisplayMagicianShared.AMD
{ {
return (AdapterIndex, AdapterBusNumber, AdapterDeviceNumber, IsPrimaryAdapter, SLSMapIndex, IsSLSEnabled).GetHashCode(); 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)] [StructLayout(LayoutKind.Sequential)]
@ -45,6 +51,8 @@ namespace DisplayMagicianShared.AMD
public List<AMD_ADAPTER_CONFIG> AdapterConfigs; public List<AMD_ADAPTER_CONFIG> AdapterConfigs;
public List<string> DisplayIdentifiers; 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) public bool Equals(AMD_DISPLAY_CONFIG other)
=> AdapterConfigs.SequenceEqual(other.AdapterConfigs); => AdapterConfigs.SequenceEqual(other.AdapterConfigs);
@ -52,6 +60,10 @@ namespace DisplayMagicianShared.AMD
{ {
return (AdapterConfigs).GetHashCode(); 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 public class AMDLibrary : IDisposable

View File

@ -12,7 +12,7 @@ using System.Drawing.Imaging;
namespace DisplayMagicianShared.AMD namespace DisplayMagicianShared.AMD
{ {
public class AMDProfileItem : ProfileItem, IEquatable<AMDProfileItem>, IComparable public class AMDProfileItem : ProfileItem, IComparable
{ {
private static List<AMDProfileItem> _allSavedProfiles = new List<AMDProfileItem>(); private static List<AMDProfileItem> _allSavedProfiles = new List<AMDProfileItem>();
private ProfileIcon _profileIcon; private ProfileIcon _profileIcon;
@ -363,47 +363,29 @@ namespace DisplayMagicianShared.AMD
return _screens; 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; AMDProfileItem otherProfile = (AMDProfileItem)obj;
return this.Name.CompareTo(otherProfile.Name); return this.Name.CompareTo(otherProfile.Name);
}*/ }
// The public override for the Object.Equals // 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 // Profiles are equal if their Viewports are equal
public bool Equals(AMDProfileItem other) public override bool EqualsDerived(object obj)
{ {
return base.EqualsDerived(obj) &&
// If parameter is null, return false. !object.ReferenceEquals(obj, null) &&
if (other is null) obj is AMDProfileItem &&
return false; ((AMDProfileItem)obj).AMDDisplayConfig == this.AMDDisplayConfig &&
((AMDProfileItem)obj).WindowsDisplayConfig == this.WindowsDisplayConfig;
// 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;
} }
public override int GetHashCode() public override int GetHashCode()
@ -414,22 +396,18 @@ namespace DisplayMagicianShared.AMD
public static bool operator ==(AMDProfileItem lhs, AMDProfileItem rhs) public static bool operator ==(AMDProfileItem lhs, AMDProfileItem rhs)
{ {
if (lhs is null) if (object.ReferenceEquals(lhs, rhs))
{ return true;
if (rhs is null)
{ if (!object.ReferenceEquals(lhs, null) &&
!object.ReferenceEquals(rhs, null) &&
lhs.Equals(rhs))
return true; return true;
}
// Only the left side is null.
return false; return false;
} }
// Equals handles case of null on right side.
return lhs.Equals(rhs);
}
public static bool operator !=(AMDProfileItem lhs, AMDProfileItem rhs) => !(lhs == rhs); public static bool operator !=(AMDProfileItem lhs, AMDProfileItem rhs) => !(lhs == rhs);
} }
} }

View File

@ -14,7 +14,7 @@ using DisplayMagicianShared.Windows;
namespace DisplayMagicianShared.NVIDIA namespace DisplayMagicianShared.NVIDIA
{ {
public class NVIDIAProfileItem : ProfileItem, IEquatable<ProfileItem>, IComparable public class NVIDIAProfileItem : ProfileItem, IComparable
{ {
private static List<NVIDIAProfileItem> _allSavedProfiles = new List<NVIDIAProfileItem>(); private static List<NVIDIAProfileItem> _allSavedProfiles = new List<NVIDIAProfileItem>();
private ProfileIcon _profileIcon; private ProfileIcon _profileIcon;
@ -548,18 +548,32 @@ namespace DisplayMagicianShared.NVIDIA
return _screens; 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"); ; if (!(obj is NVIDIAProfileItem)) throw new ArgumentException("Object to CompareTo is not a NVIDIAProfileItem"); ;
NVIDIAProfileItem otherProfile = (NVIDIAProfileItem)obj; NVIDIAProfileItem otherProfile = (NVIDIAProfileItem)obj;
return this.Name.CompareTo(otherProfile.Name); return this.Name.CompareTo(otherProfile.Name);
}*/ }
// The public override for the Object.Equals // 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 // 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) public bool Equals(NVIDIAProfileItem other)
{ {
@ -590,7 +604,7 @@ namespace DisplayMagicianShared.NVIDIA
// Otherwise if all the tests work, then we're good! // Otherwise if all the tests work, then we're good!
return true; return true;
} }
*/
// If Equals() returns true for this object compared to another // If Equals() returns true for this object compared to another
// then GetHashCode() must return the same value for these objects. // then GetHashCode() must return the same value for these objects.
public override int GetHashCode() public override int GetHashCode()
@ -602,19 +616,16 @@ namespace DisplayMagicianShared.NVIDIA
public static bool operator ==(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs) public static bool operator ==(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs)
{ {
if (lhs is null) if (object.ReferenceEquals(lhs, rhs))
{ return true;
if (rhs is null)
{ if (!object.ReferenceEquals(lhs, null) &&
!object.ReferenceEquals(rhs, null) &&
lhs.Equals(rhs))
return true; return true;
}
// Only the left side is null.
return false; return false;
} }
// Equals handles case of null on right side.
return lhs.Equals(rhs);
}
public static bool operator !=(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs) => !(lhs == rhs); 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 System.Text.RegularExpressions;
using IWshRuntimeLibrary; using IWshRuntimeLibrary;
using DisplayMagicianShared.AMD; using DisplayMagicianShared.AMD;
using DisplayMagicianShared.NVIDIA;
using DisplayMagicianShared.Windows;
//using WK.Libraries.HotkeyListenerNS; //using WK.Libraries.HotkeyListenerNS;
namespace DisplayMagicianShared namespace DisplayMagicianShared
@ -50,7 +52,7 @@ namespace DisplayMagicianShared
public int Row; public int Row;
} }
public class ProfileItem : IEquatable<ProfileItem>, IComparable public class ProfileItem : IComparable
{ {
private static List<ProfileItem> _allSavedProfiles = new List<ProfileItem>(); private static List<ProfileItem> _allSavedProfiles = new List<ProfileItem>();
private ProfileIcon _profileIcon; private ProfileIcon _profileIcon;
@ -502,7 +504,7 @@ namespace DisplayMagicianShared
return new List<ScreenPosition>(); 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"); ; 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 // The public override for the Object.Equals
public override bool Equals(object obj) => this.Equals(obj as ProfileItem); public override bool Equals(object obj)
// Profiles are equal if their Viewports are equal
public bool Equals(ProfileItem other)
{ {
return EqualsDerived(obj) &&
// If parameter is null, return false. obj.GetType() == typeof(ProfileItem);
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! // Profiles are equal if their Viewports are equal
return true; public virtual bool EqualsDerived(object obj)
{
return !object.ReferenceEquals(obj, null) &&
obj is ProfileItem &&
((ProfileItem)obj).ProfileDisplayIdentifiers == this.ProfileDisplayIdentifiers;
} }
// If Equals() returns true for this object compared to another // If Equals() returns true for this object compared to another
@ -549,19 +538,16 @@ namespace DisplayMagicianShared
public static bool operator ==(ProfileItem lhs, ProfileItem rhs) public static bool operator ==(ProfileItem lhs, ProfileItem rhs)
{ {
if (lhs is null) if (object.ReferenceEquals(lhs, rhs))
{ return true;
if (rhs is null)
{ if (!object.ReferenceEquals(lhs, null) &&
!object.ReferenceEquals(rhs, null) &&
lhs.Equals(rhs))
return true; return true;
}
// Only the left side is null.
return false; return false;
} }
// Equals handles case of null on right side.
return lhs.Equals(rhs);
}
public static bool operator !=(ProfileItem lhs, ProfileItem rhs) => !(lhs == rhs); public static bool operator !=(ProfileItem lhs, ProfileItem rhs) => !(lhs == rhs);

View File

@ -649,12 +649,6 @@ namespace DisplayMagicianShared
return false; 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) if (profile is NVIDIAProfileItem && _currentProfile is NVIDIAProfileItem)
{ {
NVIDIAProfileItem nvidiaNewProfile = (NVIDIAProfileItem)profile; NVIDIAProfileItem nvidiaNewProfile = (NVIDIAProfileItem)profile;

View File

@ -11,7 +11,7 @@ using System.Drawing.Imaging;
namespace DisplayMagicianShared.Windows namespace DisplayMagicianShared.Windows
{ {
public class WinProfileItem : ProfileItem, IEquatable<WinProfileItem>, IComparable public class WinProfileItem : ProfileItem, IComparable
{ {
private static List<WinProfileItem> _allSavedProfiles = new List<WinProfileItem>(); private static List<WinProfileItem> _allSavedProfiles = new List<WinProfileItem>();
private ProfileIcon _profileIcon; private ProfileIcon _profileIcon;
@ -318,44 +318,28 @@ namespace DisplayMagicianShared.Windows
return _screens; 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"); ; if (!(obj is WinProfileItem)) throw new ArgumentException("Object to CompareTo is not a WinProfileItem"); ;
WinProfileItem otherProfile = (WinProfileItem)obj; WinProfileItem otherProfile = (WinProfileItem)obj;
return this.Name.CompareTo(otherProfile.Name); return this.Name.CompareTo(otherProfile.Name);
}*/ }
// The public override for the Object.Equals // 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 // Profiles are equal if their Viewports are equal
public bool Equals(WinProfileItem other) public override bool EqualsDerived(object obj)
{ {
return base.EqualsDerived(obj) &&
// If parameter is null, return false. !object.ReferenceEquals(obj, null) &&
if (other is null) obj is WinProfileItem &&
return false; ((WinProfileItem)obj).WindowsDisplayConfig == this.WindowsDisplayConfig;
// 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;
} }
// If Equals() returns true for this object compared to another // If Equals() returns true for this object compared to another
@ -369,19 +353,16 @@ namespace DisplayMagicianShared.Windows
public static bool operator ==(WinProfileItem lhs, WinProfileItem rhs) public static bool operator ==(WinProfileItem lhs, WinProfileItem rhs)
{ {
if (lhs is null) if (object.ReferenceEquals(lhs, rhs))
{ return true;
if (rhs is null)
{ if (!object.ReferenceEquals(lhs, null) &&
!object.ReferenceEquals(rhs, null) &&
lhs.Equals(rhs))
return true; return true;
}
// Only the left side is null.
return false; return false;
} }
// Equals handles case of null on right side.
return lhs.Equals(rhs);
}
public static bool operator !=(WinProfileItem lhs, WinProfileItem rhs) => !(lhs == rhs); public static bool operator !=(WinProfileItem lhs, WinProfileItem rhs) => !(lhs == rhs);
} }