杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
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
//
//  BooleanDisposable.swift
//  RxSwift
//
//  Created by Junior B. on 10/29/15.
//  Copyright © 2015 Krunoslav Zaher. All rights reserved.
//
 
/// Represents a disposable resource that can be checked for disposal status.
public final class BooleanDisposable : Cancelable {
 
    internal static let BooleanDisposableTrue = BooleanDisposable(isDisposed: true)
    private let disposed: AtomicInt
    
    /// Initializes a new instance of the `BooleanDisposable` class
    public init() {
        disposed = AtomicInt(0)
    }
    
    /// Initializes a new instance of the `BooleanDisposable` class with given value
    public init(isDisposed: Bool) {
        self.disposed = AtomicInt(isDisposed ? 1 : 0)
    }
    
    /// - returns: Was resource disposed.
    public var isDisposed: Bool {
        isFlagSet(self.disposed, 1)
    }
    
    /// Sets the status to disposed, which can be observer through the `isDisposed` property.
    public func dispose() {
        fetchOr(self.disposed, 1)
    }
}