1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | using System; using Smooth.Delegates; using Smooth.Pools; namespace Smooth.Dispose { #if UNITY_IOS || UNITY_PS3 || UNITY_XBOX360 || UNITY_WII /// <summary> /// Wrapper around a value that uses the IDisposable interface to dispose of the value. /// /// On iOS, this is a struct to avoid compute_class_bitmap errors. /// /// On other platforms, it is a pooled object to avoid boxing when disposed by a using block with the Unity compiler. /// </summary> public struct Disposable<T> : IDisposable { /// <summary> /// Borrows a wrapper for the specified value and disposal delegate. /// </summary> public static Disposable<T> Borrow(T value, DelegateAction<T> dispose) { return new Disposable<T>(value, dispose); } private readonly DelegateAction<T> dispose; /// <summary> /// The wrapped value. /// </summary> public readonly T value; public Disposable(T value, DelegateAction<T> dispose) { this.value = value; this.dispose = dispose; } /// <summary> /// Relinquishes ownership of the wrapper and disposes the wrapped value. /// </summary> public void Dispose() { dispose(value); } /// <summary> /// Relinquishes ownership of the wrapper and adds it to the disposal queue. /// </summary> public void DisposeInBackground() { DisposalQueue.Enqueue(this); } } #else /// <summary> /// Wrapper around a value that uses the IDisposable interface to dispose of the value. /// /// On IOS, this is a value type to avoid compute_class_bitmap errors. /// /// On other platforms, it is a pooled object to avoid boxing when disposed by a using block with the Unity compiler. /// </summary> public class Disposable<T> : IDisposable { private static readonly Pool<Disposable<T>> pool = new Pool<Disposable<T>>( () => new Disposable<T>(), wrapper => { wrapper.dispose(wrapper.value); wrapper.dispose = t => {}; wrapper.value = default(T); } ); /// <summary> /// Borrows a wrapper for the specified value and disposal delegate. /// </summary> public static Disposable<T> Borrow(T value, DelegateAction<T> dispose) { var wrapper = pool.Borrow(); wrapper.value = value; wrapper.dispose = dispose; return wrapper; } private DelegateAction<T> dispose; /// <summary> /// The wrapped value. /// </summary> public T value { get; private set; } private Disposable() {} /// <summary> /// Relinquishes ownership of the wrapper, disposes the wrapped value, and returns the wrapper to the pool. /// </summary> public void Dispose() { pool.Release(this); } /// <summary> /// Relinquishes ownership of the wrapper and adds it to the disposal queue. /// </summary> public void DisposeInBackground() { DisposalQueue.Enqueue(this); } } #endif } |