123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System;
- using System.Collections;
- namespace UniRx
- {
- public sealed class MultipleAssignmentDisposable : IDisposable, ICancelable
- {
- static readonly BooleanDisposable True = new BooleanDisposable(true);
- object gate = new object();
- IDisposable current;
- public bool IsDisposed
- {
- get
- {
- lock (gate)
- {
- return current == True;
- }
- }
- }
- public IDisposable Disposable
- {
- get
- {
- lock (gate)
- {
- return (current == True)
- ? UniRx.Disposable.Empty
- : current;
- }
- }
- set
- {
- var shouldDispose = false;
- lock (gate)
- {
- shouldDispose = (current == True);
- if (!shouldDispose)
- {
- current = value;
- }
- }
- if (shouldDispose && value != null)
- {
- value.Dispose();
- }
- }
- }
- public void Dispose()
- {
- IDisposable old = null;
- lock (gate)
- {
- if (current != True)
- {
- old = current;
- current = True;
- }
- }
- if (old != null) old.Dispose();
- }
- }
- }
|