| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Runtime.Serialization; |
| | 7 | |
|
| | 8 | | namespace GDX.Developer |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// A comparable weak reference to an object which will not prevent garbage collection. It will positively |
| | 12 | | /// compare against similar targeted reference trackers as well as the actual target object. |
| | 13 | | /// </summary> |
| | 14 | | /// <remarks> |
| | 15 | | /// There isn't a lot of great use cases for using this sort of thing; <see cref="WeakReference" /> on its own |
| | 16 | | /// is sufficient in most of the use cases, however this particular arrangement is useful for developer-ish |
| | 17 | | /// stuff. |
| | 18 | | /// </remarks> |
| | 19 | | public class TransientReference : WeakReference, IComparable, IComparable<TransientReference>, |
| | 20 | | IComparable<WeakReference>, IEquatable<TransientReference>, |
| | 21 | | IEquatable<WeakReference> |
| | 22 | | { |
| | 23 | | /// <summary> |
| | 24 | | /// Create a <see cref="TransientReference" /> referencing the <paramref name="target" />. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="target">The target <see cref="object" /> to reference.</param> |
| 17975 | 27 | | public TransientReference(object target) : base(target) |
| 17975 | 28 | | { |
| 17975 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Create a <see cref="TransientReference" /> referencing the <paramref name="target" />. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="target">The target <see cref="object" /> to reference.</param> |
| | 35 | | /// <param name="trackResurrection">Should the object remain tracked after it has been finalized.</param> |
| 5 | 36 | | public TransientReference(object target, bool trackResurrection) : base(target, trackResurrection) |
| 5 | 37 | | { |
| 5 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Create a <see cref="TransientReference" /> from the <paramref name="info" />. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="info">A <see cref="SerializationInfo" /> representation of a <see cref="TransientReference" />. |
| | 44 | | /// <param name="context">Describes the source of the <see cref="SerializationInfo" />.</param> |
| 0 | 45 | | protected TransientReference(SerializationInfo info, StreamingContext context) : base(info, context) |
| 0 | 46 | | { |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Compare this <see cref="TransientReference" /> to the target <see cref="object" />. |
| | 51 | | /// </summary> |
| | 52 | | /// <param name="obj">The target <see cref="object" /> to compare against.</param> |
| | 53 | | /// <returns>1 if the same, 0 otherwise.</returns> |
| | 54 | | public int CompareTo(object obj) |
| 1 | 55 | | { |
| 1 | 56 | | if (obj == Target || (TransientReference)obj == this) |
| 1 | 57 | | { |
| 1 | 58 | | return 1; |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | return 0; |
| 1 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Compare this <see cref="TransientReference" /> to the target <see cref="TransientReference" />. |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="obj">The target <see cref="TransientReference" /> to compare against.</param> |
| | 68 | | /// <returns>1 if the same, 0 otherwise.</returns> |
| | 69 | | public int CompareTo(TransientReference obj) |
| 2 | 70 | | { |
| 2 | 71 | | if (obj.Target == Target || obj == this) |
| 1 | 72 | | { |
| 1 | 73 | | return 1; |
| | 74 | | } |
| | 75 | |
|
| 1 | 76 | | return 0; |
| 2 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Compare this <see cref="TransientReference" /> to the target <see cref="WeakReference" />. |
| | 81 | | /// </summary> |
| | 82 | | /// <param name="obj">The target <see cref="WeakReference" /> to compare against.</param> |
| | 83 | | /// <returns>1 if the same, 0 otherwise.</returns> |
| | 84 | | public int CompareTo(WeakReference obj) |
| 1 | 85 | | { |
| 1 | 86 | | if (obj.Target == Target || (TransientReference)obj == this) |
| 1 | 87 | | { |
| 1 | 88 | | return 1; |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | return 0; |
| 1 | 92 | | } |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// Does this <see cref="TransientReference" /> equal the target <see cref="TransientReference" />. |
| | 96 | | /// </summary> |
| | 97 | | /// <param name="other">The target <see cref="TransientReference" /> to compare with.</param> |
| | 98 | | /// <returns>true if it is the same, false otherwise.</returns> |
| | 99 | | public bool Equals(TransientReference other) |
| 1 | 100 | | { |
| 1 | 101 | | if (other == null) |
| 0 | 102 | | { |
| 0 | 103 | | return false; |
| | 104 | | } |
| | 105 | |
|
| 1 | 106 | | return Target == other.Target; |
| 1 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Does this <see cref="TransientReference" /> equal the target <see cref="WeakReference" />. |
| | 111 | | /// </summary> |
| | 112 | | /// <param name="other">The target <see cref="WeakReference" /> to compare with.</param> |
| | 113 | | /// <returns>true if it is the same, false otherwise.</returns> |
| | 114 | | public bool Equals(WeakReference other) |
| 0 | 115 | | { |
| 0 | 116 | | if (other == null) |
| 0 | 117 | | { |
| 0 | 118 | | return false; |
| | 119 | | } |
| | 120 | |
|
| 0 | 121 | | return Target == other.Target; |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | /// <summary> |
| | 125 | | /// Does this <see cref="TransientReference" /> equal the target <see cref="object" />. |
| | 126 | | /// </summary> |
| | 127 | | /// <param name="obj">The target <see cref="object" /> to compare with.</param> |
| | 128 | | /// <returns>true if it is the same, false otherwise.</returns> |
| | 129 | | public override bool Equals(object obj) |
| 1 | 130 | | { |
| 1 | 131 | | if (ReferenceEquals(null, obj) || ReferenceEquals(this, obj)) |
| 0 | 132 | | { |
| 0 | 133 | | return false; |
| | 134 | | } |
| | 135 | |
|
| 1 | 136 | | if (obj == Target) |
| 0 | 137 | | { |
| 0 | 138 | | return true; |
| | 139 | | } |
| | 140 | |
|
| 1 | 141 | | if (obj.GetType() != GetType()) |
| 0 | 142 | | { |
| 0 | 143 | | return false; |
| | 144 | | } |
| | 145 | |
|
| 1 | 146 | | return Equals((TransientReference)obj); |
| 1 | 147 | | } |
| | 148 | |
|
| | 149 | | /// <summary> |
| | 150 | | /// Return the hashcode of the <see cref="WeakReference.Target" />. |
| | 151 | | /// </summary> |
| | 152 | | /// <returns>Returns the <see cref="WeakReference.Target" />'s hash code, or -1 if null.</returns> |
| | 153 | | public override int GetHashCode() |
| 44060 | 154 | | { |
| 44060 | 155 | | if (Target == null) |
| 0 | 156 | | { |
| 0 | 157 | | return -1; |
| | 158 | | } |
| | 159 | |
|
| 44060 | 160 | | return Target.GetHashCode(); |
| 44060 | 161 | | } |
| | 162 | |
|
| | 163 | | /// <summary> |
| | 164 | | /// Compare <see cref="TransientReference" />s to see if they are equal. |
| | 165 | | /// </summary> |
| | 166 | | /// <param name="left">Left-side <see cref="TransientReference" />.</param> |
| | 167 | | /// <param name="right">Right-side <see cref="TransientReference" />.</param> |
| | 168 | | /// <returns>true/false if they are equal.</returns> |
| | 169 | | public static bool operator ==(TransientReference left, TransientReference right) |
| 2 | 170 | | { |
| 2 | 171 | | return Equals(left, right); |
| 2 | 172 | | } |
| | 173 | |
|
| | 174 | | /// <summary> |
| | 175 | | /// Compare <see cref="TransientReference" />s to see if they are not equal. |
| | 176 | | /// </summary> |
| | 177 | | /// <param name="left">Left-side <see cref="TransientReference" />.</param> |
| | 178 | | /// <param name="right">Right-side <see cref="TransientReference" />.</param> |
| | 179 | | /// <returns>true/false if they are not equal.</returns> |
| | 180 | | public static bool operator !=(TransientReference left, TransientReference right) |
| 17974 | 181 | | { |
| 17974 | 182 | | return !Equals(left, right); |
| 17974 | 183 | | } |
| | 184 | | } |
| | 185 | | } |