/// <summary> /// Indicates whether the current object is equal to another object of the same type. /// </summary> /// <param name="other">An object to compare with this object.</param> /// <returns> /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false. /// </returns> public bool Equals(PoolValue <T> other) { if (this == _Empty) { return(other.IsEmpty); } return(EqualityComparer <T> .Default.Equals(_value, other._value)); }
/// <summary> /// Returns a new PoolValue type containing a value. /// </summary> /// <param name="value">The value.</param> /// <param name="defaultIsEmpty"> </param> /// <param name="doNotPool"> </param> /// <returns></returns> public static PoolValue <T> NewValue(T value, bool defaultIsEmpty, bool doNotPool) { if (defaultIsEmpty && value.Equals(default(T))) { return(Empty); } var newItem = new PoolValue <T>(value); if (doNotPool) { return(newItem); } lock (_PoolLock) { if (PooledItems.TryGetValue(newItem, out var oldItem)) { return(oldItem); } PooledItems[newItem] = newItem; } return(newItem); }
/// <summary> /// Clears the pool. /// </summary> public static void ClearPool() { PoolValue <T> .ClearPool(); }
/// <summary> /// Ensures the internal value is pooled. Usually called after clearing the pool. /// </summary> public void Repool() { _value = PoolValue <T> .NewValue(_value.Value); }
/// <summary> /// Initializes a new instance of the <see cref="DataItem<T>"/> class, /// specifying whether the default for (T) results in an "empty" value. /// </summary> /// <param name="value">The value.</param> /// <param name="defaultIsEmpty">if set to <c>true</c> [default is empty].</param> /// <param name="doNotPool">if set to <c>true</c> [do not pool].</param> public DataItem(T value, bool defaultIsEmpty, bool doNotPool) { _value = PoolValue <T> .NewValue(value, defaultIsEmpty, doNotPool); }
/// <summary> /// Initializes a new instance of the <see cref="DataItem<T>"/> class with a value. /// </summary> /// <param name="value">The value.</param> public DataItem(T value) { _value = PoolValue <T> .NewValue(value, _defaultIsEmpty, false); }
/// <summary> /// Initializes a new instance of the <see cref="DataItem<T>"/> class with the "empty" value. /// </summary> public DataItem() { _value = PoolValue <T> .Empty; }