|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace Rubberduck.Common |
| 4 | +{ |
| 5 | + public struct WindowsVersion : IComparable<WindowsVersion>, IEquatable<WindowsVersion> |
| 6 | + { |
| 7 | + public static readonly WindowsVersion Windows10 = new WindowsVersion(10, 0, 10240); |
| 8 | + public static readonly WindowsVersion Windows81 = new WindowsVersion(6, 3, 9200); |
| 9 | + public static readonly WindowsVersion Windows8 = new WindowsVersion(6, 2, 9200); |
| 10 | + public static readonly WindowsVersion Windows7_SP1 = new WindowsVersion(6, 1, 7601); |
| 11 | + public static readonly WindowsVersion WindowsVista_SP2 = new WindowsVersion(6, 0, 6002); |
| 12 | + |
| 13 | + public WindowsVersion(int major, int minor, int build) |
| 14 | + { |
| 15 | + Major = major; |
| 16 | + Minor = minor; |
| 17 | + Build = build; |
| 18 | + } |
| 19 | + |
| 20 | + public int Major { get; } |
| 21 | + public int Minor { get; } |
| 22 | + public int Build { get; } |
| 23 | + |
| 24 | + |
| 25 | + public int CompareTo(WindowsVersion other) |
| 26 | + { |
| 27 | + var majorComparison = Major.CompareTo(other.Major); |
| 28 | + if (majorComparison != 0) |
| 29 | + { |
| 30 | + return majorComparison; |
| 31 | + } |
| 32 | + |
| 33 | + var minorComparison = Minor.CompareTo(other.Minor); |
| 34 | + |
| 35 | + return minorComparison != 0 |
| 36 | + ? minorComparison |
| 37 | + : Build.CompareTo(other.Build); |
| 38 | + } |
| 39 | + |
| 40 | + public bool Equals(WindowsVersion other) |
| 41 | + { |
| 42 | + return Major == other.Major && Minor == other.Minor && Build == other.Build; |
| 43 | + } |
| 44 | + |
| 45 | + public override bool Equals(object other) |
| 46 | + { |
| 47 | + return other is WindowsVersion otherVersion && Equals(otherVersion); |
| 48 | + } |
| 49 | + |
| 50 | + public override int GetHashCode() |
| 51 | + { |
| 52 | + unchecked |
| 53 | + { |
| 54 | + var hashCode = Major; |
| 55 | + hashCode = (hashCode * 397) ^ Minor; |
| 56 | + hashCode = (hashCode * 397) ^ Build; |
| 57 | + return hashCode; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static bool operator ==(WindowsVersion os1, WindowsVersion os2) |
| 62 | + { |
| 63 | + return os1.CompareTo(os2) == 0; |
| 64 | + } |
| 65 | + |
| 66 | + public static bool operator !=(WindowsVersion os1, WindowsVersion os2) |
| 67 | + { |
| 68 | + return os1.CompareTo(os2) != 0; |
| 69 | + } |
| 70 | + |
| 71 | + public static bool operator <(WindowsVersion os1, WindowsVersion os2) |
| 72 | + { |
| 73 | + return os1.CompareTo(os2) < 0; |
| 74 | + } |
| 75 | + |
| 76 | + public static bool operator >(WindowsVersion os1, WindowsVersion os2) |
| 77 | + { |
| 78 | + return os1.CompareTo(os2) > 0; |
| 79 | + } |
| 80 | + |
| 81 | + public static bool operator <=(WindowsVersion os1, WindowsVersion os2) |
| 82 | + { |
| 83 | + return os1.CompareTo(os2) <= 0; |
| 84 | + } |
| 85 | + |
| 86 | + public static bool operator >=(WindowsVersion os1, WindowsVersion os2) |
| 87 | + { |
| 88 | + return os1.CompareTo(os2) >= 0; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments