// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
#if !NO_TPL
using System.Threading;
namespace System.Reactive.Disposables
{
    /// 
    /// Represents a disposable resource that has an associated  that will be set to the cancellation requested state upon disposal.
    /// 
    public sealed class CancellationDisposable : ICancelable
    {
        private readonly CancellationTokenSource _cts;
        /// 
        /// Initializes a new instance of the  class that uses an existing .
        /// 
        ///  used for cancellation.
        ///  is null.
        public CancellationDisposable(CancellationTokenSource cts)
        {
            if (cts == null)
                throw new ArgumentNullException("cts");
            _cts = cts;
        }
        /// 
        /// Initializes a new instance of the  class that uses a new .
        /// 
        public CancellationDisposable()
            : this(new CancellationTokenSource())
        {
        }
        /// 
        /// Gets the  used by this CancellationDisposable.
        /// 
        public CancellationToken Token
        {
            get { return _cts.Token; }
        }
        /// 
        /// Cancels the underlying .
        /// 
        public void Dispose()
        {
            _cts.Cancel();
        }
        /// 
        /// Gets a value that indicates whether the object is disposed.
        /// 
        public bool IsDisposed
        {
            get { return _cts.IsCancellationRequested; }
        }
    }
}
#endif