using System; using System.Collections; using System.Collections.Generic; using JetBrains.Lifetimes; namespace JetBrains.Collections.Viewable { /// /// For usage in code contexts /// /// /// public class ModificationCookieViewableSet : IViewableSet where T: notnull where TCookie: struct, IDisposable { private readonly Func myCookieFactory; private readonly IViewableSet myBackingSet; public ModificationCookieViewableSet(Func cookieFactory, IViewableSet backingSet) { myCookieFactory = cookieFactory; myBackingSet = backingSet; } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public ISource> Change => myBackingSet.Change; public void Advise(Lifetime lifetime, Action> handler) { myBackingSet.Advise(lifetime, handler); } public IEnumerator GetEnumerator() { return myBackingSet.GetEnumerator(); } public bool Contains(T item) { return myBackingSet.Contains(item); } public int Count => myBackingSet.Count; public bool IsReadOnly => myBackingSet.IsReadOnly; public void CopyTo(T[] array, int arrayIndex) { myBackingSet.CopyTo(array, arrayIndex); } public bool IsProperSubsetOf(IEnumerable other) { return myBackingSet.IsProperSubsetOf(other); } public bool IsProperSupersetOf(IEnumerable other) { return myBackingSet.IsProperSupersetOf(other); } public bool IsSubsetOf(IEnumerable other) { return myBackingSet.IsSubsetOf(other); } public bool IsSupersetOf(IEnumerable other) { return myBackingSet.IsSupersetOf(other); } public bool Overlaps(IEnumerable other) { return myBackingSet.Overlaps(other); } public bool SetEquals(IEnumerable other) { return myBackingSet.SetEquals(other); } public bool Add(T item) { using (myCookieFactory()) return myBackingSet.Add(item); } public bool Remove(T item) { using (myCookieFactory()) return myBackingSet.Remove(item); } public void Clear() { using (myCookieFactory()) myBackingSet.Clear(); } public void ExceptWith(IEnumerable other) { using (myCookieFactory()) myBackingSet.ExceptWith(other); } public void IntersectWith(IEnumerable other) { using (myCookieFactory()) myBackingSet.IntersectWith(other); } public void UnionWith(IEnumerable other) { using (myCookieFactory()) myBackingSet.UnionWith(other); } public void SymmetricExceptWith(IEnumerable other) { using (myCookieFactory()) myBackingSet.SymmetricExceptWith(other); } void ICollection.Add(T item) { using (myCookieFactory()) myBackingSet.Add(item); } } }